laravel-html-to-pdf maintained by tungltdev
Description
Convert a html string to a pdf string
Author
Last update
2025/05/19 03:58
(dev-master)
License
Downloads
4
Tags
Installation:
$ composer require tungltdev/laravel-html-to-pdf
Features:
- Convert an html string to a pdf string using wkhtmltopdf
- Does not use temporary files
Basic usage:
use \Tungltdev\LARAVEL\HtmlToPdf;
try{
$pdf = new HtmlToPdf('<html><body>Hi!<body></html>', '/usr/local/bin/wkhtmltopdf');
header('Content-Type: application/pdf');
echo $pdf->generate();
}
catch(Exception $e){
echo $e->getMessage();
}
Advanced usage:
use \Tungltdev\LARAVEL\HtmlToPdf;
try{
//Get your html string
$html = file_get_contents('demo.html');
// Path to wkhtmltopdf
$pathToWkhtmltopdf = '/usr/local/bin/wkhtmltopdf';
// Initialise
$pdf = new HtmlToPdf($html, $pathToWkhtmltopdf);
// Add a command before the wkhtmltopdf command
$pdf->addBeforeCommand('unset DYLD_LIBRARY_PATH');
// Add wkhtmltopdf parameters, second parameter is optional. See: http://wkhtmltopdf.org/usage/wkhtmltopdf.txt
$pdf->setParam('grayscale');
$pdf->setParam('orientation', 'landscape');
// Execute
$pdfString = $pdf->generate();
// Show the generated pdf
header('Content-Type: application/pdf');
die($pdfString);
}
catch(Exception $e){
echo $e->getMessage();
}