# pdfcrop fails to crop a PDF document with non-white background

I have a PDF document created from the following input file.

\documentclass[cmyk,a4paper]{minimal}
\usepackage{geometry,xcolor}
\pagecolor{cyan}
\parindent=0bp

\begin{document}
\fbox{$\displaystyle E=mc^2$}
\end{document}


But it cannot be cropped by pdfcrop because of the non-white background. I think the non-white background has affected the bounding box. So how to fix this isssue?

## Confirmation

The pdfcrop author said in Martin's and Lev's reports as follows:

The PDF format does not know a "page color". pdftex.def implements it by putting a colored box as first thing on the page. Thus it is not quite clear for a program what kind of boxes are valuable content and what could be cropped.

To get a properly cropped image, you can run the TeX document without \pagecolor and run gs -sDEVICE=bbox -dBATCH -dNOPAUSE test.pdf to get the bounding box data. It you are using hyperref, you can use the Bounding Box data for option pdfpagescrop. Or run pdfcrop with option --box and the Bounding Box data.

-- Heiko Oberdiek

-
pdfcrop uses gs -sDEVICE=bbox internally to get the bounding box information. The page color might be created by a large colored rectangle in the background, which is then taken as part of the bounding box by Ghostscript. I would look for options for it to maybe ignore certain colors. Or you try to feed the bounding box data to it manually, i.e. by creating an identical file without page colors and take this as reference. –  Martin Scharrer Jul 13 '11 at 9:06
BTW: I just figured out that \pagecolor doesn't work with preview (and therefore not with standalone). –  Martin Scharrer Jul 13 '11 at 9:23
Note that the above isn't the official bug report (which should go to the Ghostscript bug tracker), but the discussion on the TeX usenet group comp.text.tex. I liked to see what other people think about it before opening a full bug report. –  Martin Scharrer Jul 14 '11 at 7:20
@Martin The gs bbox device has setting /WhiteIsOpaque (default false) that tells it to ignore, eg, a white rectangle in the background. With gs8.71 setting this true gives same non-cropping as you saw with gs9.00. So, with gs8.71 "white" covered various colours, whereas with gs9.00 "white" really seems to mean white. –  Lev Bishop Jul 15 '11 at 17:55
@Martin: Response to your comment, "BTW: I just figured out that \pagecolor doesn't work with preview (and therefore not with standalone).", what compiler did you use? –  xport Jul 30 '11 at 9:40

I would do it another way around: (This might actually solve some other issues of you)

Place the whole thing in a savebox in the preamble. (You have to manually enable the normal font use \normalfont for this AFAIK.) Then you can measure the dimensions of the box and set the page dimensions accordantly. This will give you a PDF or PS which already has to correct size. For larger content I recommend to use a minipage wrapper as usual.

The \pagecolor command still works here. I would have thought you manually need to place a colored rectangle (\rule) with the same size behind the content.

The following works for me using pdflatex (PDF), latex->dvips1 (PS), latex->dvips1->ps2pdf (PDF), xelatex and lualatex and produces a correctly sized file.

1 without any options, no -E required

\documentclass{article}
\usepackage[cmyk]{xcolor}
\pagecolor{cyan}
\pagestyle{empty}
\parindent=0bp

\begin{lrbox}{0}%
\normalfont
\fbox{$\displaystyle E=mc^2$}%
\end{lrbox}

\sbox0{\raise\dp0\box0}% raise box so it is all height, no depth
%\sbox0{\rlap{\textcolor{cyan}{\rule{\wd0}{\ht0}}}\box0}% \pagecolor surprisingly works so this is not needed


@xport: Note that instead of box #0 you might want to use an own savebox using \newsavebox\mysavebox. –  Martin Scharrer Jul 13 '11 at 10:19
@xport: lrbox is the environment version of \savebox and can replace by it. I personally just prefer environments before macro in such cases because they allow verbatim mode and are more efficient, both because the whole text doesn't has to be read as macro argument first. \sbox is the short version of \savebox. \box is the primitive version of \usebox but deletes the box afterwards, so can only be used once. Boxes are also simply accessed by number, so \newsavebox\mysavebox will make \mysavebox a number (identical to the next box register which was free). –  Martin Scharrer Jul 13 '11 at 11:21