Puppeter 的 page.pdf API 中的页眉和页脚打印是如何工作的? [英] How does header and footer printing work in Puppeter's page.pdf API?

查看:52
本文介绍了Puppeter 的 page.pdf API 中的页眉和页脚打印是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试将 headerTemplatefooterTemplate 选项与 page.pdf 一起使用时,我发现了一些不一致:

I've noticed a few inconsistencies when trying to use the headerTemplate and footerTemplate options with page.pdf:

  • 页眉和页脚的 DPI 似乎较低(我认为主体为 72 对 96).因此,如果我想匹配边距,则必须按比例进行缩放.
  • 样式不与主体共享,因此我必须将它们包含在模板中.
  • 如果我尝试使用本地存储的字体,即使我在页眉/页脚模板中包含相同的 CSS,它也适用于主体但不适用于页眉/页脚.

我怀疑这是因为页眉和页脚被视为单独的文档并单独转换为图像/pdf (https://cs.chromium.org/chromium/src/components/printing/resources/print_header_footer_template_page.html 也暗示了类似的东西).熟悉实现的人可以解释它的实际工作原理吗?谢谢!

I suspect that this happens because headers and footers are treated as separate documents and converted to image/pdf separately (https://cs.chromium.org/chromium/src/components/printing/resources/print_header_footer_template_page.html also implies something like that). Can someone familiar with the implementation explain how it actually works? Thanks!

推荐答案

感谢其他答案 (https://stackoverflow.com/a/51460641/364131) 和代码搜索,我想我找到了我想要的大部分答案.

Thanks to the other answer (https://stackoverflow.com/a/51460641/364131) and codesearch, I think I found most of the answers I was looking for.

打印实现位于 PrintPageInternal.它使用两个独立的 WebFrame —— 一个渲染内容,一个渲染页眉和页脚.页眉和页脚的渲染是通过创建一个 特殊框架,写入print_header_and_footer_template_page.html 到此框架,使用提供的选项调用 setup 函数,然后打印到共享画布.此后,页面的其余内容将打印在由边距定义的边界内的同一画布上.

The printing implementation is in PrintPageInternal. It uses two separate WebFrames — one to render the content, and one to render the header and footer. The rendering for the header and footer is done by creating a special frame, writing the contents of print_header_and_footer_template_page.html to this frame, calling the setup function with the options provided and then printing to a shared canvas. After this, the rest of the contents of the page are printed on the same canvas within the bounds defined by the margins.

页眉和页脚由 fudge_factor 不适用于其余内容.DPI 可能会发生一些有趣的事情(这可以解释 1.33333333f 的 fudge_factor,它等于 96/72).

Headers and footers are scaled by a fudge_factor which isn't applied to the rest of the content. There might be something funny going on here with the DPIs (which might explain the fudge_factor of 1.33333333f which is equal to 96/72).

我猜这个特殊的框架是阻止页眉和页脚与页面内容共享相同资源(样式、字体等)的原因.它可能没有设置为加载(并等待)页眉和页脚模板请求的任何其他资源,这就是请求的字体不加载的原因.

I'm guessing this special frame is what prevents the header and footer from sharing the same resources (styles, fonts etc.) as the contents of the page. It probably isn't setup to load (and wait for) any additional resources requested by the header and footer templates, which is why the requested fonts don't load.

这篇关于Puppeter 的 page.pdf API 中的页眉和页脚打印是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆