代码之家  ›  专栏  ›  技术社区  ›  vercapo

使用javascript从pdf获取元数据

  •  0
  • vercapo  · 技术社区  · 2 年前

    你好,我想做的是从PDF文件中获取元数据,并使用PDF-lib库进行修改 我在文档中尝试过

    但我给了我 PDFDocument is not defined

    我在文档中尝试过

    <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>
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   Aaron Meese    2 年前

    the documentation ,您需要使用库名称空间,如下所示:

    await PDFLib.PDFDocument.load(...)