Jacob Lehoux
Jacob Lehoux

Reputation: 15

Turn base64 Response into PDF

Let me start by saying I am pretty new to the development world. I am working on a project to create a PDF on a button click.

I managed to set up the back end using Node.js and got the response in my front end as a base64 object after I used JSON.Parse():

Response from server:

image - response from server.

My question is how do I get that response to be a PDF that opens up in a new tab?

Upvotes: 1

Views: 3769

Answers (2)

Daphoque
Daphoque

Reputation: 4678

With an anchor:

<a download="Title" href="data:application/pdf;base64,JVBERi0xL...." title='Download pdf document' />

In a new Tab

window.open("data:application/pdf;base64,JVBERi0x..");

Upvotes: 2

enno.void
enno.void

Reputation: 6579

If you want that Node serve a pdf file directly you have to create a pdf file with node and serve it from your service. Tools you might want to use for this are pdfkit and express.js (for the webserver)

pdfkit: http://pdfkit.org/

If you want to serve the pdf from a clientapp you could use jsPDF for this. https://parall.ax/products/jspdf

Here an example how to add an image:

var doc = new jsPDF();
var imgData = 'data:image/jpeg;base64,YOUR_BASE64STR_HERE');
doc.addImage(imgData, 'JPEG', 15, 40, 180, 160);

Upvotes: 0

Related Questions