5

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?

Ilya Chichak
  • 53
  • 1
  • 4

3 Answers3

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();
})();
raymondboswel
  • 576
  • 4
  • 12
0

This works for me:

@media print {
  @page { margin: 0; }
  body { margin: 1.6cm; }
 }
raymondboswel
  • 576
  • 4
  • 12
  • No, this will not solve problem. If document is 1 page, it's ok, but if there are more pages, it will help for top of first page and bottom of last. – Ilya Chichak Apr 18 '18 at 10:27
  • That is indeed true :( If you don't mind adding another step to the process, you can generate each page as a pdf, then concatenate them using pdftk, e.g >pdftk file1.pdf file2.pdf cat output mergedfile.pdf – raymondboswel Apr 19 '18 at 05:32
  • @raymondboswel How can you generate every page separately? – Gimbo Jun 24 '18 at 17:45
  • @GiorgioBorgonovo I have a service to which I send the html document I want to print. Each page is a div with the appropriate CSS classes (in my case constraining the page height to A4 dimensions etc). The html is then printed using puppeteer as described in my other answer. So to generate each page seperately I would just create a request to my puppeteer service for each page. – raymondboswel Jun 25 '18 at 07:51
0

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;
}
  • Try to offer some more explanation as to why it works, site and/or link to the documentation to support your claim. – Espen Oct 28 '19 at 18:51