For an app I’m working on, we need a new feature: to show some pdf stored server side.
After some research I found three different solutions to explore:
- using a Cordova plugin like Reader
- trying with the promising PDF.js, a solution completely based on js to parse/render PDFs client side.
- parse PDF on server side, render it as images and let the client manage an easy jpg.
Using Cordova / native code
This is of course the cleaner solution and probably the less error-prone one, but as I’m using Intel XDK (because afaik it’s the only solution to develop iOS application with Linux) and you can’t debug Cordova plugin in real-time with it, it’s a pain to upload and build the app everytime before trying to play with it, we are not in ninethees anymore when compile time was normal.
PDF.js
The first time I looked at this, I thought that this would be the solution I was going to choose. The Mozilla team has done a great job here and is stable enougth that they included the reader in Firefox, as you can see using a recent release of Firefox and watching the demo and the pdf used for the demo alone (the interface it’s the same and the console shows same logs).
The only issue with PDF.js is speed, as with the big PDFs I’m going to work with, the smoothness of application it’s a bit downgraded.
Choosed solution / server side conversion
There are lot of solutions to convert PDF into images and also some script to convert a PDF directly into HTML. My solution is splitted into:
Convert PDF into images:
This script has to be executed server-side for each changes on pdf directory!
To achieve this in linux we can use inotify, using a little stackoverflow script.
So after installing inotify-tools we can run ./notify_changes.sh and start copy PDFs into pdf/ directory. Converting PDFs into images is slow but after a while you’ll found your images (one per page) inside proper directory.
Last thing, we need to prepare an HTML page to host our images and put some pretty css to “emulate” a book style pages:
And here a little demo!