【问题标题】:How can I get metadata from pdf document using pdf.js如何使用 pdf.js 从 pdf 文档中获取元数据
【发布时间】:2014-05-09 17:23:32
【问题描述】:

有没有办法使用 pdf.js 从 pdf 文档中获取元数据,例如作者或标题?

在这个例子中:http://mozilla.github.io/pdf.js/web/viewer.html?file=compressed.tracemonkey-pldi-09.pdf

<div class="row">
<span data-l10n-id="document_properties_author">
    Autor:
</span>
<p id="authorField">
    -
</p>

并且作者字段为空。有什么方法可以获取这些信息?

【问题讨论】:

标签: javascript pdf.js


【解决方案1】:

仅使用没有第三方查看器的 PDF.js 库,您可以使用 Promise 获取类似的元数据。

PDFJS.getDocument(url).then(function (pdfDoc_) {
        pdfDoc = pdfDoc_;   
        pdfDoc.getMetadata().then(function(stuff) {
            console.log(stuff); // Metadata object here
        }).catch(function(err) {
           console.log('Error getting meta data');
           console.log(err);
        });

       // Render the first page or whatever here
       // More code . . . 
    }).catch(function(err) {
        console.log('Error getting PDF from ' + url);
        console.log(err);
    });

我在将pdfDoc 对象转储到控制台并查看其功能和属性后发现了这一点。我在它的原型中找到了这个方法,并决定试一试。瞧,它奏效了!

【讨论】:

  • 我认为您的短语“利用承诺”是拼写检查过程中引入的错误? :)
  • 要“查看”对象内容,您可以:console.log(JSON.stringify(stuff,null,2))
  • “PDFJS”从何而来,当我尝试使用它时,它都是未定义的。
  • @mondjunge 不幸的是,我在 5 年前写了这个答案,并且不再使用 JavaScript。图书馆可能已经更新了。也许在这里查看一些较新的示例代码? mozilla.github.io/pdf.js/examples
  • 我从它的 github 页面中包含了 pdf.js,现在它可以工作了。猜猜 Firefox 包含的 pdf.js 有某种保护层?!
【解决方案2】:

您可以从 PDFViewerApplication.documentInfo 对象获取文档基本元数据信息。例如:获取作者使用 PDFViewerApplication.documentInfo.Author

【讨论】:

    【解决方案3】:
    pdfDoc.getMetadata(url).then(function(stuff) {
        var metadata = stuff.info.Title;
        if (metadata) {
            $('#element-html').text(stuff.info.Title); // Print metadata to html
        }
    console.log(stuff); // Print metadata to console
    }).catch(function(err) {
         console.log('Error getting meta data');
         console.log(err);
    });
    

    【讨论】:

      【解决方案4】:

      尝试:

      await getDocument(url).promise.then(doc => doc.getMetadata())
      

      【讨论】:

        猜你喜欢
        • 2017-03-30
        • 2014-09-29
        • 1970-01-01
        • 1970-01-01
        • 2015-09-05
        • 1970-01-01
        • 2018-06-23
        • 2018-03-04
        • 1970-01-01
        相关资源
        最近更新 更多