3

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?

sharani97
  • 53
  • 7
  • There is a bug causing PDF blobs to not be correctly converted from HTML. Google has an Issue Tracker for bugs. By visiting the Issue Tracker and clicking the star you can let Google know that it's a problem that needs to be fixed. Please click the "Star" for the issue at: [https://issuetracker.google.com/issues/196100674](https://issuetracker.google.com/issues/196100674) – Alan Wells Aug 13 '21 at 03:08

2 Answers2

2

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

Rilves
  • 146
  • 7
  • Thank you for your answer! after trying your solution this is what I get: `Exception: The parameters (UrlFetchApp.HTTPResponse,String) don't match the method signature for Utilities.newBlob.` I'm not sure about what that means and I'm not sure if we are heading in the right direction :( – sharani97 Aug 12 '21 at 10:14
  • Oh my bad, got confused with 'response' type. I edited my answer, but sadly didn't find a working solution yet – Rilves Aug 12 '21 at 11:46
  • There is a bug causing PDF blobs to not be correctly converted from HTML. Google has an Issue Tracker for bugs. By visiting the Issue Tracker and clicking the star you can let Google know that it's a problem that needs to be fixed. Please click the "Star" for the issue at: [https://issuetracker.google.com/issues/196100674](https://issuetracker.google.com/issues/196100674) – Alan Wells Aug 13 '21 at 03:07
2

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.

  • thank you for this information. I will try to update this question as soon as something changes for me :) – sharani97 Aug 12 '21 at 14:01