8

This question is similar to that one, but I'd like to execute pdfcrop when including a PDF.

8

When the shell escape feature (--shell-escape or --enable-write18 (MiKTeX)) is enabled, the conversion can be called via \immediate\write18{...}:

\immediate\write18{pdfcrop image.pdf}%
\includegraphics{image-crop}

The conversion can be limited to the cases, where the cropped image file not yet exists:

\IfFileExists{image-crop.pdf}{}{\immediate\write18{pdfcrop image.pdf}}%
\includegraphics{image-crop}
| improve this answer | |
  • You might mention the shellesc package, which will be particularly handy when LuaTeX 0.90 will be included in TeX Live 2016 and won't support \write18. – egreg Apr 3 '16 at 21:11
0

Based on Heiko Oberdiek's answer I wrote a command to crop all included PDFs

\usepackage{xstring}
\let\oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[2][width=\textwidth]{%
    \immediate\write18{pdfcrop #2}%
    \StrSubstitute{#2}{.pdf}{-crop.pdf}[\temp]%
    \oldincludegraphics[#1]{\temp}%
    }

Issues:

  • it doesn't check if the graphic is a PDF
  • it should be optional
| improve this answer | |
  • this is missing % at ends of lines so will add three inter-word spaces before the image, and one after. – David Carlisle Apr 4 '16 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.