Is there a way to remove white margins when importing a pdf file?

A new command that works like \includegraphics, but crops the pdf image:

\newcommand{\includeCroppedPdf}[2][]{%
    \immediate\write18{pdfcrop #2}%
    \includegraphics[#1]{#2-crop}}

Remember: \write18 needs to be enabled. For most TeX distros set the --shell-escape flag when running latex/pdflatex etc.

Example

\documentclass{article}

\usepackage{graphicx}

\newcommand{\includeCroppedPdf}[2][]{%
    \immediate\write18{pdfcrop #2}%
    \includegraphics[#1]{#2-crop}}

\begin{document}
    \includeCroppedPdf[width=\textwidth]{test}
\end{document}

Avoid cropping on every compile

To avoid cropping on every document compilation, you could check if the cropped file already exists. (some checksum would be better)

\documentclass{article}

\usepackage{graphicx}

\newcommand{\includeCroppedPdf}[2][]{%
    \IfFileExists{./#2-crop.pdf}{}{%
        \immediate\write18{pdfcrop #2 #2-crop.pdf}}%
    \includegraphics[#1]{#2-crop.pdf}}

\begin{document}
    \includeCroppedPdf[width=\textwidth]{test}
\end{document} 

MD5 Checksum Example

The Idea is to save the MD5 of the image and compare it on the next run. This requires the \pdf@filemdfivesum macro (only works with PDFLaTeX or LuaLaTeX). For XeLaTeX You could use \write18 with md5sum utility or do a file diff.

\documentclass{article}

\usepackage{graphicx}

\usepackage{etoolbox}

\makeatletter
\newcommand{\includeCroppedPdf}[2][]{\begingroup%
    \edef\temp@mdfivesum{\pdf@filemdfivesum{#2.pdf}}%
    \ifcsstrequal{#2mdfivesum}{temp@mdfivesum}{}{%
        %file changed
        \immediate\write18{pdfcrop #2 #2-crop.pdf}}%
        \immediate\write\@auxout{\string\expandafter\string\gdef\string\csname\space #2mdfivesum\string\endcsname{\temp@mdfivesum}}%
    \includegraphics[#1]{#2-crop.pdf}\endgroup}
\makeatother

\begin{document}
    \includeCroppedPdf[width=\textwidth]{abc}
\end{document}

Tags:

Pdfcrop