1
votes

I have a custom Google Script with a simple logic: it looks up parts from the spreadsheet and builds an in-memory HTML string, then I call the following code to produce the PDF.

var blob = Utilities.newBlob(html_body, 'text/html').getAs('application/pdf').setName(pdf_name);

var newDoc = DriveApp.createFile(blob);

var pdf_url = newDoc.getUrl();

Surprisingly for the same content, it sometimes works and randomly returns the error:

Conversion from text/html to application/pdf failed.

I see this happening most for larger files, so I am probably hitting some quota limitations.

But as per https://developers.google.com/apps-script/guides/services/quotas#current_limitations hitting any limitations should raise a relevant message.

Also documentation around https://developers.google.com/apps-script/reference/base/blob#getAs(String) does not state anything.

Any ideas?

Thank you!

2
Do you get the same behavior after enabling the Drive v2 "advanced service" and creating the document with it, instead of DriveApp? - tehhowch
Thank you tehhowch for the suggestion! I enabled Drive v2 and changed the code as per developers.google.com/apps-script/advanced/drive resulting in the folowing code. Sadly, the prolem still persists. The first line from the code generates the error. var blob = Utilities.newBlob(html_body, 'text/html').getAs('application/pdf').setName(fileName); var file = { title: fileName, mimeType: 'application/pdf', }; newDoc = Drive.Files.insert(file, blob); var pdf_url = newDoc.id; - Ado
As described above, sadly it does not solve the problem . - Ado
You can/should provide examples of the sizes which cause this issue in the OP, and statistics on frequency of file creation failures. Relevant links to test payloads can help others reproduce your exact issues. - tehhowch
Just as update, I have tried also using the Early Access Features from GSuite Business: Flexible Quotas and Extended Script Execution time and that does not help either - see productforums.google.com/forum/#!topic/apps/… - Ado

2 Answers

2
votes

I had the same problem, and did the following:

var html = HtmlService.createHtmlOutput(html_body);//This is now an object
html = html.getContent();//Get data as string - So its not an object or file
var blob = Utilities.newBlob(html,'application/pdf');//Create pdf from string

blob.setName(pdf_name);

In my case, the getAs('application/pdf') method was failing to convert a blob from a text file to a pdf file.

0
votes

I`m facing more or less the same trouble "text/html to application/pdf failed" error message. As far as I understood, the issue is coming from the migration of the new version "This project is running on our new Apps Script runtime powered by Chrome V8.", on existing script which was converting snapshot of only one tab from google sheet to PDF.

https://pastebin.pl/view/8ebb6742