8
votes

We work using Google Drive, Docs, etc.

Background of the problem: One of our departments needs to print hundreds of emails to file away, with page information included. We currently have a Google web app that takes the contents of an gmail thread, saves the messages as an HTML blob, and then exports these to a folder as a PDF. Our users then download these PDFs to the hard drive, and mass-print them.

The problem itself: our users need to have page information on these PDFs when they are being printed. To do this, we're having the app save emails as a Google Doc first, adding footers with the relevant info, then convert to PDFs afterwards. However, what we're trying isn't working, and now we're just throwing stuff at the wall to see what will stick.

This is what we have currently working (allMessages is a String containing the HTML content of an email thread):

//save the HTML content to a PDF file.
var htmlBodyFile = myFolder.createFile("fileName.html", allMessages, "text/html");
var pdfBlob = htmlBodyFile.getAs("application/pdf");

We tried this (and failed):

//save the HTML content to a Google Doc. getAs() needs the MimeType.

var htmlBodyFile = myFolder.createFile("fileName.html", allMessages, "text/html");
var myDoc = htmlBodyFile.getAs("GOOGLE_DOCS");
// application/GOOGLE_DOCS, and application/vnd.google-apps.document don't work either

I noticed that I could use the UI to right-click the HTML file in Drive and open with Docs. It would create a new Google Doc and render the HTML properly. When I tried to script it (and failed),

var htmlBodyFile = myFolder.createFile("filename.html", allMessages, "text/html");
var myDoc = DocumentApp.openById(htmlBodyFile.getId());

it give me "Document is missing (perhaps it was deleted?)"

The following will create a doc, but doesn't actually render the HTML.

var myDoc = DocumentApp.create("docname");   
var myBody = myDoc.getBody();
myBody.setText(allMessages);
myDoc.saveAndClose();

Now I'm about out of ideas.

We tried looking into other solutions involving Adobe. Unfortunately, Acrobat is expensive and prints the docs in random order (and doesn't allow command-line printing so we can script something). And Reader doesn't have the ability to add the page information we need. So it seems our only choice is to insert that info here, when saving in Google Drive.

Does anyone know how to render HTML to a Google Doc using app script?

1
Do you really need the html step? Why not just add the text to a document and then convert that to pdf.SpiderPig
We need to keep the emails intact, including any inline images. The emails themselves convert to html cleanly as well. If we can go straight to doc without losing anything, it's worth a try.A Kirby
In this thread stackoverflow.com/questions/14663852/… someone posted some code to convert a document to html. So you could create docs that contain only the footer and nothing else, convert those to html and then combine them with the html from the mails. Alternatively you could create the html code for the footers directly.SpiderPig
I'm not sure I follow. The purpose of the footer is so that we'll have a page number at the bottom of the page, and HTML doesn't have a concept of "pages" as far as I know. If App Script could modify the PDF directly we'd be all set. But it only appears to be able to create them, no change them afterwards.A Kirby
not exactly related to your problem, but why do you need to "One of our departments needs to print hundreds of emails to file away, with page information included."? Seems like such an environmental waste, not to mention a waste of time when you need to actually look and search these 100s of emails. What's the problem in keeping them in an email account? For example, if you forward them all to gmail, all the email structure remains intact. you can search and retrieve and print whenever you want. and you can use google takeout anytime you want to get a downloadable copy of all email data.Sujay Phadke

1 Answers

7
votes

I have a solution for this. This line of code will convert any file to an appropriate google document. e.g. if it's a csv file it will be turned into a spreadsheet and if it's html it will be converted to a google document.

var newFileId = Drive.Files.copy({title: newFileName}, oldFileId, {convert: true}).id;

Alternatively you can convert a blob containing html directly to a document.

var newFileId = Drive.Files.insert({title: newFileName}, blob, {convert: true}).id;

For this to work you first have to turn on the advanced Drive API as explained here

The entire advanced drive API is explained here but that documentation is for the REST version of the API so you have to somehow figure out how to translate that to apps script using the explanation on this site under "How method signatures are determined" and the examples here. The auto complete feature in the apps script editor also helps with this.