CoreyG
CoreyG

Reputation: 51

Converting PDF to Google Docs

I managed to get a script running where the script automatically converts PDFs to a Google Doc format. The issue that we seem to be running into is that the PDFs have images in them as well. When we convert the PDF to Google Doc, the Google Doc does not have the images and only has the text. I believe the reason why this is happening is due to OCR. Is it possible that I could automate the script to convert the images on the PDF as well to Google Docs?

Here is the Script in question:

GmailToDrive('0BxwJdbZfrRZQUmhldGQ0b3FDTjA', '"Test Email"');

function GmailToDrive(folderID, gmailSubject){
   var threads = GmailApp.search('subject: ' + gmailSubject + ' -label: Imported'); // performs Gmail query for email threads

   for (var i in threads){
  var messages = threads[i].getMessages(); // finds all messages of threads returned by the query

  for(var j in messages){
     var attachments = messages[j].getAttachments(); // finds all attachments of found messages
     var timestamp = messages[j].getDate(); // receives timestamp of each found message
     var date = Utilities.formatDate(timestamp, "MST", "yyyy-MM-dd"); // rearranges the returned timestamp

     for(var k in attachments){
        var fileType = attachments[k].getContentType();
        Logger.log(fileType);
        if (fileType = 'application/pdf') {     // if the application is a pdf then it will convert to a google doc.
         var fileBlob = attachments[k].copyBlob().setContentType('application/pdf');
         var resource = {
           title: fileBlob.getName(),
           mimeType: fileBlob.getContentType()
         }; 
         var options = {
           ocr: true 
         };
         var docFile = Drive.Files.insert(resource, fileBlob, options);  
        }
      }
    }
  }
}

Upvotes: 4

Views: 728

Answers (1)

Milk
Milk

Reputation: 2655

The ocr option is intended to read characters out of images and PDF documents. This will not include the images in the uploaded result.

Have a look at the convert option instead.

The API documentation provides a test on the right hand side which you can quickly check each parameter.

Upvotes: 1

Related Questions