Aug. 26, 2015, 4:21 p.m.

    Pre-Rendering PDFs for Mobile Devices

    So I wanted to look up some local travel info, the other day.

    There's a detailed PDF file available with exactly the information I need, but my smart-phone really struggles with this heavy-weight PDF.

    Luckily, it turns out to be easy to pre-render the PDF to a big old jpeg and view this image with my phone's built-in image viewer.

    This is so straightforward and convenient, I might as well share the details for the process here.

    Some transport maps

    The map I need can be found here. The one I'll be working with is 'Lyon - Villeurbanne'. At the time of writing you can also access this map directly through this link.

    (These links are from the desktop site. If you're on a mobile device you'll be redirected to the mobile site, which is annoying, and you'll need to tell your browser to forcibly request the desktop site first.)

    There's a lot more information here than you'll find on Google maps, and this is also kept up to date by the bus company after service changes, and so on.

    The desktop version of the site proposes an interactive map, but this is pretty slow, and I like the PDF better.

    The PDF is basically exactly the same map you'll find on the sides of bus shelters and so on. I basically just want to be able to carry this map around with me in my pocket.

    PDF viewer apps

    I tried a couple of PDF viewer apps, including the Acrobat Reader app, and Googles PDF viewer, but none of these are really useable for this map. I can scroll around and pinch zoom, but the app just seems to spend forever caching and uncaching parts of the map.

    I guess it doesn't want to use too much memory, but what I really want to do is just render once, at a high enough resolution, and scroll around the resulting image.

    PDF conversion

    A bit of searching shows me a number of ways to do this.

    I end up going with ImageMagick.

    I'm on Linux, and there's a package I can install directly through the Software Manager ('imagemagick'). It looks like there are also binary installers available if you're on Windows.

    With imagemagick installed, it's as simple as calling 'convert' with the relevant input and output file names:

    ~/Downloads $ convert 3569af2d6137519d613bf0f4c6ef845b.pdf lyon_bus_map.jpg
    ~/Downloads $ ls -l lyon_bus_map.jpg
    -rw-r--r-- 1 thomas thomas 8776943 août  26 15:35 lyon_bus_map.jpg
    

    The result jpeg image is 2968 by 2457 pixels. Here's a sample:

    (inline image)

    Better resolution

    That works, and the resulting map is fairly useable, I guess we can just about read the text for bus stop names, but it's difficult to read all the road names and I'd like a bit more resolution.

    The command line options are suitably arkane. I tried things like -size and -resize semi-randomly before being rescued by this post on Stack Overflow. The trick is to use the -density option.

    The following tells imagemagick to render the PDF at 110 DPI, for example (which gives us enough resolution to read those road names):

    ~/Downloads $ convert -density 110 3569af2d6137519d613bf0f4c6ef845b.pdf lyon_bus_map.jpg
    ~/Downloads $ ls -l lyon_bus_map.jpg
    -rw-r--r-- 1 thomas thomas 17899624 août  26 16:00 lyon_bus_map.jpg
    

    Compression level

    We can also get away with some reduction in jpeg compression quality:

    ~/Downloads $ convert -density 110 3569af2d6137519d613bf0f4c6ef845b.pdf -quality 84 lyon_bus_map.jpg
    ~/Downloads $ ls -l lyon_bus_map.jpg
    -rw-r--r-- 1 thomas thomas 13880254 août  26 16:01 lyon_bus_map.jpg
    

    This comes out at 4534 by 3754 pixels. Here's another sample at the same location:

    (inline image)

    That's good enough for me.

    Theres some some whitespace around the borders, and it's possible to tell imagemagick to trim this off, but I found that hardly makes any difference to final image size (presumably because the empty borders compress very well), and I think the borders make for easier navigation.

    Viewing the image

    The final step is then just to upload the pre-rendered image onto your phone.

    I used this simple app to make a shortcut that opens the image directly. I like the way it gives you a thumbnail as the icon.

    This opens the phone's image viewer, and I found that one of the default image viewers (that comes with the phone) works fine for pinch zooming and scrolling around this image. (The other one wouldn't let me stay fully zoomed in.)

    I've noticed some small errors in the PDF rendering (missing letters), but the rendering is mostly correct, and that's not a big deal for me in practice.

    Wrap up

    Try this on a PDF bus map for your city, or for somewhere you're travelling to.

    I've posted the detailed process I used for this conversion, but I don't think that's actually the most important thing here. You could easily use other tools to convert the PDF and achieve the same result.

    What I like here is the fact that there's data available in standard file formats, and tools we can use to operate on that data and take control of how we want to use it. There doesn't have to be an app for everything.

    Getting a little blue arrow to display at the current GPS position is left as an exercise for the reader.

    Happy travelling!


    blog comments powered by Disqus