Velmont Tech

Musings about video, html, tech and linux

Nicer pdf2png, with poppler

without comments

I’ve been using convert from ImageMagick to convert PDF-files to png files. However, they’re butt ugly, or rather fugly.

Just look at the text here from convert:

Rather ugly. Look at the kerning. It’s truly horrible.

Not to say poppler doesn’t have its share of problems, but it looks rather much better, don’t you agree?

So, since I had to manually edit a presentation I had to use some time making a PDF-to-PNG converter since I couldn’t find another pdf2png.

So without further ado, here is pdf2png.py:

#!/usr/bin/env python

import poppler
import gtk
import urllib
import sys, os

if len(sys.argv) != 2:
    print("Usage: %s <filename>")
    sys.exit()

input_filename = os.path.abspath(sys.argv[1])
output_filename = os.path.splitext(os.path.basename(sys.argv[1]))[0] + '-%.2d.png'
width = 768
height = 576

doc = poppler.document_new_from_file('file://%s' % \
            urllib.pathname2url(input_filename), password=None)

for i in xrange(doc.get_n_pages()):
        page = doc.get_page(i)
        pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, width, height)
        page.render_to_pixbuf(src_x=0, src_y=0, src_width=width, src_height=height,
                    scale=width/page.get_size()[0], rotation=0, pixbuf=pixbuf)
        pixbuf.save(output_filename % i, 'png')

It’s very far from perfect. Note the hard coded height and width, all of these things are possible fixes. I didn’t find any python-poppler documentation, but I used C++-docs instead, they were helpful enough. :-)

If you do any improvements or just use it, please respond in a comment.

No related posts.

Written by Odin Omdal Hørthe

October 27th, 2011 at 8:58 pm

Posted in Code,Tips/techinques

Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.