Editable PDF Forms using NodeJS and PDF-LIB
Yogesh Rajput / Senior NodeJS Developer
PDFMake is a popular library for generating PDF documents in web applications, offering a high level of customization and flexibility in the creation of PDF content. When it comes to creating PDF forms or editable PDF files—where users can interact with the document by filling out forms or editing text—PDFMake does not natively support these features.
Do check check our previous post on building complex PDF files using PDF-Make and NodeJS.
Using PDF-Lib to generate PDF forms and fill PDFs with existing templates is an excellent choice for projects requiring interactive PDF functionality. PDF-Lib is a powerful library that allows you to create, modify, and interact with PDF files in JavaScript, making it suitable for both server-side and client-side applications.
With PDF-Lib, you can:
-
Generate PDF Forms: You can programmatically create new PDF documents and add form fields to them. This includes various types of form fields such as text fields, checkboxes, radio buttons, dropdown lists, and more. You can customize these form fields with specific properties such as position, size, default values, and font settings.
-
Fill Existing PDF Forms: PDF-Lib enables you to load existing PDF documents and programmatically fill in form fields. This is particularly useful for automating the process of filling out PDF forms with data from a database or user input. You can modify the values of existing form fields and then save the modified PDF.
-
Edit or Modify PDF Documents: Beyond working with form fields, PDF-Lib allows you to add or modify text, images, vector graphics, and more. You can also use it to merge multiple PDFs into one document, split a PDF into multiple documents, or add pages to a PDF.
-
Work with Templates: By loading existing PDFs as templates, you can use PDF-Lib to overlay additional content such as text, images, or even new form fields on top of the template. This allows you to leverage existing document layouts and designs while customizing the content for different needs.
If you need to work with PDF forms or editable PDFs in a web application or server environment, PDF-Lib is a robust solution that offers a wide range of features to meet these requirements.
Let's dive into how we are using it.
Use case
We recently had two use cases to play around with PDF files:
- 1. Generate PDF forms and share to users to collect information
- 2. Use existing PDF templates to fill up detail programmatically and share
Here is the sample PDF form that we had to generate and send out to our users so that they can fill up the details and send the updated PDF.
![Sample PDF Form](/_next/static/media/template.405258fe.png)
It does require some effort to build the PDF forms precisely, but the library does support a lot of options to make simple to complex PDF forms.
PDF-Lib is useful to add validations and action buttons too. Please do note that some of those features will only work with Adobe Acrobat Reader.
Check documentation for more detail - https://pdf-lib.js.org/docs/api/classes/pdfbutton
Sample Repo
You can check the sample repo with examples on how to create template and how to use existing template.
https://github.com/AppGambitStudio/node-pdfLib
There are many other libraries out there that does support multiple features, but for us PDFMake and PDF-Lib is going great job.