Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove timestamp and page URL from PDF created with Headless Chrome

I have a PHP application that saves a webpage as a pdf file by wrapping some headless Chrome directives into a command line statement, and then running that with shell_exec().

    $chrome = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
    $location = "C:\\xampp\\htdocs\\myfolder\\files\\d-{$dispatch_id}.pdf";
    $dispatch = site_url("dispatch/print_dispatch/{$dispatch_id}");
    $params = '--headless --disable-gpu --print-to-pdf="$location"';

    $command = '"'.$chrome.'"';
    $command .= "  --headless --disable-gpu --print-to-pdf=";
    $command .= '"'.$location.'"';
    $command .= ' "'.$dispatch.'"';
    $output = shell_exec($command);

    // echo $command returns:
    //"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --headless --disable-gpu --print-to-pdf="C:\xampp\htdocs\myfolder\files\d-71.pdf" "http://website.com/dispatch/print_dispatch/71"

This works fine for me and does exactly what I need. However, the pdf file that is generated has the date and time in the header and footer, and the page url in the footer as well. I have tried using the --no-margins option to remove this superfluous text, but that didn't work, and my Goggle-Fu has failed me. Is there a way to remove the timestamp and url from a pdf created with Headless Chrome?

I have reviwed the following similar questions but have yet to find an answer:

  • additional options in Chrome headless print-to-pdf
  • how to hide margins in headless chrome generated pdf?
like image 335
Bad Programmer Avatar asked Aug 31 '25 18:08

Bad Programmer


2 Answers

The additional flag --print-to-pdf-no-header worked for me.

like image 195
Andy Stewart Avatar answered Sep 04 '25 07:09

Andy Stewart


Answer was actually found in a different StackOverflow question:

How to remove the URL from the printing page?

<style type="text/css" media="print">
@page {
    size: auto;   /* auto is the initial value */
    margin: 0;  /* this affects the margin in the printer settings */
}
</style>

I tried this originally, but my style tag did not have the media attribute.

like image 21
Bad Programmer Avatar answered Sep 04 '25 07:09

Bad Programmer