PHP code example of agungsugiarto / latex-for-laravel
1. Go to this page and download the library: Download agungsugiarto/latex-for-laravel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
agungsugiarto / latex-for-laravel example snippets
// Get the compiler instance using the service container binding
$compiler = app('latex.compiler');
// Add a custom processor for mathematical expressions
$compiler->addProcessor(function($content, $next) {
// Process custom \math{} directives
$content = preg_replace(
'/\\\\math\s*{(.*?)}/s',
'\\begin{equation}$1\\end{equation}',
$content
);
return $next($content);
});
// Add a processor for custom bibliography handling
$compiler->addProcessor(function($content, $next) {
// Process \bibref{} to \cite{}
$content = str_replace('\bibref{', '\cite{', $content);
return $next($content);
});
// Get the compiler instance using the service container binding
$compiler = app('latex.compiler');
// Add a custom restorer for special markers
$compiler->addRestorer(function($content, $next) {
// Restore custom markers to PHP code
$content = preg_replace(
'/###CUSTOM_MATH_START###(.*?)###CUSTOM_MATH_END###/',
' echo $mathHelper->render("$1");
use Illuminate\Support\ServiceProvider;
class LaTeXMathExtensionServiceProvider extends ServiceProvider
{
public function boot()
{
$this->registerMathExtension();
}
private function registerMathExtension()
{
// Get the compiler instance using the service container binding
$compiler = $this->app->make('latex.compiler');
// Process \blademath{} directives
$compiler->addProcessor(function($content, $next) {
// Convert \blademath{expression} to markers
$content = preg_replace(
'/\\\\blademath\s*{(.*?)}/s',
'###MATH_START###$1###MATH_END###',
$content
);
return $next($content);
});
// Restore math markers to specialized PHP
$compiler->addRestorer(function($content, $next) {
$content = preg_replace(
'/###MATH_START###(.*?)###MATH_END###/',
' echo $this->renderMath("$1");
'providers' => [
// Other providers...
App\Providers\LaTeXMathExtensionServiceProvider::class,
],
// 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)]);
});
// In your controller or service
class DocumentService
{
public function setupAdvancedCompiler()
{
// Get the compiler instance using the service container binding
$compiler = app('latex.compiler');
// Add mathematical notation processor
$compiler->addProcessor(function($content, $next) {
// Convert \bladeformula{} to proper math environments
$content = preg_replace_callback(
'/\\\\bladeformula\s*{(.*?)}/s',
function($matches) {
return "\\begin{align}\n" . trim($matches[1]) . "\n\\end{align}";
},
$content
);
return $next($content);
});
// Add figure handling processor
$compiler->addProcessor(function($content, $next) {
// Process dynamic figure inclusion
$content = preg_replace(
'/\\\\bladefigure\s*{([^}]+)}\s*{([^}]+)}\s*{([^}]+)}/',
'###FIG_START###$1|$2|$3###FIG_END###',
$content
);
return $next($content);
});
// Restore figure markers
$compiler->addRestorer(function($content, $next) {
$content = preg_replace_callback(
'/###FIG_START###([^|]+)\|([^|]+)\|([^#]+)###FIG_END###/',
function($matches) {
return ' echo $this->renderFigure("' .
trim($matches[1]) . '", "' .
trim($matches[2]) . '", "' .
trim($matches[3]) . '");