PDF manipulation
5/Jun 2015
It happens, from time to time, that I find myself looking for a way to manipulate PDF on the fly. For example, I want to print them two-pages per sheet, or to extract few pages, or to shrink the size of the file without degradate the quality.
Here are few trick I collected and post here to be able to find them.
Print two pages per sheet
The first tricks comes from here
and assume you have pdfjam
installed.
This is how you can produce a pdf with two pages per sheet:
pdfjam --nup 2x1 infile.pdf --landscape --outfile outfile.pdf
Booklet
You can also print your pdf file as booklet. This means that the pages of your file are shuffled (and placed two per sheet) so that you can join them with a clip or some glue or strings in the middle just like a real book. The sommand is:
pdfbook --short-edge infile.pdf
pdfbook
is part of pdfjam
.
Extract (or join) pages
If you need to extrac some pages from your pdf file you can just run
pdftk infile.pdf cat <first_page>-<last_page> output outfile.pdf
To join pdf files, instead, run
pdftk infile1.pdf infile2.pdf infile3.pdf cat output outfile.pdf
Obviously you need pdftk
.
Shrink pdf file size
Sometiimes a pdf grows in size with no reason (apparently). It is possible to shrink it
by reduce it to pdf defaults. You will need gs
.
The command you need to run is
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -sOutputFile=outfile.pdf infile.pdf
It can be quite difficult to remember, so you can create a bash alias for a function
doing it for you. In .bashrc
add
alias pdfdefault='function _pdfdefault() { gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -sOutputFile=$2 $1;}; _pdfdefault'
and run . ~/.bashrc
before running pdfdefault infile.pdf outfile.pdf
.
Two pages per sheet with latex
It is possible, if you are writing something with pdf, to produce a pdf
with two pages per sheet without needing to run pdfjam
.
In this case just add, at the beginning of your latex document
\usepackage{pgfpages}
\pgfpagesuselayout{2 on 1}[a4paper], landscape]