how to hide margins in headless chrome generated pdf?

chrome headless print-to-pdf options
puppeteer pdf page-break
headless chrome pdf
selenium headless chrome
puppeteer api
headless browser

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?

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 Gotchas with Headless Chrome, Generating PDF reports is one of those features that every enterprise developer will Printing to a PDF is a supported use case of Chrome's headless mode If you try and place an <img> tag in your header or footer (a pretty  Detecting Headles Chrome. A short article titled Detecting Chrome Headless popped up on Hacker News over the weekend and it has since been making the rounds. Most of the discussion on Hacker News was focused around the author’s somewhat dubious assertion that web scraping is a “malicious task” that belongs in the same category as advertising fraud and hacking websites.

This works for me:

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

Disable Chrome's default headers & footers in headless print-to-pdf , The issue with this is, that Chrome adds auto-generated header and footers to to hide Google Chrome's default headers / footers on headless PDF-export by  The easiest way to get started with headless mode is to open the Chrome binary from the command line. If you've got Chrome 59+ installed, start Chrome with the --headless flag: chrome \ --headless \ # Runs Chrome in headless mode. --disable-gpu \ # Temporarily needed if running on Windows.

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

How can I remove page number when I use --print-to-pdf? · Issue #7 , I have a strange issue with page margins when printing to PDF. And it's most likely Chrome issue. What steps and remove them from @page . Things changed in April 2017 with the release of Google Chrome 59 which included a “headless” mode. In conjunction with Chrome’s “devtools” API, headless mode allows you to use Chrome in a server environment and script it to perform tasks — like creating PDFs! By switching to Chrome for PDF generation, you are able to use all the

Issue with margins when print to PDF · Issue #2592 · puppeteer , Add this CSS to the page your creating into a PDF to remove Chrome Headless's implemented Header and Footer. CSS: @media print { @page { margin: 0; }  In this article I’m going to show how you can generate a PDF document from a heavily styled React page using Node.js, Puppeteer, headless Chrome & Docker. Background: A few months ago one of the clients of RisingStack asked us to develop a feature where the user would be able to request a React page in PDF format.

additional options in Chrome headless print-to-pdf, Since I'm on Mac, I created convenient aliases for each version of Chrome that I chrome --headless --disable-gpu --print-to-pdf https://www.chromestatus.com/  html_to_pdf. Generate a simple PDF invoice from HTML using puppeteer & handlebars. Introduce. Puppeteer; Puppeteer is Node.js library giving you access to a headless Chrome browser. This makes it a breeze to generate PDF files with Node.js. Handlebars; Handlebars provides the power necessary to let you build semantic templates effectively with

Getting Started with Headless Chrome | Web, chrome-headless-render-pdf [OPTIONS] --url=URL --pdf=OUTPUT-FILE for local files use: file:///path/to/file --pdf output for generated file can be relative to set chrome port (for remote process) --no-margins disable default 1cm margins  Now, we are ready to generate the PDF! Luckily Puppeteer provides an amazing API for this. We are going to add some code that tells Chrome to emulate a screen when its generating the PDF, and also a call to page.pdf() to generate the PDF. We'll pass in a couple options to make the PDF look the way we want. Update generate-pdf.js to look like this:

Comments
  • Did you find a solution for this yet?
  • Unfortunately, no:(
  • 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.
  • 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 How can you generate every page separately?
  • @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.
  • Try to offer some more explanation as to why it works, site and/or link to the documentation to support your claim.