react-pdf icon indicating copy to clipboard operation
react-pdf copied to clipboard

Fix for "Cannot read unitsPerEm"

Open Pasquale007 opened this issue 1 year ago β€’ 2 comments

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch @react-pdf/[email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/@react-pdf/textkit/lib/textkit.js b/node_modules/@react-pdf/textkit/lib/textkit.js
index 7ca4f07..4e3f61e 100644
--- a/node_modules/@react-pdf/textkit/lib/textkit.js
+++ b/node_modules/@react-pdf/textkit/lib/textkit.js
@@ -2813,7 +2813,7 @@ const fontSubstitution = () => ({ string, runs }) => {
             // If anything that would impact res has changed, update it
             if (font !== lastFont ||
                 fontSize !== lastFontSize ||
-                font.unitsPerEm !== lastFont.unitsPerEm) {
+                font?.unitsPerEm !== lastFont?.unitsPerEm && lastFont) {
                 if (lastFont) {
                     res.push({
                         start: lastIndex,
@@ -2831,7 +2831,7 @@ const fontSubstitution = () => ({ string, runs }) => {
             index += char.length;
         }
     }
-    if (lastIndex < string.length) {
+    if (lastIndex < string.length && lastFont) {
         const fontSize = getFontSize(last(runs));
         res.push({
             start: lastIndex,

This issue body was partially generated by patch-package.

Pasquale007 avatar Oct 30 '25 09:10 Pasquale007

@Pasquale007 do line breaks (e.g. from text areas or forced in React {"\n"}) still work for you?

chris-farmact avatar Nov 21 '25 10:11 chris-farmact

@chris-farmact yes {"\n"} still breaks the line as well as normal text breaks

Pasquale007 avatar Nov 25 '25 19:11 Pasquale007