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:
The additional flag --print-to-pdf-no-header
worked for me.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With