Creating Google Document by Converting PDF and Image Files with OCR using Google Apps Script


This is a sample script for creating Google Document by converting PDF and image files with OCR using Google Apps Script.

Before you run this sample script, please install a GAS library of FetchApp.

function sample() {
  var fileId = "### fileId of PDF file and image files ###";
  var metadata = {
    name: "sampleDocument", // Filename of created Google Document
    mimeType: MimeType.GOOGLE_DOCS // MimeType of Google Document
  var fileBlob = DriveApp.getFileById(fileId).getBlob();
  var form = FetchApp.createFormData(); // Create form data
    Utilities.newBlob(JSON.stringify(metadata), "application/json")
  form.append("file", fileBlob);
  var url =
  var params = {
    method: "POST",
    headers: { Authorization: "Bearer " + ScriptApp.getOAuthToken() },
    body: form
  var res = FetchApp.fetch(url, params);
  // DriveApp.createFile(blob) // This comment line is used for automatically detecting scope for running this sample script.