bobv/latex-bundle
Symfony bundle for flexible PDF rendering via LaTeX. Build .tex documents with an OOP API, compile them to PDF, return PDFs in HTTP responses with one command, and avoid unnecessary recompiles with basic caching.
Installation:
composer require bobv/latex-bundle
Enable the bundle in config/bundles.php:
Bobv\Bundle\LatexBundle\BobvLatexBundle::class => ['all' => true],
Basic Configuration (config/packages/bob_v_latex.yaml):
bob_v_latex:
binaries:
latex: '/usr/bin/latex'
dvipdf: '/usr/bin/dvipdf'
cache_dir: '%kernel.cache_dir%/latex'
cache_enabled: true
First Use Case: Generate a PDF from a Twig template:
use Bobv\Bundle\LatexBundle\Generator\LatexGenerator;
$generator = $this->container->get(LatexGenerator::class);
$pdf = $generator->generateFromString(
'Hello, {name}!', // LaTeX template
['name' => 'World'], // Variables
'document' // Template name (for caching)
);
Resources/doc/documentation.md.Resources/doc/twig.md for template syntax.Resources/doc/caching.md for performance optimization.Dynamic PDF Generation:
// Generate from a Twig template file
$pdf = $generator->generateFromFile(
'@App/latex/templates/example.tex.twig',
['data' => 'value'],
'dynamic_pdf'
);
HTTP Response:
use Symfony\Component\HttpFoundation\Response;
return new Response(
$generator->generateFromString('...', ['var' => 'value']),
200,
['Content-Type' => 'application/pdf']
);
Caching:
# config/packages/bob_v_latex.yaml
bob_v_latex:
cache_enabled: true
cache_ttl: 3600 # 1 hour
Twig Extensions:
Use the latex Twig filter for inline LaTeX:
{{ 'Hello, {name}!'|latex(name) }}
Bibliography Support:
Configure bibtex binary in config/packages/bob_v_latex.yaml:
bob_v_latex:
binaries:
bibtex: '/usr/bin/bibtex'
Then use in Twig:
{% latex_bibliography('references.bib') %}
Error Handling: Wrap generation in a try-catch:
try {
$pdf = $generator->generate(...);
} catch (\Bobv\Bundle\LatexBundle\Exception\LatexException $e) {
// Log or handle error
}
Binary Paths:
Ensure latex/dvipdf paths are correct (use absolute paths). Test with:
which latex dvipdf
Twig 3+ Compatibility:
If using Twig 3+, ensure bob_v_latex config includes:
escaping:
use_symfony_string: true
Caching Quirks:
php bin/console cache:clear
cache_enabled: false
Log LaTeX Output:
Enable debug mode in config/packages/bob_v_latex.yaml:
debug: true
Logs will show generated .tex and .log files.
Common Errors:
.log files in cache_dir.dvipdf is installed.Custom Elements:
Extend Bobv\Bundle\LatexBundle\Element\ElementInterface for reusable LaTeX blocks:
class CustomElement implements ElementInterface {
public function render(): string { return '\\customcommand{...}'; }
}
Event Listeners:
Subscribe to latex.generate events for pre/post-processing:
// src/EventListener/LatexListener.php
public function onLatexGenerate(LatexEvent $event) {
$event->setContent($event->getContent() . '\\newpage');
}
Symfony Process:
Override Bobv\Bundle\LatexBundle\Generator\ProcessGenerator for custom binary handling.
latex_preview Twig filter for debugging:
{{ 'Hello, {name}!'|latex_preview(name) }}
texlive in your Dockerfile for consistent environments:
RUN apt-get update && apt-get install -y texlive-latex-base dvipdf
How can I help you explore Laravel packages today?