Convert HTML to PDF not working anymore

1,426 views
Skip to first unread message

Nicolas VEGA MAMANI

unread,
Aug 11, 2021, 10:58:53 AM8/11/21
to Google Apps Script Community
Hi!

I have been using this code to convert my HTML code to PDF files in Google App Script, but since yesterday is not working...

function htmlToPDF() {

var html = "<h1>Hello world</h1>"
var blob = Utilities.newBlob(html, "text/html", "text.html");
var pdf = blob.getAs(MimeType.PDF)
Logger.log(pdf)
DriveApp.createFile(pdf).setName("text.pdf");

MailApp.sendEmail("ndv...@uc.cl", "PDF File", "",
{htmlBody: html, attachments: pdf});

}

The error that I get is: "Exception: Conversion from text/html to application/pdf failed."

Bennett, Scott

unread,
Aug 11, 2021, 11:38:45 AM8/11/21
to google-apps-sc...@googlegroups.com
I had that happen 3 days ago.  After a few minutes it started working again. I was getting a google sheet though.

--
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/a17b34c9-8470-400f-941d-ca8a5a709096n%40googlegroups.com.




Alan Wells

unread,
Aug 11, 2021, 12:01:09 PM8/11/21
to Google Apps Script Community
I have also had errors from code that is creating PDF files.

CBMServices Web

unread,
Aug 11, 2021, 12:43:09 PM8/11/21
to google-apps-sc...@googlegroups.com
Same here. It broke on Monday morning for me in the automatic report generation code I have but by mid morning when I reran it manually, all was working again.

This was probably a glitch in Google code.

mtmcgurn

unread,
Aug 11, 2021, 7:20:03 PM8/11/21
to Google Apps Script Community
I'm not getting the same error on multiple domains.  I've been able to reproduce it with a simple script.

function myFunction() {
var html = "<html><head></head><body><h1>Hello World<h1></body></html>";
Logger.log(html);
var blob = Utilities.newBlob(html, MimeType.HTML, 'testFile.html');

var pdf = blob.getAs("application/pdf");
DriveApp.createFile(pdf);
}


Guy Cardwell

unread,
Aug 12, 2021, 11:25:45 AM8/12/21
to Google Apps Script Community
Same problem here, with largely the same symptoms.  We have a google form that emails a PDF file that was rendered from the form inputs back to the submitter.  It's been failing since the 9th, and no amount of re-coding to debugging seems to change anything.  Fails in Apps Script debugger as well as on the form submit event.

Alan Wells

unread,
Aug 12, 2021, 1:57:30 PM8/12/21
to Google Apps Script Community
I may have found a solution.
I'm using the advanced drive service to create files so that I can use the more restricted
" . . . /auth/drive.file"
scope

This code will run without an error and create a PDF file using HTML.

function makeTestPdfFile() {
try{
  var blob,fileObj,html,folderID,resource;
  
  folderID = "";//Put your folder ID here
  
  html = "<html><head></head><body><h1>Hello World<h1></body></html>";
  blob = Utilities.newBlob(html,MimeType.PDF);
  
  resource = {};
  
  resource = {//These settings may be for a specific API version
    title: "AAA_test pdf Delete",
    mimeType: "application/pdf"
  };
  
  if (folderID) {
    resource.parents = [{ id: folderID }];
  }
  
  fileObj = Drive.Files.insert(resource, blob,{ supportsAllDrives: true });
  
  Logger.log(fileObj.createdDate)
}catch(e) {
  Logger.log(e.message)
}
}


Alan Wells

unread,
Aug 12, 2021, 9:03:00 PM8/12/21
to Google Apps Script Community
Please click the "Star" for the issue at:

Alan Wells

unread,
Aug 12, 2021, 10:58:25 PM8/12/21
to Google Apps Script Community
The possible solution that I posted earlier has problems.  The PDF file will open with an error from Drive, and there is a problem attaching it to an email.
The only solution that I can come up with is to save the HTML as an HTML file instead of a PDF file.
Saving the file as an HTML file will avoid errors that were preventing a file from being created.  At least now a file will be created.
The HTML file can be opened in the browser, it can be opened by Google Docs, and it can be attached to an email.
With this new change, at least a file gets created, and the HTML file can be attached to an email.
If the person who gets the email clicks on the attachment, a new browser tab will open and the HTML will be displayed with it formatted the same as the PDF would look.
If you open the HTML file from your Drive, then you'll see all the HTML tags, but you can then open it in a Google Doc and the Google Doc will interpreted the HTML and display the content correctly.
You could then download the Google Doc as a PDF if you wanted to.

Fernando Falcao

unread,
Aug 13, 2021, 8:40:00 AM8/13/21
to google-apps-sc...@googlegroups.com

Muhd Tamrin

unread,
Aug 14, 2021, 8:29:50 PM8/14/21
to Google Apps Script Community
Same problem here.. The solution I choose for now is convert from docs to pdf.

Alan Wells

unread,
Aug 14, 2021, 8:41:57 PM8/14/21
to Google Apps Script Community
Do you have any code that you can share to explain how you convert from Docs to PDF?
What is your process?

Message has been deleted

Muhd Tamrin

unread,
Aug 14, 2021, 9:29:05 PM8/14/21
to Google Apps Script Community
1. Design docs template 
2. Grab data from your source (spreadsheet or html)
3. Store data to the template
4. Convert to pdf.

Code to convert:
var pdf = UrlFetchApp.fetch(url, {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}}).getBlob().setName("test");
DriveApp.createFile(pdf);

Alan Wells

unread,
Aug 14, 2021, 9:55:56 PM8/14/21
to Google Apps Script Community
Thank you.  I had searched for an export URL for Docs but wasn't able to find anything.
I appreciate it.

Reply all
Reply to author
Forward
0 new messages