How to Get PDF Document Metadata using Node.js
There’s a lot of important information stored in any PDF’s metadata object. But how do we retrieve that information efficiently when we’re processing a document through a custom web application?
The answer: use an API!
Using the below node.js code, we can easily take advantage of a free API designed to rapidly retrieve any PDF document’s metadata in a neat, organized list and enumerate that data in plain text. This information can give us a valuable insight into the author & PDF subject matter, as well as the original creation date and any subsequent modification dates. Importantly, it can also tell us about the searchability of that PDF by listing the document’s keywords.
We can structure our request in a few quick steps, starting with installing the SDK. We can do so by either running the below command:
npm install cloudmersive-convert-api-client --save
Or by adding the below snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
We can now copy the below ready-to-run node.js examples into our file to structure our API request. To authorize our request, we’ll just need to enter a free-tier API key into the indicated field (if we don’t have one, we can get one by registering a free account on the Cloudmersive website):
var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveConvertApiClient.EditPdfApi();
var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editPdfGetMetadata(inputFile, callback);
And that’s all the code we’ll need! Nice and simple.