如何隐藏无头铬生成的pdf中的页边距? [英] how to hide margins in headless chrome generated pdf?

查看:121
本文介绍了如何隐藏无头铬生成的pdf中的页边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用无头的chrome来通过Python/Django生成一个长的pdf文档.

I'm using headless chrome to generate a long pdf document with Python/Django.

是否可以从页面中删除带有日期和页脚的带有url和页数的页眉?

Is there a way to remove header with date and footer with url and pages count from pages?

尝试使用

@page{
    margin: 0;
    size: auto;
}

但是有了这个CSS,我没有需要的边距.

but with this css there are no margins, which i need.

试图使用div.wrapper和样式包装页面内容

tried to wrap page content with div.wrapper and style

.wrapper{
    margin: 15mm 10mm 15mm 15mm;
}

,但是使用此解决方案,仅在首页和末页上有上页边距和下页边距.介于两者之间的页面没有垂直边距,并且停留在顶部和底部.

but with this solution there are top and bottom margins only on first and last pages. Pages between are without vertical margins and stick to top and bottom.

在此处阅读: https://cs.chromium.org/铬/src/headless/app/headless_shell_switches.cc 没有任何特殊的标志可在禁用页眉和页脚的情况下启动chrome

read here: https://cs.chromium.org/chromium/src/headless/app/headless_shell_switches.cc there is no any special flag to launch chrome with headers and footers disabled

有什么解决方案可以隐藏页面的页眉和页脚,但可以节省边距?

is there any solution to hide page header and footer, but save margins?

推荐答案

我最终使用的另一种方法是使用puppeteer.我的脚本如下:

An alternate approach that I ended up using, is to use puppeteer. My script is as follows:

const puppeteer = require("puppeteer");

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto("file:///home/<user>/page.html");
  await page.pdf({
    path: "page.pdf",
    format: "A4",
    printBackground: true,
    displayHeaderFooter: false,
    margin: {
      left: "0.35cm"
    }
  });

  await browser.close();
})();

这篇关于如何隐藏无头铬生成的pdf中的页边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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