agungsugiarto / latex-for-laravel
A Laravel package that provides seamless integration between Laravel Blade templates and LaTeX, enabling dynamic PDF generation
Fund package maintenance!
agungsugiarto
Requires
- php: ^8.1
- illuminate/contracts: ^10.0||^11.0||^12.0
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.5.0||^10.0.0||^11.0.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
README
A Laravel package that provides seamless integration between Laravel Blade templates and LaTeX, enabling you to create dynamic PDF documents using familiar Blade syntax while maintaining valid LaTeX structure.
Features
- ๐ฅ Blade + LaTeX Integration: Use Laravel Blade directives within LaTeX templates
- ๐ Valid LaTeX Structure: Templates remain compilable with standard LaTeX compilers
- ๐ฏ Multiple Output Formats: Generate PDFs, download source files, or return content as strings
- ๐ Easy to Use: Simple API with powerful customization options
- ๐งช Well Tested: Comprehensive test suite ensuring reliability
Support us
If this package helps you in your projects, consider supporting the development by buying me a coffee on Saweria.
Your support helps maintain and improve this package for the Laravel community. โ๏ธ
Installation
You can install the package via composer:
composer require agungsugiarto/latex-for-laravel
The package will automatically register its service provider.
Requirements
- PHP 8.1 or higher
- Laravel 10.0 or higher
pdflatex
installed on your system (for PDF compilation)
Quick Start
Create a LaTeX template file resources/views/invoice.blade.tex
:
\documentclass{article} \usepackage[utf8]{inputenc} \newcommand{\blade}[1]{} \begin{document} \title{\blade{{ $title }}} \author{\blade{{ $author }}} \maketitle \blade{@foreach($items as $item)} \section{\blade{{ $item['name'] }}} \blade{{ $item['description'] }} \blade{@endforeach} \end{document}
Use it in your controller:
// Generate and download PDF return view('invoice', [ 'title' => 'Monthly Invoice', 'author' => 'John Doe', 'items' => [ ['name' => 'Service 1', 'description' => 'Description here'], ['name' => 'Service 2', 'description' => 'Another description'], ] ])->compile('invoice.pdf', 'download');
LaTeX Template Structure
All .blade.tex
files must follow this structure to ensure they are valid LaTeX documents that can be compiled directly while still supporting Laravel Blade directives.
Required Structure
1. Include the \newcommand{\blade}[1]{}
definition
Every LaTeX template must include this command in the preamble:
\documentclass{article} \usepackage[utf8]{inputenc} \newcommand{\blade}[1]{} \begin{document} % Your content here \end{document}
2. Wrap all Blade directives in \blade{}
All PHP/Blade code must be wrapped in \blade{}
commands:
Variables
% Escaped output \title{\blade{{ $title }}} % Unescaped output \section{\blade{!! $content !!}}
Control Structures
\blade{@if($condition)} \section{\blade{{ $sectionTitle }}} \blade{@endif} \blade{@foreach($items as $item)} \item \blade{{ $item['name'] }} \blade{@endforeach}
Comments and Complex Logic
\blade{@php $processedData = processData($rawData); @endphp} \blade{!! $processedData !!}
Complete Example
\documentclass{article} \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage{graphicx} \newcommand{\blade}[1]{} \begin{document} \title{\blade{{ $title }}} \author{\blade{{ $author }}} \date{\blade{{ $date }}} \maketitle \tableofcontents \blade{@foreach($sections as $section)} \section{\blade{{ $section['title'] }}} \blade{{ $section['content'] }} \blade{@if(isset($section['subsections']))} \blade{@foreach($section['subsections'] as $subsection)} \subsection{\blade{{ $subsection['title'] }}} \blade{!! $subsection['content'] !!} \blade{@endforeach} \blade{@endif} \blade{@endforeach} \blade{@if($includeReferences)} \bibliography{\blade{{ $bibliographyFile }}} \blade{@endif} \end{document}
Benefits of This Structure
- Valid LaTeX: The file can be compiled directly with
pdflatex
without processing Blade directives - Blade Processing: Laravel can process all
\blade{}
commands to generate dynamic content - IDE Support: LaTeX editors will recognize the file as valid LaTeX and provide syntax highlighting
- Version Control: The files are readable and diffable in version control systems
API Reference
Compilation Methods
The compile()
method supports various output destinations:
// Display PDF inline in browser return view('template', $data)->compile('document.pdf', 'inline'); // Force download PDF return view('template', $data)->compile('document.pdf', 'download'); // Save PDF to storage view('template', $data)->compile('document.pdf', 'storage'); // Save PDF to storage and display inline return view('template', $data)->compile('document.pdf', 'storage-inline'); // Save PDF to storage and download return view('template', $data)->compile('document.pdf', 'storage-download'); // Return PDF content as string $pdfContent = view('template', $data)->compile('document.pdf', 'string'); // Download LaTeX source file return view('template', $data)->compile('document.tex', 'tex'); // Return LaTeX source as string $latexSource = view('template', $data)->compile('document.tex', 'tex-string'); // Save LaTeX source to storage view('template', $data)->compile('document.tex', 'storage-tex'); // Custom handler with closure return view('template', $data)->compile('document.pdf', function($view, $pdfContent, $fileName, $texFile) { // Custom processing logic return response()->json(['success' => true, 'size' => strlen($pdfContent)]); });
Advanced Usage
Using with Blade Components
Create reusable LaTeX components:
% resources/views/components/latex-section.blade.tex \newcommand{\blade}[1]{} \section{\blade{{ $title }}} \blade{{ $content }} \blade{@if($includeSubsections)} \blade{@foreach($subsections as $subsection)} \subsection{\blade{{ $subsection }}} \blade{@endforeach} \blade{@endif}
Use in your main template:
\documentclass{article} \newcommand{\blade}[1]{} \begin{document} \blade{@component('components.latex-section', ['title' => 'Introduction', 'content' => $intro])} \blade{@endcomponent} \end{document}
Error Handling
try { return view('report', $data)->compile('report.pdf', 'download'); } catch (\Illuminate\Process\Exceptions\ProcessFailedException $e) { // Handle LaTeX compilation errors return response()->json(['error' => 'LaTeX compilation failed'], 500); }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.