I have a function in my AppMaker app that works like a charm -- when you click on a button, it takes the data from the current record, applies some logic to it, and mashes it up and styles it as HTML. I want to deliver the results to the user in two ways:
- Print -- the function has a return that sends the HTML back to the browser, which opens up a print dialog as expected.
- PDF by Email -- the function also generates a PDF which is attached to an email sent to the user.
I heard feedback from users that the PDF version cuts off the bottom two lines, so I'm trying to find out why.
- To send the PDF as an email attachment, I use
Utilities.newBlob()
to first blobify my HTML. Then I get the blob as a PDF, and then I send the PDF as an email attachment. The code looks like this:
var pdfContent = Utilities.newBlob(html, 'text/html', 'PE-' + name + '.html')
var pdf = pdfContent.getAs("application/pdf");
GmailApp.sendEmail(to, subject, body, {attachments: pdf});
The email is delivered correctly with a PDF attached to it, and when you open it up, it looks pretty much like the HTML that gets sent to the print dialog. When you go to print it, though, the last lines clearly get cut off. My advice to users (until I could dig into the problem) was to use the "Fit to Page" function in the print dialog. Looking at it more closely, though, I had an inkling that it might be a page size issue -- there's something off about the proportions when you view the PDF. Sure enough, upon changing the paper size from letter to A4 in the print dialog, the page lays out perfectly.
So now I can't figure out where the default to A4 is happening.
- I tried setting the
@page
attribute for size in the<style>
tag, with no effect -- it doesn't seem to survive the transition from HTML to blob to PDF:
html += '<style>@page {size: 215.9mm 279.4mm; margin: 15mm;}</style> etc...';
For size I've used millimeters (as above) and px as units, and I've tried just using the word 'letter'. No version of the above works.
Is there some other place where I can specify page size to make this stop happening?