Convert Base64 to PDF in JavaScript
Do you have a Base64 string and do not know how to convert it to PDF using JavaScript? Do not worry! I am here to help you. I will show you some practical examples how to decode Base64 to PDF using the atob function and get some information about it.
function base64ToBlob(base64) { const binaryString = window.atob(base64); const len = binaryString.length; const bytes = new Uint8Array(len); for (let i = 0; i < len; ++i) { bytes[i] = binaryString.charCodeAt(i); } return new Blob([bytes], { type: 'application/pdf' }); };
const blob = base64ToBlob(base64); const blobURL = URL.createObjectURL(blob); const link = document.createElement('a'); //link.setAttribute('target', '_blank'); link.setAttribute('download', `${bond.p_symbol}-OC.pdf`) link.href = blobURL; document.body.appendChild(link); link.click(); document.body.removeChild(link);