99use Twig \Environment ;
1010use Twig \Error \Error ;
1111use Twig \Extension \DebugExtension ;
12- use Twig \Extension \EscaperExtension ;
12+ use Twig \Extension \ExtensionInterface ;
1313use Twig \Lexer ;
1414use Twig \Loader \FilesystemLoader ;
15+ use Twig \Runtime \EscaperRuntime ;
1516
1617/**
1718 * Configure and execute the rendering
@@ -22,7 +23,7 @@ class LatexRenderer
2223
2324 private string $ latexExec ;
2425
25- private \ Twig \ Environment $ twig ;
26+ private Environment $ twig ;
2627
2728 private bool $ debug ;
2829
@@ -34,7 +35,7 @@ class LatexRenderer
3435 * @param $latexExec string the path to the latex exec to use
3536 * @param $debug bool true the files will not be deleted after attempted rendering
3637 */
37- public function __construct ($ templateDirs , string $ tmpDir = '/tmp/ ' , string $ latexExec = 'pdflatex ' , bool $ debug = false )
38+ public function __construct (array | string $ templateDirs , string $ tmpDir = '/tmp/ ' , string $ latexExec = 'pdflatex ' , bool $ debug = false )
3839 {
3940 $ this ->debug = $ debug ;
4041 $ this ->latexExec = $ latexExec ;
@@ -47,7 +48,7 @@ public function __construct($templateDirs, string $tmpDir = '/tmp/', string $lat
4748 'cache ' => false ,
4849 ]);
4950 $ this ->twig ->addGlobal ('_tex ' , []);
50- $ this ->twig ->getExtension (EscaperExtension ::class)->setEscaper ('tex ' , [LatexEscape::class, 'escape ' ]);
51+ $ this ->twig ->getRuntime (EscaperRuntime ::class)->setEscaper ('tex ' , [LatexEscape::class, 'escape ' ]);
5152 $ this ->twig ->addExtension (new LatexFilterExtension ());
5253 $ this ->twig ->addExtension (new PdfFilterExtension ());
5354 if ($ debug ) {
@@ -62,7 +63,7 @@ public function __construct($templateDirs, string $tmpDir = '/tmp/', string $lat
6263 }
6364
6465 /**
65- * @param array $lexerOptions {@see \Twig\ Lexer}
66+ * @param array $lexerOptions {@see Lexer}
6667 */
6768 public function setTwigLexer (array $ lexerOptions ): void
6869 {
@@ -74,9 +75,6 @@ public function setLogger(LoggerInterface $logger): void
7475 $ this ->logger = $ logger ;
7576 }
7677
77- /**
78- * @param $templateDir
79- */
8078 public function setTemplateDir ($ templateDir ): void
8179 {
8280 $ this ->twig ->setLoader (new FilesystemLoader ($ templateDir ));
@@ -96,6 +94,14 @@ public function setTmpDir(string $tmpDir): void
9694 }
9795 }
9896
97+ /**
98+ * @param ExtensionInterface $extension the custom extension to add to twig instance
99+ */
100+ public function addCustomExtension (ExtensionInterface $ extension ): void
101+ {
102+ $ this ->twig ->addExtension ($ extension );
103+ }
104+
99105 /**
100106 * @param array $variables _tex will be added
101107 * @param array $files additional files which will be saved to ./files/<name> - format: key=name, value=fileContent
@@ -133,7 +139,7 @@ public function renderPdf(string $templateName, array $variables, array $files =
133139
134140 $ proc = new Process ([$ this ->latexExec , 'main.tex ' ], $ this ->tmpDir . "tex/ $ templateName/ $ uid/ " , getenv ());
135141 $ proc ->run ();
136- // do a second time
142+ // run it a second time, some tex features need dual compilation
137143 $ proc2 = $ proc ->restart ();
138144 $ proc2 ->wait ();
139145 $ dir = $ this ->tmpDir . "tex/ $ templateName/ $ uid " ;
0 commit comments