Error when converting Google Doc to PDF with more than 150 pages.

160 views
Skip to first unread message

Fernando Falcao

unread,
Feb 15, 2021, 2:27:46 PM2/15/21
to Google Apps Script Community
Hello everyone!

I am facing an unexpected result when I try to convert a Google Doc to PDF. I already noticed that the error occur when the file has more than 150 pages.

Even if I open the file in Google Doc I can't convert using the path File >> Download As >> PDF. For every other format I am succeeded in convert.

Here are the two functions that I am using, but with the same error result:

function convertToPDF_v1(prmDocID) {

 var  doc = DocumentApp.openById(prmDocID)    

  var  nomeDoc = doc.getName();
  var pdf = doc.getAs('application/pdf').setName(nomeDoc + ".pdf");
  var arqPDF = DriveApp.createFile(pdf);

  var pdfID = arqPDF.getId();
  return pdfID;
}

function ConvertToPDF_v2(fileID)
{
   const FileName = DriveApp.getFileById(fileID).getName();

   var blob = UrlFetchApp.fetch(url, {headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`}}).getBlob();

   var newFile = DriveApp.createFile(blob.setName(FileName));
   
  return newFile.getId();
}

Any clue and guidance to overcome this error I will really appreciate!!

Thanks,
Fernando

Martin Hawksey

unread,
Feb 15, 2021, 3:25:25 PM2/15/21
to Google Apps Script Community
Hi,

I suspect if you can't generate the PDF as a download from the UI a script is going to encounter the same issue. Interestingly the built-in pdf printer in most bowsers sounds like it can handle higher volumes https://webapps.stackexchange.com/a/91223/30021 

Not an apps script solution but looking into running a headless chrome browser to solve this I came across this writeup that does it in a Cloud Function https://www.ribice.ba/cloud-function-html-to-pdf/ ... you could then trigger the Could Function from apps script?

Martin

Fernando Falcao

unread,
Feb 21, 2021, 8:58:22 PM2/21/21
to google-apps-sc...@googlegroups.com
Hi Martin,

Thanks for your comments.
I realized that I hadn't answered your comments.
Yes, indeed. I was surprised that the routine which exported as PDF didn't work regularly. I mention "regularly" because with some big files I succeeded. At the end of my conversion process of 1700 google doc format to PDF, I had 60 files that had to be converted manually to PDF.

Regarding cloud function, this is a topic that I still have to learn better. I know the concept but I still don't know how to use with google script.

Best regards,
Fernando



--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/5d6994a1-0518-4ca9-b160-ef29ab973225n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages