Installation:
composer require mikehaertl/phpwkhtmltopdf
Ensure wkhtmltopdf is installed on your system (download from wkhtmltopdf.org).
Basic Usage:
use MikeHaertl\Phpwkhtmltopdf\Pdf;
$pdf = new Pdf();
$pdf->addPage('Hello, World!');
$pdf->saveAs(storage_path('test.pdf'));
First Use Case: Generate a PDF from a Blade template:
$pdf = new Pdf();
$pdf->addPage( view('pdf.template', ['data' => $data])->render() );
$pdf->saveAs(storage_path('invoice.pdf'));
Dynamic PDF Generation:
$pdf = new Pdf();
$pdf->addPageHTML('<h1>Dynamic Content</h1><p>' . $user->name . '</p>');
$pdf->saveAs(storage_path('user_' . $user->id . '.pdf'));
Reusable PDF Service:
class PdfService {
public function generateInvoice($user, $invoiceData) {
$pdf = new Pdf();
$pdf->addPage(view('invoice.pdf', compact('user', 'invoiceData'))->render());
return $pdf->output();
}
}
Streaming PDFs to Browser:
$pdf = new Pdf();
$pdf->addPageHTML('<h1>Download Me</h1>');
$pdf->stream('filename.pdf');
Customizing wkhtmltopdf Options:
$pdf = new Pdf();
$pdf->setOption('orientation', 'Landscape');
$pdf->setOption('margin-top', '20mm');
$pdf->addPageHTML('<h1>Custom Layout</h1>');
$pdf->saveAs(storage_path('custom.pdf'));
Queueing PDF Jobs:
// In your job class
public function handle() {
$pdf = new Pdf();
$pdf->addPageHTML('<h1>Queued PDF</h1>');
$pdf->saveAs(storage_path('queued.pdf'));
}
Binary Path Issues:
wkhtmltopdf is in your system PATH or specify its path explicitly:
$pdf = new Pdf('/usr/local/bin/wkhtmltopdf');
which wkhtmltopdf (Linux/Mac) or check where wkhtmltopdf (Windows).Memory Limits:
memory_limit in php.ini or use chunked processing.Font Handling:
$pdf->addFont('/path/to/font.ttf', 'CustomFont');
$pdf->setOption('font-name', 'CustomFont');
CSS Quirks:
Headless Environments:
wkhtmltopdf is installed in CI/CD pipelines (e.g., Docker containers). Example Dockerfile snippet:
RUN apt-get update && apt-get install -y wkhtmltopdf
wkhtmltopdf commands:
$pdf->setOption('quiet', false);
try {
$pdf->saveAs('file.pdf');
} catch (\MikeHaertl\Phpwkhtmltopdf\Exception\PdfException $e) {
Log::error($e->getMessage());
}
Caching: Cache generated PDFs to avoid reprocessing:
if (!file_exists(storage_path('cached.pdf'))) {
$pdf->saveAs(storage_path('cached.pdf'));
}
Options Reference: Consult wkhtmltopdf options for advanced configurations like headers/footers, page numbers, or custom margins.
Laravel Integration:
Use response()->make() for seamless HTTP responses:
return response()->make($pdf->output(), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="document.pdf"',
]);
Testing:
Mock the Pdf class in tests:
$pdf = Mockery::mock('MikeHaertl\Phpwkhtmltopdf\Pdf');
$pdf->shouldReceive('addPageHTML')->once()->with('Test');
$pdf->shouldReceive('saveAs')->once()->with('test.pdf');
Extensions:
Extend the Pdf class for reusable configurations:
class CustomPdf extends Pdf {
public function __construct() {
parent::__construct();
$this->setOption('margin-top', '10mm');
$this->setOption('margin-bottom', '10mm');
}
}
How can I help you explore Laravel packages today?