Installation
Add the bundle to your composer.json:
composer require cyberspectrum/pdflatex-bundle
Enable it in config/bundles.php:
return [
// ...
Cyberspectrum\PdfLaTeXBundle\PdfLaTeXBundle::class => ['all' => true],
];
Configuration
Override default settings in config/packages/cyberspectrum_pdflatex.yaml:
cyberspectrum_pdflatex:
pdflatex_path: '/usr/local/texlive/2023/bin/x86_64-linux/pdflatex' # Adjust path
timeout: 30 # Seconds before failing
log_file: '%kernel.logs_dir%/pdflatex.log' # Optional log path
First Use Case Generate a PDF from a Twig template:
use Cyberspectrum\PdfLaTeXBundle\Service\PdfLaTeXService;
class MyController extends AbstractController {
public function generatePdf(PdfLaTeXService $pdfLaTeXService) {
$template = $this->renderView('path/to/your_template.tex.twig', [
'title' => 'My Document',
'content' => 'Hello, LaTeX!'
]);
$pdf = $pdfLaTeXService->generate($template);
return new Response($pdf, 200, ['Content-Type' => 'application/pdf']);
}
}
Template Design
Use Twig templates with .tex.twig extension (e.g., document.tex.twig).
Example:
\documentclass{article}
\begin{document}
\title{{ title }}
\author{{ author }}
\maketitle
{{ content|raw }}
\end{document}
Service Integration
Inject PdfLaTeXService into controllers/services:
public function generateReport(PdfLaTeXService $pdfLaTeX) {
$data = $this->fetchReportData();
$template = $this->renderView('reports/document.tex.twig', $data);
return $pdfLaTeX->generate($template);
}
Batch Processing Process multiple files in a loop:
foreach ($templates as $templatePath) {
$template = $this->renderView($templatePath, $data);
$pdf = $pdfLaTeXService->generate($template);
$this->storePdf($pdf, $templatePath);
}
Error Handling Wrap calls in try-catch:
try {
$pdf = $pdfLaTeXService->generate($template);
} catch (\RuntimeException $e) {
$this->addFlash('error', 'PDF generation failed: ' . $e->getMessage());
return $this->redirectToRoute('home');
}
Path Configuration
pdflatex_path may fail silently if incorrect.if (!file_exists($config['pdflatex_path'])) {
throw new \RuntimeException('pdflatex not found at: ' . $config['pdflatex_path']);
}
Timeouts
timeout. Monitor logs (log_file) for hangs.Twig Security
|raw sparingly.Dependency Conflicts
symfony/process (for exec) and twig are compatible with your Symfony version.log_file for LaTeX errors (e.g., missing packages).\documentclass{article}
\begin{document}
Test PDF
\end{document}
which pdflatex # Verify path
pdflatex --version # Check LaTeX installation
Custom Commands
Extend PdfLaTeXService to support other LaTeX commands (e.g., latexmk):
// src/Service/CustomPdfService.php
class CustomPdfService extends PdfLaTeXService {
protected function getCommand(): string {
return 'latexmk -pdf ' . escapeshellarg($this->getTempFile());
}
}
Pre/Post-Processing Hook into template rendering or PDF generation:
// config/services.yaml
services:
App\EventListener\PdfListener:
tags:
- { name: kernel.event_listener, event: pdflatex.pre_generate, method: onPreGenerate }
Storage Integration
Combine with oneup/flysystem-bundle for cloud storage:
use Oneup\FlysystemBundle\FilesystemMap;
public function storePdf(string $pdf, string $filename, FilesystemMap $filesystems) {
$fs = $filesystems->get('s3');
$fs->write('pdfs/' . $filename . '.pdf', $pdf);
}
.tex files post-generation:
$pdfLaTeXService->generate($template, true); // Auto-delete temp file
How can I help you explore Laravel packages today?