Rotating PDF pages with Python

Yesterday I got a review copy of Automate the Boring Stuff with Python. It explains, among other things, how to manipulate PDFs from Python. This morning I needed to rotate some pages in a PDF, so I decided to try out the method in the book.

The sample code uses PyPDF2. I’m using Conda for my Python environment, and PyPDF2 isn’t directly available for Conda. I searched Binstar with

binstar search -t conda pypdf2

The first hit was from JimInCO, so I installed PyPDF2 with

conda install -c https://conda.binstar.org/JimInCO pypdf2

I scanned a few pages from a book to PDF, turning the book around every other page, so half the pages in the PDF were upside down. I needed a script to rotate the even numbered pages. The script counts pages from 0, so it rotates the odd numbered pages from its perspective.

    import PyPDF2

    pdf_in = open('original.pdf', 'rb')
    pdf_reader = PyPDF2.PdfFileReader(pdf_in)
    pdf_writer = PyPDF2.PdfFileWriter()

    for pagenum in range(pdf_reader.numPages):
        page = pdf_reader.getPage(pagenum)
        if pagenum % 2:
            page.rotateClockwise(180)
        pdf_writer.addPage(page)

    pdf_out = open('rotated.pdf', 'wb')
    pdf_writer.write(pdf_out)
    pdf_out.close()
    pdf_in.close()

It worked as advertised on the first try.

How to delete pages from a PDF without Adobe Acrobat

How can you delete pages from a PDF file if you don’t have Adobe Acrobat?

Download PDFCreator from SourceForge. It installs as a printer. It lets you create PDFs from any application by selecting PDFCreator as your “printer.” To delete pages from an existing PDF, open the PDF in Adobe Reader and print to PDFCreator the pages you don’t want to delete.

I don’t have a lot of experience with PDFCreator; I just downloaded it today. But it looks good and it worked well for what I was trying to do.

Update: I tried pdftk, recommended in the comments, and it works well. It’s a command line program with more features than PDFCreator.

Click to learn more about math and computing consulting

How to put PDF properties in a LaTeX file

My previous post described how to put links in a PDF file generated from LaTeX. The hyperref package that lets you include links also lets you to set PDF document properties. I’ve been using Adobe Acrobat to do this after creating my PDF file with pdflatex, but that’s unnecessary. Here’s how to put the PDF properties directly in the LaTeX file. Add something like this

\hypersetup
{
    pdfauthor={John Hancock},
    pdfsubject={Some subject},
    pdftitle={Sample document},
    pdfkeywords={LaTeX, PDF, hyperlinks}
}

after the \usepackage{hyperref} instruction at the top of your file.

* * *

For daily tips on LaTeX and typography, follow @TeXtip on Twitter.

TeXtip logo

How to link to web pages from LaTeX-generated PDF

This has been on my to-do list for a while, but I finally found out how to embed hyperlinks in a PDF file generated from LaTeX.

Short answer: put \usepackage{hyperref} in your header, and when you want to link to a page, use the command \href{URL}{anchor text}. For example,

\documentclass{article}
\usepackage{hyperref}
\begin{document}

Here's a link to \href{http://twitter.com/home}{Twitter}.

\end{document}

For much more detail on links in LaTeX documents, see Patrick Jöckel’s LaTeX-PDF page and the hyperref package documentation. [Update: looks like these links went away.]

* * *

For daily tips on LaTeX and typography, follow @TeXtip on Twitter.

TeXtip logo

Three ways to convert documents to PDF

Microsoft has an Office 2007 plug-in that lets you save documents as PDF files. This works for all Microsoft Office applications, not just Microsoft Word. The only drawback is that this only works for Office 2007, not earlier versions of Office, and does not work with other document types.

Adobe Acrobat (not the free Adobe Reader) installs a printer driver that lets you convert any document to a PDF by “printing” it to their software. The advantage is that this works for any document type. However, if you’re starting with a Word 2007 document, Microsoft’s plug-in is much faster, maybe 10x faster.

If you don’t want to buy Adobe Acrobat, you could use PDF995. Like Adobe Acrobat, this installs a printer driver; you convert documents to PDF by choosing this software as your “printer.” PDF995 comes in two versions: a free version supported by advertising, and an advertising-free version for $9.95.

I would rank these methods in the order presented above. I’ve had the best experience with the Microsoft plug-in. The Acrobat printer driver is slow, but usually does a good job. The PDF995 printer driver works OK most of the time, but I had a few issues with it. It’s been a long time since I used it, but I think the problems had to do with unwanted footers and sometimes fonts in the PDF not matching the original fonts. I’m not sure now, but I think I’ve also had problems with the Acrobat printer driver.

If you want to make a PDF from a LaTeX document, use the pdflatex program that ships with LaTeX. I’ve never had any problems with it.

Update: See this post for notes on PDFCreator and pdftk.

Including images in LaTeX files

Here are the rules for including images in LaTeX files as far as I can tell.

Near the top of your document, use \usepackage{graphicx} to load the graphicx package. Then at the point where you want to include your image, use \includeimage{...} where … is the path to your file.

If you want to create a PDF file with pdflatex, your image must be in PDF, PNG, or JPEG format.

If you want to create a DVI file with latex or a PS file with dvips, your image must be in PS or EPS format.

There’s no way to include a GIF file without first converting it to another file format.

If you use \usepackage{pgf} rather than  \usepackage{graphics} at the top of the file, nothing changes except that you must chop the file extensions off image file names.

Related post: Watch what you name graphics files in LaTeX

For daily tips on LaTeX and typography, follow @TeXtip on Twitter.

TeXtip logo

LaTeX and PowerPoint presentations

I use LaTeX for math documents and PowerPoint for presentations. When I need to make a math presentation, I can’t have everything I want in one environment. I usually go with PowerPoint.

Yesterday I tried the LaTeX Beamer package based on a friend’s recommendation. I believe I’ll switch to using this package as my default for math presentations. Here are my notes on my experience with Beamer.

Installation

Beamer is available from SourceForge. The installation instructions begin by saying “Put all files somewhere where TeX can find them.” This made me think Beamer would be another undocumented software package, but just a few words later the instructions point to a 224-page PDF manual with plenty of detail. However, I would recommend a couple minor corrections to the documentation.

  1. The manual says that if you want to install Beamer under MiKTeX, use the update wizard. But the update wizard will only update packages already installed. To install new packages with MiKTeX, use the Package Manager. (Command line mpm.exe or GUI mpm_mfc.exe.)
  2. The manual says to install latex-beamer, pgf, and xcolor. The Package Manager shows no latex-beamer package, but does show a beamer package.

The installation went smoothly overall. However, the MiKTeX Package Manager doesn’t let you know when packages have finished installing. You just have to assume when it quits giving new messages that it must be finished. At least that was my experience using the graphical version.

Using Beamer

I found Bruce Byfield’s introduction to Beamer helpful. The Beamer package is simple to use and well documented.

It’s nice to use real math typography rather than using PowerPoint hacks or pasting in LaTeX output as images. I also like animating bullet points simply by adding pause to the end of an enumerated item.

Inserting images

The biggest advantage that PowerPoint has over LaTeX is working with images. With PowerPoint you can:

  1. Paste images directly into your presentations.
  2. Edit files in place.
  3. Carry around your entire presentation as a single file.
  4. Include multiple image formats in a consistent way.

The last point may not seem like much until you’ve tried to figure out how to include images in LaTeX.

* * *

For daily tips on LaTeX and typography, follow @TeXtip on Twitter.

TeXtip logo