CourtBouillon

Authentic people growing open source code with taste

WeasyPrint Without Cairo, What’s Different?

The next major version of WeasyPrint will work without Cairo. What does it change for you?

Why?

During these years, Cairo has been very helpful to draw things in output files. But, let’s face it: (1) Cairo isn’t in good shape currently, and (2) it’s actually not fully adapted to what we want to do: generate documents.

The first part is sad, but actually true: Cairo, a library used by millions of people without ever noticing, doesn’t get the love it needs. It’s been slowly abandoned by important software (including browsers) and lacks an active team of developers with enough time to regularly release versions.

The second part has become more and more evident with years. As explained in a previous article, a lot of features can only be added by finely mastering the PDF output: forms, signatures, accessibility, color profiles, advanced metadata… Cairo is able to generate PDF and PNG, but also a lot of other things, including accelerated renderings on screens, abstract surfaces, and even OpenGL. This myriad of output formats somehow prevents the API from being specific enough for the advanced PDF files we need.

That’s why we developed pydyf, our own low-level PDF generator to replace Cairo.

What We’ll Lose

The main (and only real) thing we’ll lose in WeasyPrint is the PNG output.

Generating PNG files has been useful for a lot of cases (including tests), but it’s always been a little bit awkward: pagination is the main goal of WeasyPrint, but it’s not possible with images. It was a cheap feature thanks to Cairo, that’s why it’s been added and maintained, and even found users.

Without Cairo, the task is much more difficult. You think that generating PDF files manually is complex? We think that rasterizing vector drawings is even more complex. We don’t want to develop the feature ourselves.

So… No more PNG output. That’s it.

If you rely on this feature, don’t be afraid: there’s a solution for you. Third-party software is perfectly able to transform PDF documents into PNG images, including the well-known Ghostscript and Poppler libraries. It works so well that we now use Ghostscript for our tests, and managed to get them pass with minor adjustment.

If it’s included for tests, then why isn’t it included in WeasyPrint? The reason is not technical, it’s legal: Ghostscript is released under AGPL, and Poppler under GPL. If we want to use them, we have to change WeasyPrint’s license.

That’s why we now use the AGPL license for our tests. Of course, WeasyPrint is still released under BSD.

If the feature is really useful for you, please contact us, we’ll be happy to discuss this topic with you.

One last thing. Our two toys based on PNG output have also been removed: navigator and renderer. We’ve had a lot of fun playing with them and they’ll be missed, but that shouldn’t be that bad for you 😉.

What We’ll Get Back

The current pydyf branch has 5 failing tests.

The first one is Acid2. It fails because of the way Ghostscript rasterizes the PDF file, but the rendering is OK. We may just remove this test, or find a solution to make it pass, but it doesn’t mean that anything is actually broken.

The 4 other tests fail because… there’s no SVG support yet. To render SVG images, we used to rely on CairoSVG, that obviously relies on Cairo. So, just the same way we replaced Cairo by pydyf in WeasyPrint, we have to replace it in CairoSVG.

We don’t know yet if the new code will be put in a separate package, or if we’ll be a new release of CairoSVG, or if we’ll include the SVG rendering feature in WeasyPrint. But we know that we’ll be able to get the SVG support back soon, at least before the new major version of WeasyPrint is released.

What We’ll Find

The pydyf branch has become the master branch.

Right. It means that it’s time for you to test WeasyPrint without Cairo.

Even if we’ve tried hard to get the generated documents as close as possible to what they were with Cairo, there’s one thing sure in life: there will be bugs.

Don’t worry. We’ll not release WeasyPrint 53 tomorrow, you have time to test it before it hits your production stack. Please report the bugs you may find, or send us a nice mail in case everything works perfectly for you 😁.

And after that? Next steps will be described in the roadmap as soon as we define them. The first version of pydyf has been released, and we’ll take some time to fix the SVG support. When everything works well, we’ll be one step closer to WeasyPrint 53…