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?
To create an HTML file in Google Apps Script, open the Script Editor and choose File > New > Html File. This will create a new file in your project, which starts out with just the following code: In this HTML file, we can write standard HTML.
Get Google Document as HTML 3 Inject HTML into Google Docs using Google Apps Script Related 1 Using OAuth 2.0 within a Google Doc container bound script 3 Inject HTML into Google Docs using Google Apps Script 0 Use Google Apps Script to Print Datastudio 0 Embed image uploaded by form to google doc using GAS 1
The basics. Apps Script can interact with Google Docs in two broad ways: any script can create or modify a document if the script's user has appropriate permissions for the document, and a script can also be bound to a document, which gives the script special abilities to alter the user interface or respond when the document is opened.
After the script is deployed as a web app, you can also choose to embed it in a Google Site. The HTML service can display a dialog or sidebar in Google Docs, Sheets, Slides, or Forms if your script is container-bound to the file.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With