bitcrowd / chromic_pdf

Convenient HTML to PDF/A rendering library for Elixir based on Chrome & Ghostscript
Apache License 2.0
409 stars 37 forks source link

Document fixes for font render issues. #264

Closed maltoe closed 1 year ago

maltoe commented 1 year ago

This patch adds some documentation to guide people through the inevitable font rendering issues on Linux.

On Linux, Skia has issues rendering certain fonts, it especially completely messes up font kerning. Mitigations exist in the outlined options, yet we decided not to apply any by default, in order not to break any existing templates.

Also added a new text_rendering option to ChromicPDF.Template to allow easily setting that CSS rule on * elements.

References

Tests

Setup

Below you can see test rendering of the same input markup with the various options applied.

<!DOCTYPE html>
<html>
  <head>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">

    <style>
      :root {
        font-family: Inter;
        font-size: 8.5pt;
      }

/*
    * {
      text-rendering: geometricPrecision !important;
    }
*/
    </style>
  </head>
  <body>
    <p style="font-size: 0.75rem;">Hello World</p>
  </body>
</html>
iex> ChromicPDF.start_link(); {:ok, pdf} = ChromicPDF.print_to_pdf({:file, "test.html"}, evaluate: %{expression: "new Promise(resolve => setTimeout(resolve, 2000))" }); File.write!("test.pdf", Base.decode64!(pdf))

Results

Desc Image PDF
nothing image test-current.pdf
--font-render-hinting=medium image test-font-render-hinting-medium.pdf
--font-render-hinting=none image test-font-render-hinting-none.pdf
text-rendering: geometricPrecision image test-text-rendering-geometric-precision.pdf