[英]How can I get metadata from pdf document using pdf.js
Is there any way to get metadata from pdf document like author or title using pdf.js?有什么方法可以使用 pdf.js 从 pdf 文档(如作者或标题)中获取元数据?
In this example : http://mozilla.github.io/pdf.js/web/viewer.html?file=compressed.tracemonkey-pldi-09.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>
And the authorField is empty.而 authorField 是空的。 Is there any way to get this info?有没有办法获得这些信息?
Using just the PDF.js library without a thirdparty viewer, you can get metadata like so, utilizing promises.仅使用 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);
});
I found this out after dumping the pdfDoc
object to the console and looking through its functions and properties.在将pdfDoc
对象转储到控制台并查看其功能和属性后,我发现了这一点。 I found the method in its prototype and decided to just give it a shot.我在它的原型中找到了这个方法,并决定试一试。 Lo and behold it worked!瞧,它奏效了!
You can get document basic metadata info from PDFViewerApplication.documentInfo object.您可以从 PDFViewerApplication.documentInfo 对象获取文档基本元数据信息。 For eg: to get Author use PDFViewerApplication.documentInfo.Author例如:获取作者使用 PDFViewerApplication.documentInfo.Author
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);
});
尝试:
await getDocument(url).promise.then(doc => doc.getMetadata())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.