Home » Posts tagged "Python"

Create a PDF document out of an HTML page

Perl has several modules on CPAN for creating and manipulating PDF files. Just a single search on PDF results in over 500 modules that have something to do with PDF files.

The most useful (or rather essential for PDF processing) are PDF::API2 and CAM::PDF. The former lends itself best for creating PDF’s and the latter for manipulating existing PDF’s and extracting data (such as plain text) from it.

Though these modules make handling PDF’s easier, handling PDF’s still isn’t much fun. As I was in need of a way to generate PDF’s out of work orders (or job tickets) and not feeling much for creating the layout manually and properly formatting paragraphs (manually) with PDF::API2 I started to look further.

I ended up trying out PDF::FromHTML. With PDF::FromHTML you can create a simple HTML layout and let the module create a PDF out of it. You can do some basic configuration such as changing fonts and font-size (check out its documentation for more). It also provides a nifty command line tool called html2pdf.pl for converting an HTML page to a PDF.

The resulting PDF’s from PDF::FromHTML weren’t as pretty as I had wanted, but good enough for the problem I needed solving. But after I started using these work order PDF’s in practice I found I needed more formatting freedom when writing the problem description. So I decided to add Markdown support through Text::Markdown.

Using Markdown I had added a list of tasks to a work order with the items being in bold text and the descriptions underneath it in normal text. Sadly the PDF’s created by PDF::FromHTML didn’t cope very well with nested HTML-elements. A bold paragraph would somehow cause the next paragraph become bold as well. I think that’s a bug in PDF::FromHTML and I’m sure it can be fixed and shame on me for not looking into it.

So instead of seeing if I could fix the bug I did a quick search on the internet and stumbled upon xhtml2pdf, which is provided by python-pisa/xhtml2pdf. Pisa is a Python library for converting HTML pages to PDF’s. It’s far more sophisticated than PDF::FromHTML as it supports more (all?) HTML tags and even CSS2 (plus some CSS3 stuff) for styling.

Currently my webapp will be using xhtml2pdf if it’s available or either fall back to PDF::FromHTML.

Some other interesting Perl PDF modules worth looking into some day are PDF::Boxer and PDF::TextBlock. And while writing this post I also found out that PhantomJS, a headless WebKit, also has a way of saving a page to PDF. So even though handling PDF’s still isn’t a lot of fun, with all these modules and software available it has become a lot easier.

Want to use a web service to convert HTML to PDF? Then take a look at HTML2PDF Web Service.

Remote controlled boat using the Raspberry Pi with an Arduino

In the last 8 weeks or so I was working together with 3 other students researching some of the possibilities of the Raspberry Pi and what it could offer the school for future education and research purposes. The results of the many small research subjects came together in a remote controlled boat.

Sadly due to me getting sick this week I haven’t been able to properly finish the project myself, but the other team members were able to though.

The boat’s (server) software is a Python app running on the Raspberry Pi (model A). This app is responsible for communicating with the Arduino through I²C and basically tells the Arduino which channels to modify. The Arduino runs a simple C program for this. There are 2 separate motor controllers which are connected to the Arduino. The rudder is a single servo which is also connected to the Arduino. Initially we had planned on mounting a camera on the pan-tilt mechanism which is located up front, but the ideas we had for implementing computer vision didn’t quite work out. The pan-tilt mechanism has two servo’s, both connected to the Arduino. So in short the Arduino drives all the hardware and the Raspberry Pi tells the Arduino what to do.

The Arduino itself is ‘mounted’ to the Raspberry Pi with the use of a custom made connector board. The Arduino is placed on top of that. On top of the Arduino is a custom made shield which has pins for connecting the motor controllers and servo’s and powers both the Arduino and Raspberry Pi.

We decided to call it the Stackberry Pi.

Stackberry Pi

Here’s our workroom where the boat was being worked on.

Raspberry Pi + Arduino powered boat

And finally its first test run!

Raspberry Pi + Arduino powered boat

But motionless pictures are no fun. Here’s a video of the boat’s first test run.


In case you’d like to hear more details about the boat please do ask.