Created
October 1, 2018 07:08
-
-
Save dccampbell/8ea06424d91c1f1f984796c8893421b4 to your computer and use it in GitHub Desktop.
Minimal PHP example of printing HTML to PDF using Headless Chrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$chromeExec = 'google-chrome'; | |
$inputFile = __DIR__.'/input.html'; | |
$outputFile = __DIR__.'/output.pdf'; | |
$version = shell_exec($chromeExec . ' --version 2>&1'); | |
if(!strpos($version, 'Google Chrome') === 0) { | |
throw new Exception('Google Chrome not found at: '.$chromeExec); | |
} | |
$html = file_get_contents($inputFile); | |
$url = 'data:text/html,' . rawurlencode($html); | |
$command = sprintf( | |
'%s --headless --disable-gpu --print-to-pdf=%s %s 2>&1', $chromeExec, escapeshellarg($outputFile), escapeshellarg($url) | |
); | |
exec($command); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install Chrome on CentOS: