Search code examples
javascripthtmlpdfpdf.js

get metadata from pdf using javascript


hello what I'm trying to do is get metadata from a PDF file and modify it using the pdf-lib library I tried this as in the documentation

But i gives me PDFDocument is not defined

I tried this as in the documentation

<head>
    <meta charset="utf-8" />
    <script src="https://unpkg.com/[email protected]"></script>
    <script src="https://unpkg.com/[email protected]"></script>
</head>

<body>
    <button onclick="get()">get </button>
</body>

<script>
    async function get() {
        const url = 'https://pdf-lib.js.org/assets/with_cropbox.pdf'
        const existingPdfBytes = await fetch(url).then(res => res.arrayBuffer())

        const pdfDoc = await PDFDocument.load(existingPdfBytes, {
            updateMetadata: false
        })
        console.log('Title:', pdfDoc.getTitle())
        console.log('Author:', pdfDoc.getAuthor())
        console.log('Subject:', pdfDoc.getSubject())
        console.log('Creator:', pdfDoc.getCreator())
        console.log('Keywords:', pdfDoc.getKeywords())
        console.log('Producer:', pdfDoc.getProducer())
        console.log('Creation Date:', pdfDoc.getCreationDate())
        console.log('Modification Date:', pdfDoc.getModificationDate()) };
</script>

</html>

Solution

  • Per the documentation, you need to use the library namespace, like so:

    await PDFLib.PDFDocument.load(...)