Skip to content
New issue

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

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

Already on GitHub? # to your account

[bug?] constructors fail in production builds? #755

Closed
Sjoerd82 opened this issue Jan 18, 2021 · 1 comment
Closed

[bug?] constructors fail in production builds? #755

Sjoerd82 opened this issue Jan 18, 2021 · 1 comment

Comments

@Sjoerd82
Copy link

Sjoerd82 commented Jan 18, 2021

What could be a reason that PDFDocument.load() does work in development, but not in production? I'm thinking perhaps a problem with the minified version (related to #736?)?

    const pdfFile = await axios.get(url, {responseType: 'arraybuffer' })
    const pdfDoc = await PDFDocument.load(pdfFile.data)

Yields a valid pdfDoc structure in development (left part in screenshot) but fails with e => e in production (right part in screenshot

image

Any clues as to the origin of this difference and error, and/or how to troubleshoot it?
Many thanks for this beautiful library. 🥇

Edit: I've upgraded to 1.15 with the same result.
Versions:
"pdf-lib": "^1.14.0"
"pdf-lib": "^1.15.0"

Another thing I've noticed is on development the content-type includes mention of UTF-8:
image

Update 2021-01-21:
Still digging deeper...

While using the example PDF (dod_character.pdf) and example code, I still encounter the same phenomenon {e => e}, except that the output actually has correctly filled out the forms. I found that the reason behind this is that the example know up front which fields to expect with which data type, as such it can simply do:

const nameField = form.getTextField('CharacterName 2')

And as such is simply not affected by the weird e => e phenomenon.
In my form filler the fields are more dynamic, to deal with this I do

        formFields.forEach(feField => {
            const type = feField.constructor.name
            const name = feField.getName()

            // deal with the field, based on type.

        })

Which, by the way, I copied straight out of the AP docsI: https://pdf-lib.js.org/docs/api/classes/pdfform#getfields

const type = feField.constructor.name does not return the type, but simply "e". This is where everything falls apart.
Again, only in the production build, not in my development build.

In the meantime I had also learned that the e => e is related to the class constructors:
https://stackoverflow.com/questions/65828310/what-does-could-this-javascript-notation-e-e-signify

@Sjoerd82 Sjoerd82 changed the title Error when deployed in production, possible reason? (e => e) [bug?] constructors fail in production builds? Jan 21, 2021
@Hopding
Copy link
Owner

Hopding commented Sep 24, 2021

See #933 (comment).

@Hopding Hopding closed this as completed Sep 24, 2021
rmacklin added a commit to rmacklin/pdf-lib that referenced this issue Jul 29, 2022
It is not safe/stable to rely on `.constructor.name` in general because
minification will mangle the names of constructor functions (to make
them as short as possible).

This has confused several folks before, as seen in these issues:
Hopding#736
Hopding#755
Hopding#933
Hopding#1089

As such, I think we should remove these examples from the codebase.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants