我正在尝试通过 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);
谢谢...
最佳答案
如 documentation 中所写,
You can't change all CSS properties with @page. You can only change the margins, orphans, widows, and page breaks of the document. Attempts to change any other CSS properties will be ignored.
但是您可以使用高度实验性的 size
属性:
<style>
@page {
margin: 0;
size: letter; /*or width then height 150mm 50mm*/
}
</style>
关于html - 从 HTML 创建 PDF 并设置页面大小和页边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55600931/