Installation:
pdfcrowd-php dependency manually).AppKernel.php:
new Amp\PDFCrowdBundle\AmpPDFCrowdBundle(),
config.yml:
amp_pdf_crowd:
username: %env(PDFCROWD_USERNAME)%
apikey: %env(PDFCROWD_APIKEY)%
First Use Case: Convert a URL to PDF in a controller:
use Amp\PDFCrowdBundle\PDFCrowd\PDFCrowd;
public function generatePdfAction()
{
$pdfCrowd = $this->get('amp_pdf_crowd.api');
$url = $this->generateUrl('route_name', [], true);
$pdfData = $pdfCrowd->convertURI($url);
return new Response($pdfData, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="document.pdf"',
]);
}
Key Files:
src/Amp/PDFCrowdBundle/Resources/config/services.yml (Service definitions).src/Amp/PDFCrowdBundle/PDFCrowd/PDFCrowd.php (Core API wrapper).URL-to-PDF Conversion:
$pdfCrowd = $this->get('amp_pdf_crowd.api');
$pdfData = $pdfCrowd->convertURI('https://example.com');
file_put_contents('path/to/file.pdf', $pdfData);
HTML-to-PDF Conversion:
$html = '<h1>Hello World</h1>';
$pdfData = $pdfCrowd->convertHTML($html);
Streaming PDFs:
$response = new Response($pdfData, 200, [
'Content-Type' => 'application/pdf',
]);
return $response;
CLI Conversion (via Symfony Command):
php bin/console pdfcrowd:convert https://example.com public/pdfs/output.pdf
.env for credentials (e.g., PDFCROWD_USERNAME).try {
$pdfData = $pdfCrowd->convertURI($url);
} catch (\Exception $e) {
$this->addFlash('error', 'PDF generation failed: ' . $e->getMessage());
return $this->redirectToRoute('home');
}
$cache = $this->get('cache.app');
$cacheKey = 'pdf_' . md5($url);
if (!$cache->has($cacheKey)) {
$pdfData = $pdfCrowd->convertURI($url);
$cache->set($cacheKey, $pdfData, 3600); // Cache for 1 hour
} else {
$pdfData = $cache->get($cacheKey);
}
EventDispatcher to queue PDF jobs (e.g., for invoices).Deprecated Symfony 2.x:
config/services.yml or use a compatibility layer like symfony/symfony-bridge.Custom pdfcrowd-php Dependency:
pdfcrowd-php via Composer’s repositories (as shown in the README).composer.json to automate downloads:
"scripts": {
"post-install-cmd": [
"php vendor/bin/pdfcrowd-downloader"
]
}
File Permissions:
chmod -R 775 var/).API Rate Limits:
PDFCrowdException for throttling:
catch (\Amp\PDFCrowdBundle\PDFCrowd\PDFCrowdException $e) {
if ($e->getCode() === 429) {
// Retry or notify admin
}
}
Deprecated Methods:
convertURI vs. newer SDK methods).PDFCrowd class to wrap the latest SDK:
class ExtendedPDFCrowd extends \Amp\PDFCrowdBundle\PDFCrowd\PDFCrowd {
public function convertUrl(string $url) {
return $this->client->convertUrl($url);
}
}
Enable Debug Mode:
PDFCROWD_DEBUG: true in .env to log API responses:
amp_pdf_crowd:
debug: %env(bool:PDFCROWD_DEBUG)%
Log API Errors:
PDFCrowd class to log exceptions:
public function convertURI($uri) {
try {
return parent::convertURI($uri);
} catch (\Exception $e) {
$this->logger->error('PDFCrowd error: ' . $e->getMessage());
throw $e;
}
}
Test Locally:
Custom PDF Options:
$pdfCrowd->setOption('margin-top', '20mm');
$pdfData = $pdfCrowd->convertURI($url);
Event Listeners:
pdfcrowd.pre_convert):
// In services.yml
amp_pdf_crowd.api:
class: Amp\PDFCrowdBundle\PDFCrowd\PDFCrowd
calls:
- [setEventDispatcher, ['@event_dispatcher']]
Storage Adapters:
file_put_contents with a custom storage service (e.g., S3):
$storage = $this->get('storage_service');
$storage->save('pdfs/example.pdf', $pdfData);
Fallback Mechanisms:
try {
$pdfData = $pdfCrowd->convertURI($url);
} catch (\Exception $e) {
$pdfData = $this->fallbackConverter->convert($url);
}
How can I help you explore Laravel packages today?