8

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?

Michał Sekunda
  • 81
  • 1
  • 1
  • 3
  • Can you include a snippet of the code you use or something? – amoebe Mar 30 '14 at 12:20
  • The PDF does not have the author field populated. Display a different document, e.g. http://mozilla.github.io/pdf.js/web/viewer.html?file=/deuxdrop/pdf-docs/conversation-protocol.pdf – async5 Mar 31 '14 at 13:23

4 Answers4

13

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!

The Unknown Dev
  • 3,039
  • 4
  • 27
  • 39
2

You can get document basic metadata info from PDFViewerApplication.documentInfo object. For eg: to get Author use PDFViewerApplication.documentInfo.Author

0
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);
});
Emy
  • 639
  • 4
  • 13
0

try:

await getDocument(url).promise.then(doc => doc.getMetadata())
HaNdTriX
  • 28,732
  • 11
  • 78
  • 85