Reputation: 81
Is there any way to get metadata from pdf document like author or title using pdf.js?
In this example : 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. Is there any way to get this info?
Upvotes: 8
Views: 18799
Reputation: 29746
try:
await getDocument(url).promise.then(doc => doc.getMetadata())
Upvotes: 0
Reputation: 639
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);
});
Upvotes: 0
Reputation: 3118
Using just the PDF.js library without a thirdparty viewer, you can get metadata like so, utilizing promises.
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. I found the method in its prototype and decided to just give it a shot. Lo and behold it worked!
Upvotes: 13
Reputation: 33
You can get document basic metadata info from PDFViewerApplication.documentInfo object. For eg: to get Author use PDFViewerApplication.documentInfo.Author
Upvotes: 2