open-administration / php-latex-renderer
在PHP中渲染LaTeX模板
1.1.8
2022-05-09 15:28 UTC
Requires
- php: ^7.4|^8.0
- psr/log: ^1|^2|^3
- symfony/process: ^5.3|^6.0
- twig/twig: ^3.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.2
- monolog/monolog: ^2.3
- roave/security-advisories: dev-latest
README
此库封装了LaTeX渲染和通过Twig模板生成。这个库做了以下事情:
- 使用Twig进行LaTeX模板化
- 将用户数据插入到LaTeX模板中
- 转义用户数据,因此不能通过用户数据引入(新的)LaTeX命令
- 渲染LaTeX文件并返回PDF
- 具有紧凑的LaTeX错误日志
安装
建议通过composer进行安装
composer require open-administration/php-latex-renderer
使用方法
require './vendor/autoload.php'; $tex = new LatexRenderer('./templates/'); // <- dir where to search the templates $tex->setTmpDir('./runtime/'); // <- where to build the latex files $pdf = $tex->renderPdf('simple-report', [ // <- which template to use (file ending .tex.twig) 'title' => 'My Custom Title', // <- variables to set 'author' => 'Me!', ]); // output / save the pdf with file_put_contents('main.pdf', $pdf); // or echo with fitting header header("Content-type:application/pdf"); echo $pdf;
Twig选项
以下符号用于Twig模板化
$options = [ 'tag_block' => ['(%', '%)'], 'tag_comment' => ['(!', '!)'], 'tag_variable' => ['((', '))'], ];
由于{{}, {#}和{%)在普通LaTeX代码中太常见,因此请注意((},它也容易用在计算中。你可以用它来创建自定义变体。
$tex->setTwigLexer($options)
。
元Twig上下文
引入了一个新的全局变量_tex
,它可以在任何地方使用,其定义如下
$this->twig->addGlobal('_tex', [ 'files' => $fileNames, // with name.pdf => files/name.pdf (local path in dir) 'dir' => $tmpDir . "tex/$templateName/$uid/", 'template' => $twigTemplateName, ]);
示例: _tex.dir
尝试示例
php -f samples/simple-report.php
贡献
请在进行pull request之前运行
composer cs-fix
。