2017 © Pedro Peláez
 

library tex-wrapper

PDFLatex wrapper class

image

vrok/tex-wrapper

PDFLatex wrapper class

  • Saturday, October 21, 2017
  • by j-schumann
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

vrok/tex-wrapper

PHP class wrapping calls to PDFLaTeX/LuaLaTeX to generate PDF files from LaTeX generated by PHP itself., (*1)

Requires pdflatex/lualatex or another engine to be installed, e.g. in Debian package texlive-latex-base., (*2)

Build Status Coverage Status, (*3)

Usage

// 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();

Post-Processing

To apply post-processing to the created PDF, e.g. to reduce file size use postProcess($cmd). The command must keep the PDF file in place, else postProcess will return false. The placeholder %file% points to the previously created PDF file, %dir% to the containing folder., (*4)

// 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']);
}

Note on texfot

Texfot tries to remove unnecessary lines from the LaTeX engine output. But while pdflatex/lualatex outputs everything including all errors to the stdout, texfot writes errors to stderr, so we have to include them in stdout by using "2>&1" in the command for the wrapper to log them. Texfot uses the temporary file /tmp/fot which is not deleted after execution so it can cause errors when used by different users which can not write the others files., (*5)

Note on LuaLaTeX

The lualatex command requires access to a working directory in $HOME, e.g. $HOME/.texlive2017, depending on the installation and version. Make sure to create it for the user executing PHP before using the script., (*6)

The Versions

21/10 2017

dev-master

9999999-dev https://www.vrok.de/

PDFLatex wrapper class

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

wrapper latex pdflatex

12/10 2017

v1.1.0

1.1.0.0 https://www.vrok.de/

PDFLatex wrapper class

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

wrapper latex pdflatex

09/10 2017

v1.0.0

1.0.0.0 https://www.vrok.de/

PDFLatex wrapper class

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

wrapper latex pdflatex