Open Source JavaScript PDF Editor Library
Free & Open Source JavaScript library to create and edit PDF documents, PDF forms & draw content on PDF.
What is pdf-lib?
Pdf-lib is a free and open source JavaScript library for working with PDF documents to performs operations like to create new PDFs from scratch and modify existing ones.
Following are some of the main features of pdf-lib:
- PDF Creation and Modification: pdf-lib enables the creation of new PDF documents from scratch and the modification of existing PDFs. This includes adding, removing, and rearranging pages and editing content.
- Content Drawing: You can draw text, images, PDF pages, vector graphics, and even SVG paths, allowing for the addition of diverse content to PDF documents.
- Form Handling: You can create interactive forms, fill out form fields, and flatten forms to make them non-editable which is useful for various document processing tasks.
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
Create PDF and Add Content to PDF
We can utilize the pdf-lib library to create PDF document and add content to PDF. This includes the ability to customize the appearance of added text, such as font size, boldness, and various styling options. In below example, we will use the createPdf function to create a new PDF document and drawText function to add text in the PDF including the formatting of the text:
Output
The following output displays the PDF document generated by above code snippet:
Modify PDF Document
We can modify an existing PDF document by adding text, images & pages etc. to it using the pdf-lib library. In below example, we will read an input PDF file from the local directory, embed the text and save the modified PDF as an output file for demonstration purpose:
Output
Below screenshot displays the output PDF document with the addition of red diagonal text:
Create PDF Forms
We can also make our own unique PDF forms using the pdf-lib library. In below code snippet, we will demonstrate the use of pdf-lib library to build forms with text fields, radio buttons, checkboxes, dropdown menus and options lists:
Output
Below screenshot shows the table created by above code
Conclusion
In conclusion, pdf-lib is an easy to use PDF editing library and has great form handling capabilities, making it a valuable resource for tasks ranging from content creation to data collection.
However, pdf-lib needs improvement in terms of documentation and handling resource-intensive operations. Despite its limitations, pdf-lib empowers developers to efficiently manage PDFs and enhance document workflows, making it a noteworthy addition to their toolkit.
Similar Products
- Apache POI XWPF | Open Source Java API to Create & Modify DOCX files
- DocX | Open Source .NET API to Create & Modify DOCX files
- Docx4J | Java API to Create & Modify DOC and DOCX files
- ExcelDataReader | Open Source .NET API to read XLS, XLSX, CSV and Spreadsheet documents
- FileFormat.Cells | Cerate and Update Excel files with C# .NET