[Tex/LaTex] How to use qpdf via latexmk

latexmktools

In the manual of latexmk I read that

[latexmk] can also, if needed, call an external program to do other
postprocessing on the generated files.

How can I call qpdf with latexmk? I want to do qpdf --linearize on the file that latexmk is operating on. Preferably latexmk will call qpdf only when it is called with a specific flag such as -l.

Best Answer

The possibility of postprocessing mentioned in the latexmk documentation is only for dvi and postscript files. (See the description of the -dF and -pF options, and of the configuration variables $dvi_filter and $ps_filter.) I need to correct the documentation on this point. I could improve latexmk to also do this for pdf files.

But there is a simpler and more general method. Just define the $pdflatex command to include the invocation of qpdf. For example, you could put the following in one of latexmk's initialization files:

$pdflatex = 'pdflatex %O %S && qpdf -linearize %D tmp.pdf && mv tmp.pdf %D';

(This is in a form suitable for Unix-like operating systems, it will need to be changed for MS-Windows, probably.) When latexmk uses this, it executes the pdflatex command; if successful it invokes qpdf, putting the result in a temporary file; if that is successful the temporary file is moved to replace the originally generated pdf file.

I don't have qpdf installed on my computer, so I am working from its documentation to write a suitable invocation of it.

If you want a command line option to control the use of qpdf, put the above line in its own file, e.g., useqpdf. Then you can invoke latexmk to read this file when needed:

latexmk -r useqpdf foo.tex
Related Question