Free JavaScript Library for PDF Metadata Management
Free & open source JavaScript library for managing metadata of PDF documents.
What is pdf-lib?
Pdf-lib is a free and open source JavaScript library for working with PDF documents to perform operations such as creating new PDFs from scratch, modifying existing ones or reading and updating metadata of PDF documents. We have already discussed PDF editing features of pdf-lib but in this review, we will only focus on metadata management features of pdf-lib which include:
- Read Metadata: You can read or extract metadata of PDF documents using pdf-lib. This metadata includes following information about the PDF document:
- Author
- Creator
- Producer
- Title
- Subject
- Keywords
- Language
- Creation Date
- Modification Date
- Update Metadata: You can also update above metadata of PDF documents using pdf-lib.
Getting Started with pdf-lib
There are two ways to install pdf-lib:
NPM Modules
You can use npm modules if you are using npm or yarn as your package manager:
Install using NPM
npm install --save pdf-lib
Install using Yarn
yarn add pdf-lib
UMD Modules
If you aren't using a package manager, UMD modules are available on the unpkg and jsDelivr CDNs:
- https://unpkg.com/pdf-lib/dist/pdf-lib.js
- https://unpkg.com/pdf-lib/dist/pdf-lib.min.js
- https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.js
- https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js
Read Metadata of PDF
We can utilize the pdf-lib library to read metadata of PDF documents. There are many functions like getAuthor, getCreator, getCreationDate etc. that allows accessing or extracting metadata of the PDF documents as demonstrated in the below code snippet:
Output
Above code snippet will display metadata of the PDF document as shown below:
Edit Metadata of PDF
We can also utilize the pdf-lib library to edit or update metadata of PDF documents. Like get functions (getAuthor, getCreator etc.), pdf-lib also provides set functions (like setAuthor, setCreator and so on) to update metadata of PDF files. We will use setAuthor and setTitle functions to update the metadata of a PDF document in below code snippet for demonstration purpose:
Conclusion
It's easier for JavaScript developers to read/extract and update metadata of the PDF documents using pdf-lib library. API is quite neat and provides standard set/get functions to read and update properties of the PDF documents. No issues are observed regarding metadata management of PDFs during our use.