WeasyPrint v67 Has Been Released
WeasyPrint v67
has been released! It is one of the most important versions of WeasyPrint with
many, many major features: CMYK, PDF/X, calc(), ::first-line,
breaks in grid rows, new length units, improved fonts management… Let’s discover
what’s new in v67 ✨
Colors
Version 67 includes the support of many features introduced by the CSS Color Module Level 5, and among them everything you need to correctly handle colors for printed documents.
CMYK
Before version 67, CMYK was only supported for images. Thanks to new CSS
features and the NLnet grant,
you can now define your colors with the device-cmyk()
function, giving the values you want to the cyan, magenta, yellow and black
channels 🌈!
The values given with this function will be stored directly in the PDF file, and not translated into another color space. This way, you can fully control the colors you want to store in your document, and these values will be used by the printer when the document is actually printed 🖨️.
New other ways exist to handle colors in your generated PDF. You can, for example, use device-independent colors everywhere and store them in the PDF using CIE Lab and Oklab color spaces. These colors are independent from any device and can then be converted into device-dependent channels just before being drawn on a screen or printed on paper. This conversion is possible using a color profile, that CSS now allows you to declare in your stylesheet.
Color Profiles
In PDFs, color profiles can be embedded for two different uses:
- to translate device-dependent input channels into device-independent colors, so that you can define your colors with custom channels and still have the possibility to transform them into device-independent colors if needed;
- to translate device-independent colors into device-dependent channels, typically to define how much cyan, magenta, yellow and black ink is needed to render a given device-independent color on a specific printer.
WeasyPrint now gives you the possibility to embed color profiles for these two
different uses, thanks to the @color-profile at-rule 💐.
@color-profile is usually used to let users define their own color
profiles. This way, they can define the name and the number of channels they
want for they color profile, and attach an .icc file to map these
values to device-independent colors. These colors can then be used in
CSS using the color() function, with the custom color profile name
and the different values for the different channels. These input values are
stored in the PDF, but thanks to the input color profile file embedded in the
PDF, they can be translated by the PDF reader into device-independent colors if
needed.
But @color-profile can also be used to define the color profile
used for printed output. The device-cmyk profile is defined by CSS
to allow users to map device-independent colors into CMYK channels.
By adding an .icc file in the corresponding at-rule, you
can actually define the profile that will be used to transform any
device-independent colors into the right amount of ink for your specific
printer .
PDF/X
Now that we have a proper CMYK support, why not supporting the PDF/X standard? 🤯 This subset of PDF adds extra checks to ensure that a PDF includes all the required data to be correctly printed.
WeasyPrint v67 adds a list of new supported PDF variants, from PDF/X-1a
to PDF/X-5g, available through the --pdf-variant option.
The different variants allow or deny different lists of features, carefully
select the one adapted to your needs!
And More…
Color Level 5 comes with more than CMYK colors.
You probably noticed, and maybe even used, the light-dark()
function that allows to dynamically define different colors depending on the
light or dark theme used by the browser.
This feature is now available in WeasyPrint. We don’t know how this could be
useful to generate PDFs, but we also know that users always find incredible use
cases when new features are added. So… If you ever use ligth-dark()
please let us know why 🤔!
Math Functions and New Units
Version 67 also comes with many features introduced by the
CSS Values and Units Level 4.
And one of these is among the oldest and most wanted features on WeasyPrint’s
bug tracker: the calc() function.
The calc() Function
calc() allows users to define values using additions,
multiplications, and other simple mathematical operations 🤓.
The feature can be really useful in a large number of use cases, including:
- mixing different units, for example with
calc(10px + 5%), - mixing values and variables, like in
calc(2 * var(--vertical-border)), - handling fractions, such as
calc(100% / 3)…
That’s one more reason to quit your CSS pre-processor! This feature is also widely used by many CSS frameworks, so it should help you if you want to use one of them to generate your paged documents.
More Math
If calc() is the biggest star of this module, other
mathematical functions come with it to fulfill all your mathematical
dreams 🦄 (or nightmares 😱):
- comparisons with
min(),max()andclamp(), - stepped values with
round(),mod()andrem(), - trigonometry with
sin(),cos()and their friends, - exponential functions such as
pow(),sqrt(),log()and more, - sign-related calculations with
abs()andsign().
Constants are also available for your pleasure: e, pi,
infinity, -infinity and NaN 🤷.
More Units
New units have been added to define lengths relatively to your font metrics:
capandrcapare relative to the cap-height of the current font,chandrchare relative to the advance measure of the 0 (zero) glyph,icandricare relative to the advance measure of the 水 (water) glyph.
And all units now get the actual metrics from the fonts, even for @font-face
fonts!
And Even More!
-
Talking about units, WeasyPrint now handles units in a case-insensitive way.
That’s good news for the fans of the
Q(quarter-millimeter) unit! (And please contact us if you’re a member of the Qlub! 🥸). -
The
xalias for thedppxunit is now supported.
What’s More?
Breaks in Grid Rows
The grid layout is supported by WeasyPrint since version 62, with some limitations. One of them is fragmentation: page breaks were not allowed in grid rows, which can be a problem when your whole page is a grid for example.
Page breaks in grid rows are now supported, thanks to Ocean Recap. Even if the exact algorithm is not specified yet, the result is much better for common cases and should give good renderings for your documents too!
::first-line
One of the last, unsupported features of CSS2 is now live:
you can now use the ::first-line pseudo-element,
thanks to the support of Karte Technology.
For your next book, don’t hesitate to set a specific style to the first line of
your chapters 📖!
Better Fonts Management
Fonts management is always complex in CSS, and WeasyPrint can sometimes give
unpredictable results depending on @font-face rules, fonts
installed system-wide and Fontconfig configuration.
This new version of WeasyPrint improves the font-matching algorithm
to avoid crashes and broken renderings is corner cases. @font-face fonts
are not mixed with system fonts anymore, and variable fonts can pacifically
coexist with their multi-file counterparts.
And even missing glyphs are not ignored anymore! As in many applications, WeasyPrint draws the .notdef glyph, also known as "tofu", when a glyph is not available in any font.
And a Lot of Smaller Fixes…
We can’t list here all the changes brought to you by version 67, but here are some features 🚀 and bug fixes 🐛 that could be useful for you:
- you can disable a list of unwanted network protocols when resolving URLs,
- PDF/A-1a, PDF/A-2a PDF/A-3a, PDF/A-4e and PDF/A-4f are now supported and tested,
- CSS layers are now supported, thanks to Code & Co.,
- SVG drawing avoids useless and invalid drawing operations,
- sizes and ratios are correctly handled for SVGs,
- textareas with multi-line text are correctly rendered in PDF forms…
What’s Next?
We hope you’ll have fun with this new version and that all these new features will be useful for you 💜!
Thanks a lot to all the contributors (and welcome to the newcomers!) and sponsors 💖.
After adding all these features, it’s time for us to work on specifications. We are currently working on new properties that bring layouts currently impossible to obtain with pure CSS. Which ones? You’ll have to wait a little bit to know ☺️!
We’ll also work hard to stabilize, clean, and test the current code. Maintaining the code base requires a lot of work behind the curtains, and it is important to continuously improve the quality of the implementation. We regularly rewrite some parts of the layout engine to simplify the code and make it closer to the specifications. And, as we want really hard not to break the compatibility between older and newer versions, we’ll of course add many new unit tests and rendering tests… just in case!
You use WeasyPrint in your company? You’d like to get personalized support? It’s time to subscribe to our consulting packages! You can also become an amazing WeasyPrint sponsor to give us more time to help you generate wonderful documents and turn coffee ☕ into code and tests.
If you’re interested in contributing to WeasyPrint, a list of good first issues is waiting for you. It’s the best way to contribute, and we’re there to help you if you want. Don’t hesitate to add a comment to tell that you’re interested, and we’ll get in touch soon!