Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 381
Håkon Hægland Avatar asked Aug 20 '13 19:08

Håkon Hægland


People also ask

How do you put a border on all pages in a PDF?

In order to insert border in PDF, you can click the "Comment" > "Rectangle" option. Then you can select a rectangle and drawn throughout the document.

How do I scale a page for printing?

Reduce or enlarge a sheet to fit the pageOn the Page Layout tab, select Page Setup. On the Page tab, select the Adjust to check box, and select the percentage to reduce or enlarge the sheet. Note: Printed data never exceeds 100%. On the File menu, click Print.


2 Answers

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:

  • How to scale the page content of PDF files? (Aug 2008; by Matt)

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

like image 170
Michael Cole Avatar answered Oct 14 '22 14:10

Michael Cole


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
like image 33
Håkon Hægland Avatar answered Oct 14 '22 13:10

Håkon Hægland