- Express.js — how to download base64 string as PDF file?
- 2 Answers 2
- Save base64 string as PDF at client side with JavaScript
- How to convert base64 data into PDF document in javascript using viewport?
- Let’s convert base64 into PDF using 3rd party library. In this article, you’ll get the different way to convert base64 into PDF/file.
- Let’s write the code:
- Support our IDKBlogs team
Express.js — how to download base64 string as PDF file?
I have pdf file encoded as base64 string. How to download this string to the browser as file in .pdf format? What I have already tried:
res.set('Content-Disposition', 'attachment; filename="filename.pdf"'); res.set('Content-Type', 'application/pdf'); res.write(fileBase64String, 'base64');
Yeah, thanks, updated the question 🙂 I can see the response status OK on the client side, but file is never being saved.
If that’s all you are writing, use res.end(fileBase64String, ‘base64’) , which will both write the data and close the response.
2 Answers 2
I ended up to decode the pdf first and then send it to the browser as binary as follows:
(For simplicity I use node-http here but the functions are available in express as well)
const http = require('http'); http .createServer(function(req, res) < getEncodedPDF(function(encodedPDF) < res.writeHead(200, < 'Content-Type': 'application/pdf', 'Content-Disposition': 'attachment; filename="filename.pdf"' >); const download = Buffer.from(encodedPDF.toString('utf-8'), 'base64'); res.end(download); >); >) .listen(1337);
What drove me nuts here was the testing with Postman:
I was using the Send Button instead of the Send and Download Button to submit the request:
Using the Send button for this request causes that the pdf file becomes corrupted after saving.
Save base64 string as PDF at client side with JavaScript
So here my problem: I have a pdf file as a base64 String that i am getting from the server. I would like to use this string to either display the PDF directly to the browser or give it a option of «Save as. » when clicking on a link. Here the code i am using:
Run Code
Its working fine with Chrome and Safari. Firefox does recognize the pdf but does not display it as FF requires extensions to be present but the data-URI has none in this case. The reason I’m insisting here, if chrome and safari get it to work, then there has to be a solution for FF and IE I know there are a few relevant questions to this but not really the exact one and now also a bit old ones. I know a workaround would be to have the pdf generated at server side but I would like to generate it at client side. So please intelligent folks, is it possible through some hacks or additional JS download plugins?
How to convert base64 data into PDF document in javascript using viewport?
Let’s convert base64 into PDF using 3rd party library. In this article, you’ll get the different way to convert base64 into PDF/file.
🙋♂️ Shubham Verma 🗓 August 21, 2020
ase64 data is sometime very useful and easy to convert into the file. In this post we’ll see how we can convert the base64 data into pdf using 3rd party library. In this we’ll use viewport of given scale. we’ll use the render method to show the pdf using viewport in the given canvas .
Let’s write the code:
After writing the above code, save the file and open this file into the browsser, and see the result.
In the result you can see the script code will be ran and the base64 will be converted into «Hello world» pdf as:
How to convert base64 data into PDF document in javascript using viewport?
Support our IDKBlogs team
Creating quality content takes time and resources, and we are committed to providing value to our readers. If you find my articles helpful or informative, please consider supporting us financially.
Any amount (10, 20, 50, 100, . ), no matter how small, will help us continue to produce high-quality content.
Thank you for your support!
If you found this article is helpful, then please share this article’s link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please subscribe us, You’ll get the monthly newsletter having all the published article of the last month.