html - 从 HTML 创建 PDF 并设置页面大小和页边距

标签 html css pdf google-apps-script

我正在尝试通过 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/

相关文章:

html - 如何使用 bootstrap 3.2.0 完成以下设计?

javascript - border-radius无法通过使用PIE支持工作?

python - 导航标题没有很好地对齐并且导航栏有点太粗了?

css - 边距被忽略。特异性问题

java - 让 PDF 显示在 HTML 中,而不是使用 Adob​​e 加载或加载到单独的窗口中

javascript - 使用 jQuery 替换背景图像不起作用

css - 如何重复网格行?

javascript - 如何获得滚动和封面效果?

javascript - 制作一个 anchor 标记,点击时触发打印操作

c++ - 用于 C/C++ 应用程序的开源 PDF 库?