I'm in the process of generating a PDF from an HTML page using google-apps-script and need to set the page size to "Letter" instead of "A4" (8.26 x 11.69). While I have managed to tackle background colors, images, and more based on various resources including Creating PDF from HTML background color in the PDF, the challenge lies in adjusting the page size.
function htmlToPDF() {
var html = "<h1>Hello world</h1>"
+ "<p>This is just a paragraph"
+ "<p style='color:red;'>I am red</p>"
+ "<p style='color:blue;'>I am blue</p>"
+ "<p style='font-size:50px;'>I am big</p>"
+ "<table style='border-collapse: collapse; width: 698px; height: 115px; background-color: #C5D9F1;' border='0' cellpadding='10'>"
+ "<tbody>"
+ "<tr>"
+ "<td style='padding: 5px;background-color:powderblue;' nowrap='nowrap'><strong>Bold with background-color:</strong></td>"
+ "</tr>"
+ "</tbody>"
+ "</table>";
var blob = Utilities.newBlob(html, "text/html", "text.html");
var pdf = blob.getAs("application/pdf");
DriveApp.createFile(pdf).setName("text.pdf");
MailApp.sendEmail("[your email here ]", "PDF File", "",
{htmlBody: html, attachments: pdf});
}
Several methods have been attempted to modify the size
1) Including the following snippet adjusts the margin to zero
<style> @page { margin: 0; }
However, specifying Width & Height like so, does not produce any changes.
@page { margin: 0; width: 216mm; height: 279mm }
and/or
@media print { margin: 0; width: 600px; height: 800px }
2) Even trying to alter the Width & Height during the creation of the HTML Output as shown below
var h = HtmlService.createHtmlOutput(myHTML);
h.setWidth(2550);
h.setHeight(3300);
Thank you...