Showing posts with label PDF. Show all posts
Showing posts with label PDF. Show all posts

Thursday, November 28, 2013

Using Latex To Add Page Numbers and a Binding Offset to PDF Files

I spent the weekend getting my tax documents in order (yeah I know, I live a pretty extreme life).  I like to assemble them into a single PDF file of about 100 pages and keep a digital copy and a produce a bound printed copy.  The bound version is easy to make notes on and show to others.  That's all well and good, but sometimes when binding the printed version, the holes are punched through important information on the side of the pages.  Adding a binding offset solves this problem.

A binding offset is an area on the side of the page that's kept clear for the holes to be punched through.  Offsetting the pages isn't enough though, they also need to be scaled to fit into the smaller area available.  In my case I print on both sides of the paper, this complicates things further as well.  It means the binding offset needs to alternate between the left and right sides of the page on consecutive pages.

I thought that this was pretty much impossible unless you used professional tools, but I soon found out it's ridiculously easy in LaTeX.  While I was at it, I decided to add page numbering to the output file as well.

Below I'll go though a quick exaggerated demonstration of what I mean.  I've started by looking at the 2nd and 3rd pages of a 3 page document.  The page on the left covers the extents of the page.

PDF file capture
Example document
After being processed the output file now contains an area in the middle of the page to allow binding.  The page numbers are also added to the bottom of the page opposite the binding area.
PDF file capture
Binding offset and page numbers added
The LaTeX file I used to generate the output is below.  It by no means covers all possible situations, PDF files can get complex, and this could break one of the more esoteric features of the standard, so I recommend testing it first.  It worked perfectly for me though.

The code below is also just a starting point, you could also include pages from other PDF files or put multiple pages on one page, it's a highly customisable tool.  This however should be enough of a framework to get you started.  I'm still learning all the tricks myself.

% pdfbind.tex

\documentclass[10pt,a4paper,twoside]{report}
\usepackage[final]{pdfpages}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhead{}                       % clear header
\fancyfoot{}                       % clear footer
\fancyfoot[LE,RO]{\Large\thepage}  % add page numbers to pages

% remove bars from top and bottom of page
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\begin{document}
\includepdfset{offset=42mm 0cm, pagecommand=\thispagestyle{fancy}}
\includepdf[fitpaper=true,scale=0.6,pages=-]{PDFInputFile.pdf}
\end{document}



Tuesday, June 5, 2012

Using Xournal to Annotate PDF Presentations

When doing screen-casts I sometimes use PDF documents as the basis of my presentation.  To do this I need to be able to show the document full screen and easily move between pages.  Althought the standard PDF viewers in Linux don't quite do what I want, Xournal does the job brilliantly.  Although I use it for screen-casts it would work just as well in a lecture situation.

While doing presentations you can annotate PDF files and save the output to another PDF file.  I have nightmares about lectures using OHP's where the lecturer spent ages trying to get a pen that worked and then you couldn't concentrate on what they were saying because you were too busy copying notes.  Using a combination of Xournal and PDF documents you can streamline the process.  Have your presentation done before hand, if you feel the need to clarify a concept or add other relevant notes you can do that while you're presenting, at the end save the file and upload it to the internet.  This allows people viewing your presentation to focus on what you're saying instead of madly copying notes.  The process is so simple and fast that a student could walk out of a lecture and immediately retrieve the file for printing or viewing.  All that's required to do this is a computer and preferably a graphics tablet, although in a pinch a mouse could be used.

I know programs like this have been around for some time, but this is the nicest free one that I have used, so I thought I'd do a quick demo on how to use and configure Xournal.


Wednesday, December 14, 2011

Converting Image Files to PDFs

Recently I have been processing some image files and needed a way to combine them into a PDF file.  Image files by themselves can get a little bit messy, so it just tidies thing up a little by collating related images.  Don't get me wrong, I still hang on to the originals but PDFs are nicer to actually use.

So here is the situation, I have a folder that contains all the images named sequentially.  The images then need to be converted to PDFs and then combined into one file.  Additionally I want a grayscale version and a colour version.  As usual, the easiest way to do this is with a script in linux.  If you haven't got them already, you will need to run the following commands to install packages for this to work.

sudo apt-get install imagemagick
sudo apt-get install pdftk

Imagemagick is an awesome tool for processing images, particularly when processing batches of them, and Pdftk is a tool kit that allows you to rearrange, remove, and add pages to to pdf files.  With these two tools the following script does the job nicely.

#!/bin/bash

mkdir Col
mkdir BW
rm -rf ./Col/*
rm -rf ./BW/*

ls *.png | sed -e "s/.png$//" | xargs -r -I FILE \
convert FILE.png -density 300x300 -compress jpeg \
-quality 60 ./Col/FILE.pdf
 
ls *.png | sed -e "s/.png$//" | xargs -r -I FILE \
convert FILE.png -colorspace gray -density 300x300 \
-compress jpeg -quality 60 ./BW/FILE.pdf

pdftk ./BW/*.pdf cat output BW.pdf
pdftk ./Col/*.pdf cat output Col.pdf

rm -rf ./Col/*
rm -rf ./BW/*

rmdir ./Col
rmdir ./BW

The script is run from the directory that contains the images.  It first creates two directories, one for the colour PDFs and one for the GrayScale PDFs.  Any files that may exist in these directories are deleted.  Next the images are converted to PDFs by the ImageMagick convert command.  All the png files from the directory are found via the ls command and piped to sed where the file extension is removed.  Xargs is then used to run the convert command.  Options are used to control the output, density sets the viewing DPI, compress set the compression method, and quality set the JPEG compression quality.  Colorspace is used in the second command to set the output to grayscale.

Next pdftk is used to combine the PDF's that were just created into one file.  The order of the pages in the PDF is based on the alphabetical order of the input files.

The intermediary files and folders are then deleted.  Job done, and I could leave the computer unattended for most of the time as well.