Skip to content

Fix for "Expected instance of _PDFDict, but got instance of PDFNull" #96

@ImmaB

Description

@ImmaB

What are you working on?

I have a pdf from a client, that I unfortunately can't share. Every pdf viewer I tried could open it but in pdf-lib I get the error "Expected instance of _PDFDict, but got instance of PDFNull". I found that in pdf-lib the page tree was missing while here for example it was not missing.

For those that have the same (apparently very rear) issue: My solution was to build and insert the page tree myself:

function fixMissingPageTree(doc: PDFDocument) {
    const pagesName = PDFName.of('Pages');
    const pages = doc.catalog.lookupMaybe(pagesName, PDFDict);
    if (pages) return;
    const indirectObjects = doc.context['indirectObjects'] as Map<PDFRef, PDFObject>;
    const pageLeafs = indirectObjects.entries()
        .filter(([_, obj]) => obj instanceof PDFPageLeaf)
        .toArray() as [PDFRef, PDFPageLeaf][];
    const structParentsName = PDFName.of('StructParents');
    const getIndex = (pageLeaf: PDFPageLeaf) => pageLeaf.lookup(structParentsName, PDFNumber)?.asNumber();
    pageLeafs.sort((a, b) => getIndex(a[1]) - getIndex(b[1]));
    const kids = PDFArray.withContext(doc.context);
    for (const pageLeaf of pageLeafs)
        kids.push(pageLeaf[0]);
    const pageTree = PDFPageTree.withContext(doc.context);
    pageTree.set(PDFName.of('Type'), pagesName);
    pageTree.set(PDFName.of('Kids'), kids);
    pageTree.set(PDFName.of('Count'), PDFNumber.of(pageLeafs.length));
    doc.catalog.set(pagesName, pageTree);
}

Additional Notes

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions