我正在尝试通过google-apps-script从HTML页面创建PDF,并将页面大小设置为"Letter"而不是"A4"(8.26 x 11.69英寸)。我查看了许多相关帖子,实际上已经弄清了背景颜色、图像等等。可以参考Creating PDF from HTML background color in the PDF,但我无法更改页面大小。
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});
}
我尝试了几种方法来设置大小。
1)添加以下内容将边距调整为零
<style> @page { margin: 0; }
但是按照以下方式添加宽度和高度不起作用。
@page { margin: 0; width: 216mm; height: 279mm }
和/或
@media print { margin: 0; width: 600px; height: 800px }
2)我甚至尝试在创建HTML输出时调整宽度和高度,如下所示:
var h = HtmlService.createHtmlOutput(myHTML);
h.setWidth(2550);
h.setHeight(3300);
谢谢你...