<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nahuel Giudizi</title>
    <description>The latest articles on DEV Community by Nahuel Giudizi (@vexarsoft).</description>
    <link>https://dev.to/vexarsoft</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4259909%2Fccbf3c96-0153-4538-9208-145a48776ed9.png</url>
      <title>DEV Community: Nahuel Giudizi</title>
      <link>https://dev.to/vexarsoft</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vexarsoft"/>
    <language>en</language>
    <item>
      <title>react-pdf was squeezing my letters: how I patched its line breaker and justifier</title>
      <dc:creator>Nahuel Giudizi</dc:creator>
      <pubDate>Mon, 23 Nov 2026 07:02:18 +0000</pubDate>
      <link>https://dev.to/vexarsoft/react-pdf-was-squeezing-my-letters-how-i-patched-its-line-breaker-and-justifier-4419</link>
      <guid>https://dev.to/vexarsoft/react-pdf-was-squeezing-my-letters-how-i-patched-its-line-breaker-and-justifier-4419</guid>
      <description>&lt;p&gt;I build &lt;a href="https://foliumcv.com" rel="noopener noreferrer"&gt;Folium&lt;/a&gt;, a free resume builder. The live preview and the PDF you download come from the same renderer, &lt;code&gt;@react-pdf/renderer&lt;/code&gt; (4.9, with &lt;code&gt;@react-pdf/textkit&lt;/code&gt; 7.0.1), so whatever the layout engine does, people see it twice.&lt;/p&gt;

&lt;p&gt;A user told me their resume "didn't look well arranged". They were right, and the cause was not in my templates. It was in how textkit breaks and justifies lines. This post is what I measured, the three changes I made, and how I keep them from silently disappearing on the next &lt;code&gt;npm install&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What was wrong
&lt;/h2&gt;

&lt;p&gt;I rendered 30 templates with 5 kinds of content each (sample data, a realistic resume, the same one justified, and both again with a bigger font) and measured every line with PyMuPDF. Three problems showed up:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Letters squeezed in left-aligned text.&lt;/strong&gt; About 1 line in 8 had its letters compressed, by up to 2%. Not justified text: plain left-aligned paragraphs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Letters stretched in justified text.&lt;/strong&gt; On lines with few words, the gaps were filled by spreading the letters apart, up to +24% on the worst lines.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Huge gaps in narrow columns.&lt;/strong&gt; Sidebars set to justify, with no hyphenation, ended up with rivers of white space.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Cause 1: the line breaker lets left-aligned lines overflow
&lt;/h2&gt;

&lt;p&gt;textkit uses a Knuth-Plass style line breaker. Each space is a piece of "glue" that can stretch and shrink. The defaults:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;opts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stretch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;shrink&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// per space: shrink = (width * opts.width) / opts.shrink  -&amp;gt; a third of the space&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That shrink is applied whatever the alignment, so the breaker happily accepts a left-aligned line that is a bit wider than the box, counting on squeezing the spaces later. Then, when each line is aligned:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;shouldJustify&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'justify'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;lineWidth&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A line that overflows gets sent to the justifier even if it is left-aligned. The justifier can only take a little out of each space, so it takes the rest out of the letters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; only justified text is allowed to count on shrinking spaces. A left-aligned line can never be wider than its box, so it never reaches the justifier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;shrink&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'justify'&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shrink&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Cause 2: the justifier moves letters, not just spaces
&lt;/h2&gt;

&lt;p&gt;The justifier distributes the extra (or missing) width with priority factors. Spaces go first, but once each space has moved beyond a small limit, letters start absorbing the difference (&lt;code&gt;EXPAND_CHAR_FACTOR&lt;/code&gt; is 37/256 per side). That is where the +24% came from.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; letters get a factor of 0, spaces get a very large one, so all of the adjustment always fits in the spaces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;EXPAND_WHITESPACE_FACTOR&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;WHITESPACE_PRIORITY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unconstrained&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;EXPAND_CHAR_FACTOR&lt;/span&gt;       &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="n"&gt;after&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="n"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;LETTER_PRIORITY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;     &lt;span class="n"&gt;unconstrained&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;SHRINK_WHITESPACE_FACTOR&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;WHITESPACE_PRIORITY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unconstrained&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;SHRINK_CHAR_FACTOR&lt;/span&gt;       &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="n"&gt;after&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="n"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;LETTER_PRIORITY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;     &lt;span class="n"&gt;unconstrained&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why not just mark spaces as &lt;code&gt;unconstrained&lt;/code&gt;? I tried. The unconstrained path in &lt;code&gt;getDistances&lt;/code&gt; only adds the "before" side of a space when the glyph before it is also unconstrained, so half of the adjustment gets lost and justified lines come out short. Large constrained factors avoid that path entirely.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 3: narrow columns should not be justified
&lt;/h2&gt;

&lt;p&gt;Without hyphenation, a column of about 20 to 25 characters per line cannot be justified nicely. No factor fixes that. So below 25 em, justified text is laid out like its last line (usually left-aligned):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;FOLIUM_NARROW_EM&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;folNarrowJustify&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;align&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;'justify'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fontSize&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;FOLIUM_NARROW_EM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;alignLastLine&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s1"&gt;'left'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is applied in two places: when breaking lines (so the breaker treats the paragraph as not justified) and when aligning each line.&lt;/p&gt;

&lt;h2&gt;
  
  
  Patching node_modules without losing the patch
&lt;/h2&gt;

&lt;p&gt;These are changes inside &lt;code&gt;node_modules/@react-pdf/textkit/lib/textkit.js&lt;/code&gt;, applied by a &lt;code&gt;postinstall&lt;/code&gt; script. Each patch is a find/replace on an &lt;strong&gt;exact&lt;/strong&gt; fragment of the library. The rule I follow: if neither the original fragment nor the patched one is found, the script fails. A library update must make me look at the patch again; it must never drop it silently. That way the build, the Cloudflare workers, the tests and my scripts all see the same textkit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tests that fail without the patch
&lt;/h2&gt;

&lt;p&gt;The test lays out a real paragraph with textkit at 30 widths (18 to 44 em at 10 pt), left and justified, with hyphenation turned off like in the templates, and checks four things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;every letter keeps its natural advance (difference under 0.001 pt), in every width and alignment;&lt;/li&gt;
&lt;li&gt;left-aligned lines never exceed the box;&lt;/li&gt;
&lt;li&gt;in a wide column, justified lines fill the box using the spaces;&lt;/li&gt;
&lt;li&gt;under 25 em, justified text breaks into exactly the same lines as left-aligned text.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Three of the four fail on unpatched textkit, which is how I know the patch is still doing something.&lt;/p&gt;

&lt;h2&gt;
  
  
  Result
&lt;/h2&gt;

&lt;p&gt;Across 150 PDFs (30 templates times 5 cases), no document changed its number of pages, and no line has squeezed or stretched letters anymore. Justified resumes look justified in the main column and calm in the sidebars.&lt;/p&gt;

&lt;p&gt;If you use react-pdf with justified text or long paragraphs, it is worth measuring a few of your PDFs: open them with PyMuPDF, compare each glyph's advance with the font's natural width, and see how many lines were "fixed" by moving letters. I was surprised by how many.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>pdf</category>
      <category>typography</category>
    </item>
  </channel>
</rss>
