Installation
composer require 2lenet/pdf-generator-bundle
Add unoserver to your docker-compose.yml (required for Word-to-PDF conversion):
unoserver:
image: registry.2le.net/2le/2le:unoserver
Publish Configuration
php artisan vendor:publish --provider="Lle\PdfGeneratorBundle\PdfGeneratorBundle" --tag="config"
Update config/lle_pdf_generator.php with your template path (default: data/pdfmodel).
First Use Case
Place a Word document (e.g., mydoc.doc) in data/pdfmodel/.
Generate PDF via Twig:
<a href="{{ path('lle_pdf_generator_show_ressource', {'id': 'mydoc'}) }}">Download PDF</a>
The bundle auto-detects mydoc.doc and converts it to PDF using the word_to_pdf generator.
Template Management
config('lle_pdf_generator.path').template_name.doc → Generates template_name.pdf.invoice_template.doc → Accessible via id: 'invoice_template'.Dynamic Generation
lle_pdf_generator_show_ressource with the template ID.use Lle\PdfGeneratorBundle\Service\PdfGeneratorService;
$pdfGenerator = app(PdfGeneratorService::class);
$pdfContent = $pdfGenerator->generate('template_name');
return response($pdfContent)->header('Content-Type', 'application/pdf');
Custom Generators
Override the default (word_to_pdf) by extending the PdfGeneratorInterface:
namespace App\Service;
use Lle\PdfGeneratorBundle\Service\PdfGeneratorInterface;
class CustomPdfGenerator implements PdfGeneratorInterface {
public function generate(string $templatePath): string {
// Custom logic (e.g., HTML-to-PDF via Dompdf)
}
}
Register in config/lle_pdf_generator.php:
default_generator: "custom_generator"
Tagging System
lle_pdf_generator_admin_balise (e.g., via Crudit):
public function getListActions(): array {
return [
ListAction::new(
"Tags",
Path::new('lle_pdf_generator_admin_balise'),
Icon::new("tag")
),
];
}
pdf_generator.yaml (project-specific):
templates:
invoice:
tags: ["finance", "report"]
contract:
tags: ["legal"]
Integration with CRUD
public function getListActions(): array {
$actions = parent::getListActions();
$actions[] = ListAction::new(
"Generate PDF",
Path::new('lle_pdf_generator_show_ressource', ['id' => 'contract']),
Icon::new("file-pdf")
);
return $actions;
}
Unoserver Dependency
word_to_pdf generator fails if unoserver is unreachable.unoserver is running in Docker and accessible via the configured host/port (default: http://unoserver:3000).Template Path Resolution
FileNotFoundException.lle_pdf_generator.path points to an accessible directory and templates exist with .doc/.odt extensions.Caching Headaches
php artisan cache:clear
php artisan config:clear
Route Conflicts
lle_pdf_generator_show_ressource may clash with existing routes.config/routes.yaml:
lle_pdf_generator:
resource: "@LlePdfGeneratorBundle/Resources/config/routes.yaml"
prefix: "/pdf"
Log Generator Output
Enable debug mode in config/lle_pdf_generator.php:
debug: true
Logs generator calls and errors to storage/logs/laravel.log.
Validate Template IDs
Use the PdfGeneratorService to check if a template exists:
if (!$pdfGenerator->templateExists('nonexistent')) {
// Handle error
}
Test Generators Locally
Mock unoserver for local testing:
# docker-compose.yml
unoserver:
image: registry.2le.net/2le/2le:unoserver
ports:
- "3000:3000"
Access via http://localhost:3000 (if using Docker’s host network).
Custom Generators
PdfGeneratorInterface for new formats (e.g., HTML-to-PDF):
class DompdfGenerator implements PdfGeneratorInterface {
public function generate(string $templatePath): string {
$html = file_get_contents($templatePath . '.html');
$dompdf = new \Dompdf\Dompdf();
$dompdf->loadHtml($html);
$dompdf->render();
return $dompdf->output();
}
}
Pre/Post-Processing
PdfModel entity to add metadata or hooks:
namespace App\Entity;
use Lle\PdfGeneratorBundle\Entity\PdfModel as BasePdfModel;
class PdfModel extends BasePdfModel {
public function getCustomData(): array {
return ['author' => 'App'];
}
}
Tag-Based Routing
$taggedTemplates = $pdfGenerator->getTemplatesByTag('finance');
Queue PDF Generation
dispatch(new GeneratePdfJob('template_name', $user->email));
Implement the job to use PdfGeneratorService.How can I help you explore Laravel packages today?