Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Font (Libre Barcode 128) : Trying to access beyond buffer length #1551

Closed
2 tasks done
joshpachner opened this issue Nov 22, 2023 · 4 comments
Closed
2 tasks done

Comments

@joshpachner
Copy link

What were you trying to do?

I'm trying to use a custom font Libre Barcode 128 to be able to have a barcode on shipping labels.

How did you attempt to do it?

I have imported fontkit, and registered it. I have tried converting the ttf file to an otf file.
I have tested other custom fonts. All work fine without the error, only this font throws the error, and sadly this is the font I need to use.

What actually happened?

when I go to PDFDocument.save() it throws the error RangeError: Trying to access beyond buffer length

import fontkit from "@pdf-lib/fontkit"; import fs from "fs"; import { PDFDocument } from "pdf-lib"; const fontData = fs.readFileSync("app/styles/LibreBarcode128-Regular.ttf"); const pdfDoc = await PDFDocument.load(template); pdfDoc.registerFontkit(fontkit); const customFont = await pdfDoc.embedFont(fontData); const form = pdfDoc.getForm(); Object.keys(data).forEach((element: string) => { try { // setting form fields in my pdf template const field = form.getTextField(element); field.setText(String(data[element as keyof typeof data])); if (element.includes("barcode")) { field.updateAppearances(customFont); } } catch (error) { console.log(error); } }); console.log("gonna save"); return await pdfDoc.save();

What did you expect to happen?

I expected it to save and not throw an error.

How can we reproduce the issue?

You can reproduce this error by downloading the Libre Barcode 128 and then try to embed the font and then save the document

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

  • My report includes a Short, Self Contained, Correct (Compilable) Example.
  • I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

Maybe I'm doing something wrong, also I am open to any other options of how to get the Libre Barcode 128 into a pdf. I have googled for hours now, and even turned to Bard, and still have not been able to find an answer. Please someone save me

@joshpachner
Copy link
Author

This is also an issue with trying to use the Libre Barcode 39

@joshpachner
Copy link
Author

Ok, this is awkward, but I think I found the solution, and it was merely setting the subset when embedding the font
await pdfDoc.embedFont(fontData, { subset: true });

It works. I'm an idiot. I'll close this issue. But hopefully my stupidity will save someone the nightmare I just went through

@dxptqhtlutehvlyxcmtg
Copy link

dxptqhtlutehvlyxcmtg commented Feb 28, 2024

We've found a number of fonts available from the Google Fonts API (https://www.googleapis.com/webfonts/v1/webfonts) that calling embedFont(binary, { subset: false }) fails with:

[
  [
    "Bebas Neue",
    "http://fonts.gstatic.com/s/bebasneue/v14/JTUSjIg69CK48gW7PXooxW5rygbi49c.ttf"
  ],
  [
    "Jura",
    "http://fonts.gstatic.com/s/jura/v31/z7NOdRfiaC4Vd8hhoPzfb5vBTP1d7auhTfmrH_rt.ttf"
  ],
  [
    "Josefin Sans",
    "http://fonts.gstatic.com/s/josefinsans/v32/Qw3PZQNVED7rKGKxtqIqX5E-AVSJrOCfjY46_DjQXMFrLgTsQV0.ttf"
  ],
  [
    "Zilla Slab",
    "http://fonts.gstatic.com/s/zillaslab/v11/dFa6ZfeM_74wlPZtksIFWj0w_HyIRlE.ttf"
  ],
  [
    "Texturina",
    "http://fonts.gstatic.com/s/texturina/v28/c4mM1nxpEtL3pXiAulRTkY-HGmNEX1b9NspjMwhAgliHhVrXy2eYGvUg25riW1OD.ttf"
  ],
  [
    "Big Shoulders Stencil Text",
    "http://fonts.gstatic.com/s/bigshouldersstenciltext/v26/5aUV9-i2oxDMNwY3dHfW7UAt3Q453SM15wNj53bCcab2SJYLLUtk1OGR04TIGS_Py_AWbQ.ttf"
  ],
  [
    "Lemonada",
    "http://fonts.gstatic.com/s/lemonada/v28/0QI-MXFD9oygTWy_R-FFlwV-bgfR7QJGeut2mfWc3Z2pTg.ttf"
  ],
  [
    "Rakkas",
    "http://fonts.gstatic.com/s/rakkas/v19/Qw3cZQlNHiblL3j_lttPOeMcCw.ttf"
  ],
  [
    "Dancing Script",
    "http://fonts.gstatic.com/s/dancingscript/v25/If2cXTr6YS-zF4S-kcSWSVi_sxjsohD9F50Ruu7BMSoHTeB9ptDqpw.ttf"
  ],
  [
    "Varela Round",
    "http://fonts.gstatic.com/s/varelaround/v20/w8gdH283Tvk__Lua32TysjIvoMGOD9gxZw.ttf"
  ],
  [
    "Rokkitt",
    "http://fonts.gstatic.com/s/rokkitt/v36/qFdb35qfgYFjGy5hukqqhw5XeRgdi1ryd5DLE76HvN6n.ttf"
  ],
  [
    "Baloo 2",
    "http://fonts.gstatic.com/s/baloo2/v21/wXK0E3kTposypRydzVT08TS3JnAmtdgazapv9Fat7WcN.ttf"
  ],
  [
    "Ropa Sans",
    "http://fonts.gstatic.com/s/ropasans/v15/EYqxmaNOzLlWtsZSScyKWjloU5KP2g.ttf"
  ],
  [
    "Spectral",
    "http://fonts.gstatic.com/s/spectral/v13/rnCr-xNNww_2s0amA-M-mHnOSOuk.ttf"
  ],
  [
    "Hachi Maru Pop",
    "http://fonts.gstatic.com/s/hachimarupop/v19/HI_TiYoRLqpLrEiMAuO9Ysfz7rW1EM_btd8u.ttf"
  ],
  [
    "Stick",
    "http://fonts.gstatic.com/s/stick/v17/Qw3TZQpMCyTtJSvfvPVDMPoF.ttf"
  ],
  [
    "Kiwi Maru",
    "http://fonts.gstatic.com/s/kiwimaru/v14/R70YjykGkuuDep-hRg6YmACQXzLhTg.ttf"
  ],
  [
    "Kosugi Maru",
    "http://fonts.gstatic.com/s/kosugimaru/v14/0nksC9PgP_wGh21A2KeqGiTqivr9iBq_.ttf"
  ],
  [
    "Faustina",
    "http://fonts.gstatic.com/s/faustina/v20/XLY4IZPxYpJfTbZAFXWzNT2SO8wpWHlsgoEvGVWWe8tbEg.ttf"
  ]
]

Using { subset: true } does allow these fonts to be embedded into a document. But subsetting itself has a number of problems so it's not usable as a workaround for us:

You can also get binaries for these same font families straight from Google's repo and strangely they don't have the same embedding problem. No idea what the difference is, and I'd guess they're probably older versions, but the file contents are different. In the case of Bebas Neue, for example, the file from the API is 57676 bytes, while the repo one is 57624 bytes.

Maybe this info will be helpful for others. I'd open a new issue but the pdf-lib project has obviously long been abandoned.

@bckmnn
Copy link

bckmnn commented May 19, 2024

Same RangeError: Trying to access beyond buffer length happened to me with IBM Plex Mono. Using { subset: true } fixed the initial error, but I could not create text containing a space in this font. So I downloaded the fonts .ttf from Googles repo, and indeed it fixed it for me. No { subset: true } required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants