Håkon Hægland
Håkon Hægland

Reputation: 40778

Scale pdf to add border for printing full size pages

When printing a pdf with no border (or margins), the printer choppes off around 1mm of the image data at the edges of the paper. I am therefore looking for a solution to scale/resize a pdf page slightly on the page to add a white border at the edges that will correspond with the white space at the edges produced by the printer.

I have tried using gs so far.. For instance, suppose i have an A4 size pdf 1.pdf, then I used:

gs -sDEVICE=pdfwrite \
    -q -dBATCH -dNOPAUSE \
     -dPDFFitPage \
     -r300x300 \
     -g2232x3157 \
    -sOutputFile=1A.pdf \
     1.pdf 

Here, a full a4 paper is given by -g2480x3508 and I have tried to multiply by 0.9 to scale, but I do not see any effect of this..

Upvotes: 5

Views: 6178

Answers (4)

Michael Cole
Michael Cole

Reputation: 16257

Here's a Gist of a bash script that builds on the prev. Fixes a color compatibility problem (possibly specific to my pdf), and does some dependency checking:

#!/bin/bash

# pdfScale.sh
#
# Scale PDF to specified percentage of original size.
# Ref: http://ma.juii.net/blog/scale-page-content-of-pdf-files.

echo "This script doesn't handle files with spaces in them."

SCALE=0.95 # scaling factor (0.95 = 95%, e.g.)

# Validate args.
[ $# -eq 1 ] || { echo "***ERROR: Usage pdfScale.sh <inFile>.pdf"; exit 99; }
INFILEPDF="$1"
[[ "$INFILEPDF" =~ ^..*\.pdf$ ]] || { echo "***ERROR: Usage pdfScale.sh <inFile>.pdf"; exit 99; }
OUTFILEPDF=$(echo "$INFILEPDF" | sed -e s/\.pdf$// -).SCALED.pdf

# Dependencies
command -v identify >/dev/null 2>&1 || { echo >&2 "Please install 'imagemagick' (sudo apt-get install imagemagick).  Aborting."; exit 1; }
command -v gs >/dev/null 2>&1 || { echo >&2 "Please install 'ghostscript' (sudo apt-get install ghostscript ?).  Aborting."; exit 1; }
command -v bc >/dev/null 2>&1 || { echo >&2 "Please install 'bc' arbitrary precision calculator language.  Aborting."; exit 1; }

# Get width/height in postscript points (1/72-inch), via ImageMagick identify command.
# (Alternatively, could use Poppler pdfinfo command; or grep/sed the PDF by hand.)
IDENTIFY=($(identify $INFILEPDF 2>/dev/null)) # bash array
[ $? -ne 0 ] &GEOMETRY=($(echo ${IDENTIFY[2]} | tr "x" " ")) # bash array — $IDENTIFY[2] is of the form PGWIDTHxPGHEIGHT
PGWIDTH=${GEOMETRY[0]}; PGHEIGHT=${GEOMETRY[1]}

# Compute translation factors (to center page.
XTRANS=$(echo "scale=6; 0.5*(1.0-$SCALE)/$SCALE*$PGWIDTH" | bc)
YTRANS=$(echo "scale=6; 0.5*(1.0-$SCALE)/$SCALE*$PGHEIGHT" | bc)

echo $PGWIDTH , $PGHEIGHT , $OUTFILEPDF , $SCALE , $XTRANS , $YTRANS , $INFILEPDF , $OUTFILEPDF

# Do it.
gs \
-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
-dCompatibilityLevel="1.5" -dPDFSETTINGS="/printer" \
-dColorConversionStrategy=/LeaveColorUnchanged \
-dSubsetFonts=true -dEmbedAllFonts=true \
-dDEVICEWIDTH=$PGWIDTH -dDEVICEHEIGHT=$PGHEIGHT \
-sOutputFile="$OUTFILEPDF" \
-c "<</BeginPage{$SCALE $SCALE scale $XTRANS $YTRANS translate}>> setpagedevice" \
-f "$INFILEPDF"

https://gist.github.com/MichaelJCole/86e4968dbfc13256228a

More information about this method and a discussion of this gist is availanle on this blog post:

See tavinus/pdfScale, it's a fork with some other features added to it.

Upvotes: 5

Newton_Jose
Newton_Jose

Reputation: 141

Nice Håkon Hægland! I make a little improvement to make easy select the input.

So if you run

$ scaleA4PDF 10 yourfile.pdf

you will receive a yourfile_scaled.pdf file.

 #! /bin/bash
 input=$2
 output=$(echo $2 | sed s/.pdf/_scaled.pdf/)
 if [ $# -ne 2 ] ; then
 echo "Bad arguments!"
 exit
 fi

 # assume 0<=$1<=100 (no error checks!)
 xx="595" #width of A4 in post script points
 yy="842" #height of A4 in pps

 ss=$(echo "scale=4; $1 / 2" | bc)
 sx=$(echo "scale=4; ${xx}"'*'"( ${ss}/ 100 )" | bc)
 sy=$(echo "scale=4; ${yy}"'*'"( ${ss}/ 100 )" | bc)
 s=$(echo "scale=4; 1 - $1 / 100" | bc)
 gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
 -dCompatibilityLevel="1.3" -dPDFSETTINGS="/printer" \
 -dSubsetFonts=true -dEmbedAllFonts=true \
 -sPAPERSIZE=a4 -sOutputFile="${output}" \
 -c "<</BeginPage{${s} ${s} scale ${sx} ${sy} translate}>> setpagedevice" \
 -f ${input}

Upvotes: 2

H&#229;kon H&#230;gland
H&#229;kon H&#230;gland

Reputation: 40778

It seems like the solution provided at http://ma.juii.net/blog/scale-page-content-of-pdf-files works well here..

Based on that solution, I wrote the following bash script (scaleA4Pdf) for scaling the page content of an A4 pdf file. You can now just write scaleA4Pdf 10 to scale the page 10%..

#! /bin/bash

if [ $# -ne 1 ] ; then
    echo "Bad arguments!"
    exit
fi

# assume 0<=$1<=100 (no error checks!)
xx="595" #width of A4 in post script points 
yy="842" #height of A4 in pps

ss=$(echo "scale=4; $1 / 2" | bc)
sx=$(echo "scale=4; ${xx}"'*'"( ${ss}/ 100 )" | bc)
sy=$(echo "scale=4; ${yy}"'*'"( ${ss}/ 100 )" | bc)
s=$(echo "scale=4; 1 - $1 / 100" | bc)
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
  -dCompatibilityLevel="1.3" -dPDFSETTINGS="/printer" \
  -dSubsetFonts=true -dEmbedAllFonts=true \
  -sPAPERSIZE=a4 -sOutputFile="1A.pdf" \
  -c "<</BeginPage{${s} ${s} scale ${sx} ${sy} translate}>> setpagedevice" \
  -f 1.pdf

Upvotes: 2

Kevin Brown
Kevin Brown

Reputation: 8877

Since you did not specify a particular tool you are interested in, I would use iText to accomplish such a task. You could write simple code in Java or .NET (iTextSharp) to accomplish this task easily. Use this as inspiration (n-up tool). While it is actually putting multiple pages of a document into single pages, you could adopt this code to slightly scale individual pages in the same way.

Upvotes: 1

Related Questions