sensiolabs/gotenberg-bundle
Symfony bundle to generate PDFs and screenshots via the Gotenberg API. Convert from URL, HTML, Markdown, or Office files, then stream or save outputs locally. Supports source-specific options, advanced usage, and profiler/testing integrations.
docker run -d --name gotenberg -p 3000:3000 gotenberg/gotenberg:8
composer require sensiolabs/gotenberg-bundle
config/packages/sensiolabs_gotenberg.yaml):
sensiolabs_gotenberg:
http_client: 'gotenberg.client'
Add to framework.http_client.scoped_clients:
framework:
http_client:
scoped_clients:
gotenberg.client:
base_uri: 'http://localhost:3000'
use Sensiolabs\GotenbergBundle\GotenbergPdfInterface;
class PdfController {
public function generatePdf(GotenbergPdfInterface $gotenberg): Response {
return $gotenberg->url()
->url('https://example.com')
->generate()
->stream();
}
}
use Sensiolabs\GotenbergBundle\GotenbergScreenshotInterface;
class ScreenshotController {
public function generateScreenshot(GotenbergScreenshotInterface $gotenberg): Response {
return $gotenberg->html()
->content('template.html.twig', ['title' => 'Test'])
->generate()
->stream();
}
}
Key Files to Explore:
config/packages/sensiolabs_gotenberg.yaml (configuration)src/Controller/ (example controllers)templates/ (Twig templates for PDFs)$gotenberg->url()
->url('https://example.com')
->timeout(30) // Optional: Set timeout in seconds
->generate()
->saveAs('/path/to/file.pdf'); // Save to filesystem
$gotenberg->html()
->content('invoice.html.twig', ['user' => $user])
->options(['margin-top' => '20mm', 'margin-bottom' => '20mm'])
->generate()
->stream(); // Stream directly to browser
$gotenberg->office()
->file($filePath) // Local file path
->format('pdf')
->generate()
->saveAs('output.pdf');
$gotenberg->url()
->url('https://example.com')
->width(1200) // Optional: Set width in pixels
->height(800) // Optional: Set height in pixels
->generate()
->saveAs('screenshot.png');
$gotenberg->markdown()
->content('# Hello World')
->generate()
->stream();
$gotenberg->merge()
->files(['file1.pdf', 'file2.pdf'])
->generate()
->saveAs('merged.pdf');
$gotenberg->encrypt()
->file('input.pdf')
->password('secure123')
->generate()
->saveAs('encrypted.pdf');
Use {{ gotenberg_asset('path/to/image.jpg') }} in Twig templates to reference assets.
Configure the assets_directory in sensiolabs_gotenberg.yaml if needed:
sensiolabs_gotenberg:
assets_directory: '%kernel.project_dir%/public/uploads'
Configure webhook endpoints in sensiolabs_gotenberg.yaml:
sensiolabs_gotenberg:
webhook:
url: 'https://your-app.com/webhook/gotenberg'
events: ['pdf:created', 'screenshot:created']
Use ->route() instead of ->url() to generate PDFs of Symfony routes:
$gotenberg->url()
->route('app.invoice_show', ['id' => 123])
->generate()
->stream();
Autowire services in controllers:
use Sensiolabs\GotenbergBundle\GotenbergPdfInterface;
class InvoiceController {
public function __construct(
private GotenbergPdfInterface $gotenberg
) {}
}
Create a console command for batch processing:
use Sensiolabs\GotenbergBundle\GotenbergPdfInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class GeneratePdfCommand extends Command {
protected static $defaultName = 'app:generate-pdf';
public function __construct(private GotenbergPdfInterface $gotenberg) {
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$gotenberg->url()
->url('https://example.com')
->generate()
->saveAs('output.pdf');
$output->writeln('PDF generated successfully!');
return Command::SUCCESS;
}
}
Use the provided testing utilities:
use Sensiolabs\GotenbergBundle\Test\GotenbergTestCase;
class MyTest extends GotenbergTestCase {
public function testPdfGeneration() {
$this->mockPdfGeneration('https://example.com', 'output.pdf');
// Assertions...
}
}
--chromium-ignore-certificate-errors to Gotenberg's Docker command.request_context.base_uri in sensiolabs_gotenberg.yaml to match your Symfony app's URL (e.g., http://host.docker.internal:8000 for Docker setups).{{ gotenberg_asset() }} instead of hardcoded paths.assets_directory is correctly configured in sensiolabs_gotenberg.yaml.->timeout(60) // 60 seconds
->chromiumTimeout(30)
php.ini or .env:
memory_limit = 512M
sensiolabs_gotenberg:
chromium:
args: ['--disable-gpu', '--no-sandbox']
symfony).host.docker.internal or configure Docker networks properly.The bundle includes a Symfony Profiler panel to inspect requests:
framework.profiler is enabled in config/packages/dev/profiler.yaml.Enable debug logging in config/packages/dev/monolog.yaml:
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["!event"]
gotenberg:
type: stream
path: "%kernel.logs_dir%/gotenberg.log"
level: debug
channels: ["gotenberg"]
Use ->getResponse() to debug raw responses:
$response = $gotenberg->url()
->url('https://example.com')
->generate()
->getResponse();
if ($response->isSuccessful()) {
$content = $response->getContent();
// Log or inspect $content
}
Override the default HTTP client for advanced use cases:
sensiolabs_gotenberg:
http_client: 'custom.gotenberg.client'
Define the client in `framework.http_client
How can I help you explore Laravel packages today?