#generate_pdf may return an incomplete pdf
bryanyee opened this issue · comments
There are cases in which an incomplete file may be saved and the method will return cleanly, with no indication that an error occurred and that the pdf is incomplete.
For example, if using xelatex, an error after the first page may cause an incomplete file to be written to the tmp directory and returned. (In the case that I tested, this was not an issue when using pdflatex).
LatexToPdf#generate_pdf
only raises an exception if a file doesn't exist at the specified tmp location ( <root>/tmp/rails-latex/<Process.pid>-<Thread.current.hash>/input.pdf
) after running the tex commands. An incomplete pdf file will return without an error. It seems that when using xelatex, the -halt-on-error
option isn't enough to stop invalid pdf generation. And even with pdflatex, a more robust error handler may be more desirable than just checking for the existence of a file.
One solution is to validate that the exit status of the forked child process is zero (indicating that the tex commands did not raise an exception within the child process) before returning a result. If the exit status is nonzero, then raise an exception.
$?.exitstatus.zero?
Or, if the "English" library is required,
$CHILD_STATUS.exitstatus.zero?
Here's one case that will create an incomplete pdf when using xelatex:
\documentclass[letterpaper, 10pt]{article}
\usepackage{hyperref}
\usepackage{soul}
\begin{document}
first page \clearpage \clearpage
second page \ul{\url{http://www.imdb.com/title/tt1843866/}}
more text
\end{document}
\ul and \url don't work well together and will generate an error:
! Argument of \hyper@n@rmalise has an extra }.
Despite the error, a pdf will still be generated, and both the link and "more text" will not be rendered in the pdf.
For reference, here's the recipe used:
{
command: 'xelatex',
input: 'input.tex',
arguments: ['-halt-on-error', '-shell-escape', '-interaction=batchmode'],
extra_arguments: [],
runs: 2
}