Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
6.78k stars 647 forks source link

Loading bigger metadata informations of PDF create maximum call stack exceeded on IOS devices #1374

Open thomaspeitz opened 1 year ago

thomaspeitz commented 1 year ago

What were you trying to do?

Hey folks,

first of all. Thanks for the nice library. I am currently building a free project for schools which create certificates and the teacher love the results. So you are helping with your library in german school digitalization. Thanks!

To my problem:

I was trying several hours to debug the lib and find out what is happening.

The decodeText() triggers the "maximum call stack exceeded"

 PDFDocument.prototype.getSubject = function () {
        var subject = this.getInfoDict().lookup(PDFName.Subject);
        if (!subject)
            return undefined;
        assertIsLiteralOrHexString(subject);

        return subject.decodeText();
    };

On IOS devices the field can not be shown anymore.

Here is an example project. It allows to download a file with big subject field and then while loading it on IOS it will fail with "Maximum call stack exceeded". https://github.com/thomaspeitz/pdflib-debug

If you are interested I could pay you some bucks to solve this. Not much as it is a free project, but I see the point that this bug is hitting such a small audience that it is probably not getting fixed at all.

How did you attempt to do it?

What actually happened?

The ios device browser throws "maximum call stack exceeded" error while loading the subject field

What did you expect to happen?

I expected that the pdf subject field can be parsed without "maximum call stack exceeded" error.

How can we reproduce the issue?

https://github.com/thomaspeitz/pdflib-debug

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

Additional Notes

If you wanna pair this I would love to get at least some hint how to get this solved and can PR it. Currently I have not the slightest idea.

thomaspeitz commented 1 year ago

Okay I solved it. Need to understand why it is like that.

Occurences of:

    return String.fromCodePoint.apply(String, codePoints);

Need to be:

 var binary = '';
    for (var i = 0; i < codePoints.length; i++) {
        binary += String.fromCodePoint( codePoints[ i ] );
    }
    return binary;

This fixes the error.