Get PDF Custom Metadata in Node.js / Typescript

Discussão em 'Angular' iniciado por Jeremias Nater, Abril 29, 2025.

  1. I am trying to get Custom Metadata from a PDF file in node (angular-universal).

    The custom metadata is visible when logging the file as string, so i thought this would be an easy task.

    Packages i've tried


    I tried the following packages and many more, but they all resulted in some errors or did not include custom metadata

    pdfjs-dist

    • works in client but stops working when running Server-side-rendering (angular-universal) because of DOM-dependencies
    • problems with browser dependencies like canvas and more
    • installing dependencies like canvas causes errors canvas.node needs appropriate loader. to this i only found solutions adjusting webpack config, but i dont have a custom-webpack builder with angular-universal (Server-side-rendering)
    pdf-parse

    • has not been updated for 3 years and always goes into debug mode because it has an outdated let isDebugMode = !module.parent; in index.js (ISSUE)
    • when using a patch branch ("pdf-parse": "gitlab:aduh95/pdf-parse#patch-1",) there are other errors xx is not a function.
    pdf-lib

    • Works but does not support Custom metadata
    Example file

    copied code snipet to test from This Stackoverflow Answer

    • works for prepared metadata, but i got stuck trying to get the Custom metadata


    document.getElementById('f').oninput = async function() {
    var pdf = this.files[0];
    var details = await pdfDetails(pdf);
    console.log(details);
    };

    function pdfDetails(pdfBlob) {
    return new Promise(done => {
    var reader = new FileReader();
    reader.onload = function() {
    var raw = reader.result;
    var Pages = raw.match(/\/Type[\s]*\/Page[^s]/g).length;
    var regex = /<xmp.*?:(.*?)>(.*?)</g;
    var meta = [{
    Pages
    }];
    var matches = regex.exec(raw);
    while (matches != null) {
    matches.shift();
    meta.push({
    [matches.shift()]: matches.shift()
    });
    matches = regex.exec(raw);
    }
    done(meta);
    };
    reader.readAsBinaryString(pdfBlob);
    });
    }

    <input type=file id=f accept=".pdf">

    Continue reading...

Compartilhe esta Página