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.
- Alpha / Transparency -> Code
- Masking images / Alpha channel -> Code
- 360 Degree Text -> Code
- Transform & Circular Text -> Code
- Diagrams -> Code
- Rectangles with rounded corners -> Code
- Draw segments of circles -> Code
- Draw star shapes -> Code
- Restrict elements to be displayed on screen / printed only -> Code
- Gradients -> Code
- Stamp and Watermark -> Code
- Adjust text to cell width -> Code
- Skew, shear, rotate text -> Code
- Sinus (Sine) Text -> Code
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.