Searching for a better workflow to get a cropped PDF and EPS graphics - Stage 2
[0] [1] xport
[2011-07-11 13:38:59]
[ pstricks conversion paper-size pdfcrop ]
[ http://tex.stackexchange.com/questions/22771] [DELETED]

This is the second stage of my journey to find the best way to get a cropped PDF and EPS graphics. You can find the first stage in Searching for a better workflow to get a cropped PDF and EPS graphics - Stage 1 [1].

I need your suggestion which method do you propose and explain the underlying consideration.

## Assumptions

1. I never need to use psgrid for the final result.
2. I always know the size of canvas on which the PSTricks objects will be drawn.

## Method A

This method will use the following batch file named MethodA.bat.

latex -interaction=nonstopmode %1
dvips -R -t unknown %1
ps2pdf -dAutoRotatePages#/None -dCompatibilityLevel#1.5 -dPDFSETTINGS#/prepress %1.ps %1-temp.pdf
pdfcrop --restricted --hires %1-temp %1.pdf
pdftops -level3 -eps %1.pdf


The input file does not specify the paper size. It is possible because the batch file above use pdfcrop to trim the surrounding whitespace.

% methodA.tex -- paper size is not specified !
\documentclass[cmyk]{article}
\usepackage{pstricks,pst-func}
%\usepackage[paperwidth=4cm,paperheight=8cm,margin=0cm]{geometry}

\pagestyle{empty}
\parindent=0cm
\begin{document}
\psset{yunit=0.4cm,xunit=0.5cm}
\begin{pspicture*}(-4,-5)(4,15)
\psaxes[Dy=2,Dx=2,labelFontSize=\bfseries\tiny,mathLabel=false]{<->}(0,0)(-4,-5)(4,15)
\psset{linewidth=1pt,linecolor=blue}
\psPolynomial[coeff=-1 0 1,linecolor=red]{-4}{4}
\psPolynomial[coeff=-1 0 1,xShift=1,linestyle=dashed]{-4}{4}
\rput[rt](3,12){\textcolor{red}{$f(x)$}}
\rput[lt](2.5,1){\textcolor{blue}{$g(x)$}}
\psframe(-4,-5)(4,15)
\end{pspicture*}
\end{document}


## Method B

Batch file named MethodB.bat. It does not use pdfcrop to save CPU time.

latex -interaction=nonstopmode %1
dvips -R -t unknown %1
ps2pdf -dAutoRotatePages#/None -dCompatibilityLevel#1.5 -dPDFSETTINGS#/prepress %1.ps
pdftops -eps %1.pdf


And the input file needs to specify the paper size exactly.

% methodB.tex -- paper size must be specified exactly !
\documentclass[cmyk]{article}
\usepackage{pstricks,pst-func}
\usepackage[paperwidth=4cm,paperheight=8cm,margin=0cm]{geometry}

\pagestyle{empty}
\parindent=0cm
\begin{document}
\psset{yunit=0.4cm,xunit=0.5cm}
\begin{pspicture*}(-4,-5)(4,15)
\psaxes[Dy=2,Dx=2,labelFontSize=\bfseries\tiny,mathLabel=false]{<->}(0,0)(-4,-5)(4,15)
\psset{linewidth=1pt,linecolor=blue}
\psPolynomial[coeff=-1 0 1,linecolor=red]{-4}{4}
\psPolynomial[coeff=-1 0 1,xShift=1,linestyle=dashed]{-4}{4}
\rput[rt](3,12){\textcolor{red}{$f(x)$}}
\rput[lt](2.5,1){\textcolor{blue}{$g(x)$}}
\psframe(-4,-5)(4,15)
\end{pspicture*}
\end{document}


## Elapsed Time Comparison

(1) Can you clarify what your goals are? I suppose you are aiming for CPU / file size, but why? 33K is tiny by modern standards, though 2-3s is not very fast. Are you using this as a backend to some kind of web system (as your other posts might imply)? Then I can see that 2-3s is really slow, but that simply suggests that latex isn't really a great tool in the first place; you're never going to get the times down below 500ms, which is probably what you'd want. What about something like SVG? - kgr
does the pdf file itself have to be cropped, or would it be sufficient if it can be used with \includegraphics without white margins in another pdflatex file? - Martin
...oh. I suppose I should have answered this question with the bounty instead of the other one. Ah well. - LianTze Lim
[0] [2011-07-12 07:57:42] xport [ACCEPTED]

## Cons of Method A:

1. pdfcrop takes a significant amount of time. For a web-based system, it really hurts a system performance.
2. pdfcrop cannot crop a valuable object when the object is surrounded by a non-white background. See the pdfcrop author's confirmation in question: pdfcrop fails to crop a PDF document with non-white background [1] for the details.
3. pdfcrop might produce a bad cropping as follows.

input file to be cropped by pdfcrop:

% cropped-n.tex
\documentclass{minimal}
\parindent=0pt
\begin{document}
$\displaystyle n$%
\end{document}


main input file:

% comparator.tex
\documentclass{minimal}
\usepackage{graphicx}
\begin{document}
$\displaystyle n$ \includegraphics{cropped-n}
\end{document}


output: It is difficult for normal eyes to detect the defect unless we magnify it 6400 times that is the maximum magnification scale provided by Adobe Reader.

## Pros of Method A:

1. As long as the valuable object is surrounded by a white background, pdfcrop can trim the excessive white spaces. Thus we don't need to specify manually and exactly a tight paper size in advance. Using pdfcrop is practically very useful when we don't know the tight paper size in advance because of glyph issues that exist such as in the following examples.

% example-1.tex
\documentclass{minimal}
\usepackage{xcolor}
\usepackage[a5paper]{geometry}
\parindent=0bp
\begin{document}
$\displaystyle E=mc^2$
\end{document}


or

% example-2.tex
\documentclass{minimal}
\usepackage{pstricks}
\usepackage[a5paper]{geometry}
\parindent=0bp
\begin{document}
\begin{pspicture}[showgrid](2,3)
\end{pspicture}
\end{document}


## Cons of Method B:

1. We need to specify manually a paper size in advance. The paper size can be specified exactly if there is no glyph issue, otherwise we must find a padding constant manually by trial and error. Here are the workarounds for the examples given in Pros of Method A:

% workaround-for-example-1.tex
\documentclass{minimal}

\newlength\Left
\newlength\Right
\newlength\Bottom
\newlength\Top

% there are 4 magic numbers obtained by trial & errors.
\setlength{\Left}{-0.420bp}
\setlength{\Right}{-0.945bp}
\setlength{\Bottom}{0.093bp}
\setlength{\Top}{0.155bp}

\newsavebox\IBox
\savebox\IBox
{%
\raisebox{\dimexpr\depth+\Bottom\relax}
[\dimexpr\totalheight+\Bottom+\Top\relax]
{\hspace\Left$\displaystyle E=mc^2$\hspace\Right}
}

\paperwidth=\wd\IBox
\paperheight=\ht\IBox
\voffset=-1in
\hoffset=-1in
\topskip=0bp
\special{papersize=\the\paperwidth,\the\paperheight}
\parindent=0bp

\begin{document}
\usebox{\IBox}
\end{document}


and

% workaround-for-example-2.tex
\documentclass{minimal}
\usepackage{pstricks}

\newlength\Width
\newlength\Height

% it is a magic number obtained by a trial and error.
% it is not necessary to make it so tight because it will be set to 0 at the final.
\setlength{\padding}{8.4bp}% set it to 0 if you turn off the grid.
\setlength{\Width}{2cm}
\setlength{\Height}{3cm}

\topskip=0bp
\special{papersize=\the\paperwidth,\the\paperheight}
\parindent=0bp

\begin{document}
\begin{pspicture}[showgrid](\Width,\Height)
\end{pspicture}
\end{document


## Pros of Method B:

1. Using the following batch file, we can compile workaround-for-example-1.tex and workaround-for-example-2.tex faster. We also don't need to specify too many switches such as embedding fonts, etc.

rem simplified batch for method B.
latex %1
dvips -t unknown %1
gswin32c -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=%1.pdf %1.ps
pdftops -eps %1.pdf

2. We can use \pagecolor{<non-white-color>}.

## Summary

Good bye pdfcrop ...

[1] http://tex.stackexchange.com/q/22939/2099

It's certainly not true that pdfcrop "cannot do cropping when the page background color is set to any color other than white". \documentclass{minimal} \usepackage{xcolor} \begin{document} \pagecolor{red} Hello \end{document} - Lev Bishop
Yes. Exactly as without the \pagecolor - Lev Bishop
@Lev: This doesn't work for me with neither of the compilers. pdfTeX 3.1415926-1.40.11-2.2 (TeX Live 2010), XeTeX 3.1415926-2.2-0.9997.4 (TeX Live 2010, LuaTeX, Version beta-0.60.2-2010071218, PDFCROP 1.31, 2010/09/17. I get a A4 PDF after cropping. - Martin Scharrer
@Martin: I use miktex 2.9. I put my intermediate files and maybe you can check at which stage there is a difference. dl.dropbox.com/u/5560769/colorback.tex dl.dropbox.com/u/5560769/colorback.dvi dl.dropbox.com/u/5560769/colorback.ps dl.dropbox.com/u/5560769/colorback-temp.pdf dl.dropbox.com/u/5560769/colorback.pdf - Lev Bishop
@Lev: When I pdfcrop your temp PDF it is still A4. I think it is the underlying Ghostscript. Which version are you using? I have GPL Ghostscript 9.01 (2011-02-07). - Martin Scharrer
@Martin: GPL Ghostscript 8.63 (2008-08-01). Here is pdfcrop --verbose --hires --restricted colorback-temp.pdf colorback.pdf output: dl.dropbox.com/u/5560769/pdfcrop.log - Lev Bishop
@Lev: Seems to the different GS version (8.63 vs. 9.01). I get %%BoundingBox: 0 0 612 792 %%HiResBoundingBox: 0.000000 0.000000 611.999981 791.999976 - Martin Scharrer
@Lev, xport: I posted this now on comp.text.tex to make the author of pdfcrop aware of it. However, there is not much you or he can do about it. Maybe I write a bug report for GPL ghostscript as well. - Martin Scharrer
@Lev, xport: Here the comp.text.tex link (took a few minutes to appear): groups.google.com/d/topic/comp.text.tex/sVM3FtWe95w/discussion - Martin Scharrer
To narrow it down, the change comes between gs8.71 and gs9.00. No obvious entries in the changelog jump out, but there are various things there affecting bbox device svn.ghostscript.com/ghostscript/tags/ghostscript-9.02/doc/… - Lev Bishop
1