Reputation: 827
I'm working on a project. I'm trying to generate a report with my charts and dynamic tables. I tried to used wkhtmltopdf but I wasn't able to deal with the complexity. However, I read most of the answered to other question on wkhtmltopdf so I decided to try snappy which was a sugeestion. below is my code.
require_once 'C:/wamp/www/Mamca/lib/snappy/src/autoload.php';
use Knp\Snappy\Pdf;
$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
echo $snappy->getOutput('http://www.github.com');
but the pdf generated does not open, error from the pdf reader states "cannot open file"
Upvotes: 2
Views: 2389
Reputation: 21
There is no need for working with snappy to convert html to pdf. After spending one day on snappy, i used simple exec() function of php to convert html into pdf.
$exe_path = '..path to your exe file..\wkhtmltopdf.exe';
$file_html_path = '..path of your html file..\report.html';
$file_pdf_path = '..path of your pdf file..\report.pdf';
$cmd = <<<EOT
"$exe_path" --orientation "Landscape" --page-size "A4" --viewport-size
"1280x1024" "$file_html_path" "$file_pdf_path" 2>&1
EOT;
//echo $cmd;die;
exec($cmd,$output,$return);
Please note here:
$cmd is a command
$output will return response of exec execute successfully or return error code and message
$return will return a boolean value 0 or 1 zero means success or 1 means error
Upvotes: 2
Reputation: 827
require_once 'C:/wamp/www/Mamca/lib/snappy/src/autoload.php';
use Knp\Snappy\Pdf;
**$snappy = new Pdf('C://DEV/wkhtmltopdf/bin/wkhtmltopdf.exe');**
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
echo $snappy->getOutput('http://www.github.com');
I just changed the path of the wkhtmltopdf and it worked.
Upvotes: 1