3

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:

Bad Programmer
  • 893
  • 1
  • 13
  • 27

2 Answers2

4

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

Andy Stewart
  • 5,013
  • 2
  • 28
  • 38
2

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.

Bad Programmer
  • 893
  • 1
  • 13
  • 27
  • 2
    Tested April 2020. This seems to work for one-page docs. But multi-page docs end up with no top/bottom margin, which looks odd and is not good for printing. – Daniel Winterstein Apr 08 '20 at 19:10