Installation
composer require dgarden/gotenberg-bundle
Ensure sensiolabs/gotenberg-bundle is also installed (dependency).
Configuration Add the required configs to:
config/packages/dgarden_gotenberg.yaml (output path)config/routes/dgarden_gotenberg.yaml (routes)config/packages/gotenberg.yaml (Gotenberg API connection via sensiolabs/gotenberg-bundle).First Use Case Generate a PDF from HTML in a controller:
use SensioLabs\GotenbergBundle\Client\GotenbergClientInterface;
use SensioLabs\GotenbergBundle\Model\ConvertDocumentRequest;
public function generatePdf(GotenbergClientInterface $client)
{
$request = new ConvertDocumentRequest();
$request->setSource('html', '<h1>Hello, PDF!</h1>');
$response = $client->convert($request);
return new Response($response->getContent(), 200, [
'Content-Type' => 'application/pdf',
]);
}
PDF Generation from HTML
Use ConvertDocumentRequest to convert HTML strings or URLs to PDFs:
$request = new ConvertDocumentRequest();
$request->setSource('html', $htmlContent);
$client->convert($request);
PDF Generation from URLs
$request->setSource('url', 'https://example.com');
Saving PDFs Locally
Leverage dgarden_gotenberg.output_path to auto-save PDFs:
dgarden:
gotenberg:
output_path: '%kernel.project_dir%/var/pdf'
Then use the bundle’s service to handle file storage.
Queueing PDF Jobs Use Symfony’s Messenger component to offload PDF generation:
$message = new GeneratePdfMessage($html, $outputPath);
$bus->dispatch($message);
Custom Metadata
Extend sensiolabs/gotenberg-bundle config for PDF metadata:
sensiolabs_gotenberg:
default_options:
pdf:
html:
metadata:
Author: 'Your App'
Title: 'Dynamic PDF'
/api/pdf).GotenbergClientInterface calls in try-catch blocks for API failures.Missing Dependencies
Ensure sensiolabs/gotenberg-bundle is installed and configured. The dgarden bundle relies on it for core functionality.
Output Path Permissions
If output_path is set, ensure the directory is writable:
mkdir -p var/pdf && chmod -R 775 var/pdf
Gotenberg Service Unavailable The API must be running (Docker setup recommended). Test connectivity:
curl -X POST http://localhost:3000/forms/chromium/convert/url -F 'url=https://example.com'
HTML/URL Size Limits Gotenberg has payload size limits. For large content, stream data or split into chunks.
Caching Responses PDF generation can be slow. Cache responses (e.g., with Symfony’s cache system) for repeated requests.
APP_DEBUG=true) to inspect Gotenberg API responses.GotenbergClient HTTP requests in browser dev tools for errors.docker logs gotenberg
Dynamic Filenames
Use uniqid() or UUIDs to avoid filename collisions in output_path:
$filename = 'pdf_' . uniqid() . '.pdf';
Async Processing For long-running tasks, use Symfony’s Messenger with a queue (e.g., RabbitMQ):
# config/packages/messenger.yaml
framework:
messenger:
transports:
async: '%env(MESSENGER_TRANSPORT_DSN)%'
Custom Templates
Store reusable HTML templates in a templates/pdf/ directory and load them dynamically:
$html = $twig->render('pdf/template.html.twig', ['data' => $data]);
Environment-Specific Config
Override gotenberg.yaml per environment (e.g., local vs. production DSN):
# config/packages/gotenberg_prod.yaml
sensiolabs_gotenberg:
http_client: 'gotenberg.client.prod'
Extension Points
Extend the bundle by creating custom services that wrap GotenbergClientInterface for domain-specific logic (e.g., InvoicePdfGenerator).
How can I help you explore Laravel packages today?