How to Convert Base64 to PDF in Node.JS?

Oct 11, 2022

.

Admin

How to Convert Base64 to PDF in Node.JS?

Hey Developer,

This tutorial will provide an example of how to convert base64 to pdf in node js. I explained simply about node js convert base64 to pdf. if you want to see an example of node js convert base64 string to pdf then you are in the right place. We will use node js decode base64 pdf. follow the below example for node.js convert base64 to pdf.

In this example, i will take one "pdfString" variable with base64 string and then i will convert it into pdf using "fs" library. so let's see the example code below:

Step 1: Install Node JS


This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:

mkdir my-app

cd my-app

npm init

Step 2: Create server.js File

You can take the below code and put into "server.js" file.

server.js

const fs = require("fs");

// Create Variable with Base64 PDF String

var pdfString = "JVBERi0xLjMNCiXi48/TDQoNCjEgMCBvYmoNCjw8DQovVHlwZSAvQ2F.....";

// Store PDF into Server

fs.writeFile("sample.pdf", pdfString, 'base64', function(err) {

console.log(err);

});

console.log("PDF Saved Successfully.");

Run Node JS:

All steps have been done, now you have to type the given command and hit enter to run the node.js app:

node server.js

Output:

#Node JS