JavaScript PDF Conversion: Converting Base64 Encoded Strings to PDF Files
By: Rajat Kumar | Last Updated: April 16, 2023
Introduction:
In this article we are going to learn how we can convert base64 encoding string data to PDF file and download them into your system. Base64 is a way to represent binary data in an ASCII string format. PDF files can be encoded in Base64 format for transmission over the internet. In order to convert a Base64 encoded string to a PDF file, you need to decode the Base64 string and convert it back to its original binary format.
Steps Convert base64 String to PDF File:
To convert a Base64 encoded string to a PDF file in JavaScript, you can follow these steps:
- Decode the Base64 string using the atob() function. This will return a binary string.
- Create a Uint8Array from the binary string using the Uint8Array() constructor.
- Create a Blob object from the Uint8Array using the Blob() constructor.
- Create a URL for the Blob using the URL.createObjectURL() method.
- Create an a element and set its href attribute to the URL of the Blob.
- Set the download attribute of the a element to the desired filename.
- Programmatically click the a element using the click() method.
Here's an example code snippet:
function downloadPdfFromBase64(base64String, fileName) {
const binaryString = atob(base64String);
const uint8Array = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
uint8Array[i] = binaryString.charCodeAt(i);
}
const blob = new Blob([uint8Array], { type: "application/pdf" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
Code Explanation:
- Decode the Base64 string using the atob() function. This function takes a Base64 encoded string as input and returns a binary string.
- Create a Uint8Array from the binary string using the Uint8Array() constructor. This constructor creates an array of 8-bit unsigned integers that represent the binary data. In this code, we create a new Uint8Array with the length of the binary string. Then we loop through the binary string and set each element of the array to the character code of the corresponding character in the binary string.
- Create a Blob object from the Uint8Array using the Blob() constructor. A Blob is a data structure that represents a file-like object of immutable, raw data. In this code, we create a new Blob with the Uint8Array as its data and a type of "application/pdf".
- Create a URL for the Blob using the URL.createObjectURL() method. This method creates a DOMString containing a URL representing the Blob object.
- Create an a element and set its href attribute to the URL of the Blob. This will create a hyperlink that can be clicked to download the PDF file.
- Set the download attribute of the a element to the desired filename. This attribute specifies that the target will be downloaded when a user clicks on the hyperlink. In this code, we set the download attribute to the desired filename.
- Programmatically click the a element using the click() method. This will simulate a user clicking the hyperlink, which will trigger the file download.
- Clean up by removing the a element and revoking the URL using the URL.revokeObjectURL() method.
In this code, we remove the a element from the document body and revoke the URL, which releases the resources associated with it. And that's it! You've successfully converted a Base64 encoded string to a PDF file in JavaScript.
You can call this function with the Base64 encoded string and the desired filename:
JavaScript code:
const base64String = "..."; // replace with your Base64 encoded string
const fileName = "example.pdf"; // replace with your desired filename
downloadPdfFromBase64(base64String, fileName);
Why technical SEO is a foundation to traditional on-page or off-page SEO Invoking Native Social Sharing with Vanilla JavaScript: A Comprehensive Guide Creating a Front-End Security Audit Checklist: Examples and Tips ES Modules in JavaScript: Organizing Code for the Modern Web How to Copy the contents of the clipboard in JavaScript? Steps for designing any dynamic website (for beginner). JavaScript Download: Handling Content-Disposition Headers for Attachments and Inlines Comparing MEAN Stack, MERN Stack, and Full Stack Development: Choosing the Right Technology for Your Project