2

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!

Capricorn
  • 2,061
  • 5
  • 24
  • 31
Ado
  • 21
  • 3
  • Do you get the same behavior after enabling the Drive v2 "advanced service" and creating the document with it, instead of `DriveApp`? – tehhowch Jul 18 '18 at 10:28
  • Thank you tehhowch for the suggestion! I enabled Drive v2 and changed the code as per https://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 Jul 18 '18 at 12:51
  • As described above, sadly it does not solve the problem . – Ado Jul 18 '18 at 12:56
  • 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 Jul 18 '18 at 13:01
  • 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 https://productforums.google.com/forum/#!topic/apps/-chhjT8yjKY;context-place=forum/apps – Ado Aug 23 '18 at 07:33

3 Answers3

1

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.

Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • Hi. Thanks for this answer. I was trying to use it for a problem but for some reason DriveApp is generating a corrupt PDF. Maybe it is just because I am not very familiar with HTML. But I was trying the following function and the output PDF is corrupted: `var html = '

    Test

    '; var pdf = Utilities.newBlob(html, 'application/pdf'); pdf.setName('test.pdf'); DriveApp.createFile(pdf).setName("test.pdf");`
    – Gabriel Landi Jun 20 '20 at 16:22
  • I might be mistaken, but this seems to not actually *convert* the HTML into a PDF, but take the raw HTML and interpret it as a PDF. For example, when I convert a simple HTML document into a PDF using this answer and look at the hexdump of the PDF, it's not a PDF document, but rather a HTML document with a PDF extension. – Loovjo Aug 12 '21 at 08:06
  • 1
    There is a Google bug which has been causing this code to fail since 8/9/2021. I don't know if the problem is related to your findings. Many people are experiencing code failures when creating a PDF file. I'm trying to find a solution but haven't had any luck yet. – Alan Wells Aug 12 '21 at 23:56
  • Backing up @AlanWells 's comment: there is an open issue for it on the issuetracker: https://issuetracker.google.com/issues/196210913 (note the linked duplicate issues as well) – derekantrican Aug 16 '21 at 00:25
0

I had the same problem (that everybody else started getting Aug 9 2021) commencing Friday Aug 13th (because I run weekly processes/triggers).

Here is the WORKING solution by muhdtam...@gmail.com that I got from the Google Apps Script Community:

You have to add the following authorization to your "headers" section that is pointed to in your "options" statement that is part of...

var blob = UrlFetchApp.fetch(fileUrl, options).getBlob();

"authorization": "Bearer " + ScriptApp.getOAuthToken()

After I did that and re-tested, the error "Exception: Conversion from text/html to application/pdf failed" went away.

Google must have changed something but never told anybody.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
-1

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

I.D.M
  • 1
  • 1
    This is not an answer. It might be a useful comment. You might want to read https://stackoverflow.com/help/how-to-answer – zhon Aug 31 '20 at 06:44