Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created April 20, 2019 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/b73396314254f7a5bf571af6b65eac07 to your computer and use it in GitHub Desktop.
Save tanaikech/b73396314254f7a5bf571af6b65eac07 to your computer and use it in GitHub Desktop.
Creating Google Document by Converting PDF and Image Files with OCR using Google Apps Script

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
  form.append(
    "metadata",
    Utilities.newBlob(JSON.stringify(metadata), "application/json")
  );
  form.append("file", fileBlob);
  var url =
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart";
  var params = {
    method: "POST",
    headers: { Authorization: "Bearer " + ScriptApp.getOAuthToken() },
    body: form
  };
  var res = FetchApp.fetch(url, params);
  Logger.log(res);
  // DriveApp.createFile(blob) // This comment line is used for automatically detecting scope for running this sample script.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment