Downloading a webpage as a PDF in Google Drive using Google Apps Script

Viewed 310

I am working on a Google apps script that is supposed to save web pages as pdf files in a folder inside my google drive. Here is my code for now:

function downloadFiles(){
   var fileName ="";
   var fileSize ="";
   var response=UrlFetchApp.fetch("https://fr.wikipedia.org/wiki/Wikip%C3%A9dia:Accueil_principal");

   var rc=response.getResponseCode()
   if(rc==200){
     var fileBlob=response.getAs('application/pdf');
     var folder= DriveApp.getFolderById("ID")// folder where the files to compare will be stored
     if (folder!=null){
       var file=folder.createFile(fileBlob)
       fileName= file.getName()
       fileSize= file.getSize()
     }
   }
   var fileInfo={'rc':rc, "filename":fileName,"filesize":fileSize}
 } 

for some reason, when I run my script I get this error: Exception: Conversion from text/html to application/pdf failed.

I read on some websites that it could be related to a limitation or something like that... Do you know how I can solve this issue?

2 Answers

From what I understand the problem might come from the getAs() that fails to do the conversion.

I tried with the following version

var page = response.getContentText();
var fileBlob = Utilities.newBlob(page,"application/pdf");
fileBlob.setName("name");

And it manages to create a pdf, but the content is corrupted beyond the first page. I'll keep looking

That conversion service seems to be failing in the last 24 hours (I've been trying to fix my script since then). The script was running for 2 years and is failing since yesterday.

Someone else opened this stackoverflow issue: Google App Script convert HTML to PDF not working but it was flagged (wrongly) as duplicate.

My bet: Something wrong on Google App's side, but I'll subscribe to this question just in case.

Related