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?