Welcome folks today in this blog post we will be using the pdfkit
library to encrypt
pdf documents with password in javascript. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the below libraries
using the below command as shown below
npm init -y
npm i pdfkit
After that you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
const PDFDocument = require("pdfkit"); const fs = require("fs"); var options = { userPassword: "123456", } // Create a document const doc = new PDFDocument(options); // Pipe its output somewhere, like to a file or HTTP response // See below for browser usage doc.pipe(fs.createWriteStream("output.pdf")); doc.addPage() // Finalize PDF file doc.end(); |
Ass you can see we are importing the pdfkit
library at the top and then we are making the pdf document
and adding two empty pages and after that we are declaring the password
and then encrypting
the pdf document with that password using the PDFDocument()
constructor. And then we are saving the pdf document using the fs
module.
node index.js