Ludochaordic
Fantaisies programatico-ludiques

fpdf2.4.0 and converting GIFs to PDFs

fpdf2 is a minimalist PDF creation library for Python that I am maintaining.

With the release yesterday of its v2.4.0, I'm going to present some of its notable new features since the latest minor version.

https://github.com/pyfpdf/fpdf2/ Pypi latest version Doc: https://pyfpdf.github.io/fpdf2/

JPEG images take less space

Glitched JPEG tombstone

fpdf2 now uses the newly supported DCTDecode image filter for JPEG images, in order to improve the compression ratio without any image quality loss.

On test images, this reduced the size of embedded JPEG images by 90%.

Basic Markdown styling

Markdown

fpdf2 now allows to use basic Markdown-like styling: **bold**, __italics__, --underlined--.

Currently, this is only supported by the FPDF.cell method, using markdown=True, but I plan to also implement this for the FPDF.multi_cell & FPDF.write methods.

Document outline & table of contents

Screenshot from Sumatra PDF

Quoting the PDF format reference:

A PDF document may optionally display a document outline on the screen, allowing the user to navigate interactively from one part of the document to another. The outline consists of a tree-structured hierarchy of outline items, which serve as a visual table of contents to display the document’s structure to the user.

Since v2.3.3 you can define such outlines, as well as tables of contents !

Full documentation: fpdf2 / Document outline & table of contents

Annotations

Text annotation example

fpdf2 now allows to add various PDF annotations, such as text annotations or links to directly open other PDF files on the same file-system.

Full documentation: fpdf2 / Annotations

Accessibility

fpdf2 logo with cursor showing an alternate description

Since v2.3.1 alternative text descriptions can now be added by the FPDF.image() & FPDF.link() methods.

They allow screen readers to extract readable descriptions from images & links in the generated PDFs.

Presentations

Exploding cat GIF

In PDF readers, presentation mode can usually be enabled with the CTRL + L shortcut.

This release introduces support for page transitions in presentation mode: FPDF.add_page() now accepts two new optional parameters, duration & transition, to define how the viewer application should advances to the next page.

Full documentation: fpdf2 / Presentations

After introducing this feature, I realized that PDF page durations allowed to build presentations that behave like GIFs: sequences of images, one per page, that move forward automatically.

Hence I wrote a little script, gif2pdf.py, to test this silly idea. There are the result:

You can open those files in Adobe Acrobat Reader (page durations are ignored by some other PDF readers), then press CTRL+L to launch presentation mode, and then ENTER to admire the animation!

Of course PDF readers are not optimized for this kind of animated files, so the animation may be pretty slow, despite being configured to execute at 25 frames per second.


That's it for the notable recent features of fpdf2! You can also check the detailed CHANGELOG for an exhaustive list of all changes: bug fixes, other minor improvements and deprecation notices.

As someone recently asked about it: if ever you want to support my work on this open-source Python library, you can buy one of the games I published on itch.io.

Now I wish you to have a lot of fun building PDFs with fpdf2!

I recently used it myself to create an animated poem for my girlfriend 😉 I'd love to know what creations you have made using it, so please add comment below if you want to share them!