Installation
composer require pdfcrowd/pdfcrowd-php
Add your API username and password to .env:
PDFCROWD_USERNAME=your_username
PDFCROWD_PASSWORD=your_password
First Use Case: Generate a PDF from HTML
use Pdfcrowd\Pdfcrowd;
$client = new Pdfcrowd(getenv('PDFCROWD_USERNAME'), getenv('PDFCROWD_PASSWORD'));
$pdf = $client->convertHtml('<h1>Hello, PDF!</h1>');
$pdf->saveAs('output.pdf');
Key Files to Review
vendor/pdfcrowd/pdfcrowd-php/src/Pdfcrowd.php (core class)vendor/pdfcrowd/pdfcrowd-php/README.md (API docs, examples)Dynamic PDF Generation
// Generate PDFs from Blade templates
$html = view('invoices.pdf', ['invoice' => $invoice])->render();
$pdf = $client->convertHtml($html);
Batch Processing
foreach ($users as $user) {
$pdf = $client->convertHtml($user->generateHtml());
$pdf->saveAs("storage/app/user_{$user->id}.pdf");
}
Integration with Laravel Queues
// Job class
public function handle() {
$pdf = $client->convertHtml($this->html);
Storage::put("pdfs/{$this->filename}.pdf", $pdf->getBytes());
}
Custom Headers & Options
$pdf = $client->convertHtml($html, [
'page_size' => 'A4',
'margin_top' => '1cm',
'margin_bottom' => '1cm',
]);
storage/app/pdf to avoid redundant API calls.try-catch blocks for PdfcrowdException.config('pdfcrowd') for credentials in production.API Rate Limits
config/pdfcrowd.php:
'usage_limit' => env('PDFCROWD_USAGE_LIMIT', 1000), // Default: 1000 pages/month
HTML/CSS Quirks
position: fixed, flexbox) may render unpredictably.Memory Limits
memory_limit.memory_limit in php.ini.Authentication Caching
$client->clearCache();
$client = new Pdfcrowd($username, $password, [
'verbose' => true,
'log_file' => storage_path('logs/pdfcrowd.log'),
]);
$pdf->getHttpStatus() for errors (e.g., 403 = invalid credentials).$client->setCustomHeader('X-Custom-Header', 'value');
$client = new Pdfcrowd($username, $password, [
'proxy' => 'http://proxy.example.com:8080',
]);
Pdfcrowd to log conversions:
$client->addListener(function ($event) {
Log::info("PDF Event: {$event->getType()}", $event->getData());
});
How can I help you explore Laravel packages today?