Installation (Legacy Context)
Since this bundle targets Symfony 2.3, ensure your project aligns with its constraints. Add the bundle to composer.json:
composer require amenophis/prince-bundle
Register the bundle in app/AppKernel.php:
new Amenophis\PrinceBundle\AmenophisPrinceBundle(),
Configuration
Configure the bundle in app/config/config.yml:
amenophis_prince:
binary_path: /path/to/prince/bin/prince # Absolute path to Prince binary
timeout: 30 # Timeout in seconds
First Use Case: PDF Generation
Inject the PrinceService into a controller/service and generate a PDF from HTML:
use Amenophis\PrinceBundle\Service\PrinceService;
class PdfController extends Controller
{
public function generatePdfAction()
{
$html = $this->renderView('path/to/template.html.twig');
$pdf = $this->get('amenophis_prince')->generate($html, 'output.pdf');
return new BinaryFileResponse($pdf);
}
}
HTML-to-PDF Conversion
Use the PrinceService to convert HTML (from Twig, strings, or files) to PDF:
$service = $this->get('amenophis_prince');
$pdf = $service->generate($htmlContent, 'output.pdf');
Dynamic File Handling Stream PDFs directly to the browser or save them to disk:
// Stream to browser
$response = new BinaryFileResponse($pdf);
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_INLINE,
'report.pdf'
);
return $response;
// Save to disk
file_put_contents('path/to/output.pdf', $pdf);
Twig Integration Pass HTML templates to the service:
{% extends 'base.html.twig' %}
{% block content %}
<h1>Dynamic Content</h1>
{{ someVariable }}
{% endblock %}
$html = $this->renderView('template.html.twig', ['someVariable' => 'value']);
$pdf = $service->generate($html, 'output.pdf');
Batch Processing Loop through HTML strings/arrays and generate multiple PDFs:
$htmls = ['<h1>Doc 1</h1>', '<h1>Doc 2</h1>'];
foreach ($htmls as $index => $html) {
$service->generate($html, "doc_{$index}.pdf");
}
Deprecated Bundle
spatie/pdf or dompdf for Laravel.Prince Binary Dependencies
binary_path in config.yml must point to the Prince executable. Test with:
/path/to/prince/bin/prince --version
www-data) has execute permissions on the binary.Memory/Timeout Issues
memory_limit or Prince’s timeout. Adjust:
amenophis_prince:
timeout: 60 # Increase if needed
; php.ini
memory_limit = 512M
Output Handling
generate() method returns raw binary data. Always wrap in BinaryFileResponse for HTTP responses.# app/config/config_dev.yml
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
echo "<h1>Test</h1>" | /path/to/prince/bin/prince -o test.pdf
Custom CSS/JS Inject Prince-specific stylesheets or scripts into your HTML:
<link rel="stylesheet" href="{{ asset('css/prince-styles.css') }}">
<script type="text/prince">
@page { size: A4; }
</script>
Pre/Post-Processing
Extend the service by overriding the generate() method in a custom class:
class CustomPrinceService extends \Amenophis\PrinceBundle\Service\PrinceService
{
public function generate($html, $outputFile)
{
$html = $this->preProcessHtml($html); // Add headers, etc.
return parent::generate($html, $outputFile);
}
}
Register the service in services.yml:
services:
amenophis_prince:
class: AppBundle\Service\CustomPrinceService
arguments: ["@service_container"]
Fallback Mechanisms Implement a fallback to a simpler library (e.g., Dompdf) if Prince fails:
try {
$pdf = $service->generate($html, 'output.pdf');
} catch (\Exception $e) {
$pdf = $this->fallbackToDomPdf($html);
}
Since this bundle is Symfony-specific, Laravel developers should consider:
How can I help you explore Laravel packages today?