vrok/tex-wrapper

PDFLatex 包装类

1.2.1 2023-01-24 18:16 UTC

This package is auto-updated.

Last update: 2024-09-24 22:31:36 UTC


README

PHP类,用于封装对PDFLaTeX/LuaLaTeX的调用,以从PHP生成的LaTeX生成PDF文件。

需要安装pdflatex/lualatex或其他引擎,例如在Debian包中为texlive-latex-base。

Build Status Coverage Status

使用方法

// autogenerate filename for TeX file in temp dir
$wrapper = new TexWrapper\Wrapper();

// use existing TeX or store in custom path.
// the resulting PDF will have the same filename with ".pdf" appended
$wrapper = new TexWrapper\Wrapper('/my/path/texfile');

// generate the TeX file
$texContent = '\documentclass{article}
        \begin{document}
        \title{Introduction to \LaTeX{}}
        \author{Author Name}
        \maketitle
        \section{Introduction}
        Here is the text of your introduction.
        \end{document}';
$wrapper->saveTex($texContent);

// to customize log output or apply texfot to filter unnecessary messages
$wrapper->setCommand('texfot '.$wrapper->getCommand().' 2>&1');

// to use lualatex instead of pdflatex
$cmd = 'lualatex --file-line-error '
        .' --interaction=nonstopmode --output-directory=%dir% %file%';
$wrapper->setCommand($cmd);

// build PDF file in the same path where the TeX file lives
$result = $wrapper->buildPdf();
if ($result) {
    echo "PDF file ".$wrapper->getPdfFile()." was created!";
} else {
    echo "PDF file wasn't generated!";
}

// even when the PDF was generated there could be errors and the latex engine
// or the post-processing command exited with an error code > 0
// getErrors() returns an array, possible indexes are: engine, missingFonts,
// postProcessor
var_dump($wrapper->getErrors());

// pdflatex always generates output, some warnings like missing fonts do not
// generate errors, the output is always saved:
var_dump($wrapper->getLog()); // returns string

// if you don't need the TeX file anymore
// it is automatically deleted on Wrapper destruction if no initial filename
// was set
$wrapper->deleteTex();

后处理

要对创建的PDF应用后处理,例如减小文件大小,请使用postProcess($cmd)。该命令必须保持PDF文件原位,否则postProcess将返回false。占位符%file%指向先前创建的PDF文件,%dir%指向包含文件夹。

// Example: use ghostscript (debian package: ghostscript) to minimize PDF file
// size. Test first: very simple documents can get larger through this!
$postProcessor = 'gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4'
        .' -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH'
        .' -dDetectDuplicateImages -dCompressFonts=true -r150'
        .' -sOutputFile=%file%.tmp %file% && mv %file%.tmp %file%';
$result = $wrapper->postProcess($postProcessor);
if ($result) {
    echo "post-processed ".$wrapper->getPdfFile();
} else {
    echo "post-processing failed!";

    $errors = $wrapper->getErrors();
    var_dump($errors['postProcessor']);
}

关于texfot的说明

Texfot试图从LaTeX引擎输出中删除不必要的行。但是,虽然pdflatex/lualatex将所有内容(包括所有错误)输出到stdout,texfot将错误写入stderr,因此我们必须在包装器的命令中使用"2>&1"将其包含在stdout中。Texfot使用临时文件/tmp/fot,该文件在执行后不会删除,因此当不同用户无法写入其他文件时,可能会引起错误。

关于LuaLaTeX的说明

lualatex命令需要访问$HOME中的工作目录,例如$HOME/.texlive2017,具体取决于安装和版本。在使用脚本之前,请确保为执行PHP的用户创建它。