Extensions And Scripts For fdpf2 / FPDF

For a small Python OCR project I had the need to add a transparent text overlay to an existing PDF file and rotate the corresponding pages if necessary.  With reportlab, rotating the pages and placing the individual characters was a bit too complicated for my taste, since the coordinate system of the unrotated page is used and you may have to work with a rotation matrix to convert coordinates. All doable, but led to headaches for me 🙂

In the end, I decided to use FPDF / fdpf2 to create the transparent text layer and then add it as overlay to the original file using pikepdf. Unfortunately, I failed first because FPDF does not support transparency. A short search led me to http://fpdf.org/en/script/ where the extensions and example scripts of FPDF, originally written in PHP, are archived. Luckily there was also a script for transparency effects that I could translate to Python for my purposes.

Since I had my text editor open already , I translated some other helpful scripts and put them on github as well.

Unfortunately, I noticed too late that fpdf2 happened to be working on implementing some functions directly in fpdf2 in parallel with me as part of a Hacktober initiative. If possible, you should use the natively implemented functions – on the other hand, the approach in the scripts may well cover specific use cases better.

Leave a Reply

Your email address will not be published. Required fields are marked *