how to hide margins in headless chrome generated pdf?

被刻印的时光 ゝ 提交于 2019-12-10 10:04:49

问题


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

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

Tried to use

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

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

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.

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?


回答1:


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();
})();



回答2:


This works for me:

@media print {
  @page { margin: 0; }
  body { margin: 1.6cm; }
 }



回答3:


This works for me very good.

@page {
    margin: 0!important;
    margin-top: 0cm!important;
    margin-bottom: 0cm!important;
    margin-left: 0cm!important;
    margin-right: 0cm!important;
}


来源:https://stackoverflow.com/questions/48966246/how-to-hide-margins-in-headless-chrome-generated-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!