[pdftex] Cropping bottom margin off one-page documents.

Michael Chapman pdftex at mchapman.com
Mon Sep 20 05:58:36 CEST 2004

On Sunday 19 September 2004 10:23 am, Sebastian Rahtz wrote:
> Michael Chapman wrote:
> >I have thought of using the basis of 'pdfcrop' and measuring the bounding
> > box for each document, and then recreating all the pdf-documents using
> > 'geometry' to set the page lengths file-by-file.
> that would be my thinking. I'd take each PDF file and use GS to make a
> bitmap,  use simple (eg imagemagick) tools to trim white space, get the
> resulting size, and use that to derive an appropriate instruction
> to hack the boundingbox

Thanks, I was just thinking of using 'gs -sDEVICE=bbox' without going that 
far (a direct crib from pdfcrop ...).

Since the first post I have got the following code working, but it is _slow_ 
(it makes each page twice):

system( "pdflatex ./sub-pdf/tmp.tex" );   # make FIRST time with silly 
#                                    page height (5 metres), near the Tex
#                                    limit ("nineteen feet").
system( "cd temp-files; gs -sDEVICE=bbox -dBATCH -dNOPAUSE tmp.pdf 2>bbox 
1>/dev/null ; grep '%%Bound' bbox > BBox; cd -" );
# could just pipe the output to grep, but the redundant file is/was
#  useful for debugging
open (BOX, "temp-files/BBox") or die "Cannot open bounding box file\n";
$bbox = <BOX> ;  close BOX;
chomp $bbox;
($junk, $junk, $lly, $junk, $ury) = split ' ', $bbox;
$length = int( 5015 - (($lly)*(25.4/72)) ) ;  
# old silly height (5m) plus a new bottom margin (15mm) minus
# what we discovered (lower-left-y) converted from "bp" into "mm"
$blength = $length - 25;    # body height for geometry
#                     (paperheight minus top (10) and bottom (15) margins).
$length = $length."mm";     # paperheight for geometry
$blength = $blength."mm";
system( "cd temp-files; sed -e 's/5000mm/$length/' -e 's/4980mm/$blength/' 
tmp.tex > crop.tex; cd -" );
#   putting margins into 'geometry' and leaving "body" out would simplify
system( "pdflatex ./sub-pdf/crop.tex" );   # make SECOND time
#                        with calculated height
system( "mv temp-files/tmp.pdf /new_pdf/$filename" );

1) the above is, obviously, in Perl. no claims to prettiness, is made ;-)>
2) the actual working code is different because the working versions of 'gs' 
and 'pdftex' are on different machines ('gs' before the Spring of this year 
had a bug that messes up many pdfcrop type operations (v.8.14 works o.k.). So 
all the intranet remote calls and file transfers have been edited out ... so 
that may have left a logical leap. If so apologies.

Suggestions for less time-consuming approaches very much welcome.

Michael Chapman

More information about the pdftex mailing list