How to Convert Base64 to PDF in JavaScript Without Blank Pages
Embedding massive data:application/pdf;base64 strings directly in an iframe or embed tag triggers silent browser truncation above ~2 MB. Learn the clean Uint8Array + Blob URL pattern and proper memory cleanup.
Why Chrome & Safari Blank Out Large data:application/pdf Iframes
Convert Base64 into a Uint8Array and Blob URL instead of an inline Data URI so multi-megabyte PDFs render reliably across all desktop and mobile browsers.
Read full technical details
Front-end teams commonly wire carrier or billing API strings straight into <iframe src="data:application/pdf;base64,..."> because one-page sandbox receipts render fine in early testing. Once production returns a 5-page commercial invoice or high-DPI thermal label exceeding roughly 2 MB, Chromium and Edge hit internal URL navigation character caps and paint a completely blank white frame with zero JavaScript exceptions.
Materializing raw octets into a Uint8Array, wrapping them inside new Blob([bytes], { type: "application/pdf" }), and binding URL.createObjectURL(blob) bypasses URL-length limits entirely. The browser reads the PDF stream directly from an internal memory handle, supporting smooth scrolling and native print dialogs even on 50 MB+ documents.
Before calling window.atob(), always strip wrapping JSON quotes, unescape \/ forward slashes, collapse whitespace, normalize URL-Safe -/_ characters, and restore missing = padding; otherwise DOMException: InvalidCharacterError breaks rendering before bytes are ever built.
Preventing Blob Memory Leaks & Main-Thread Stalls in SPAs
Revoke superseded Object URLs deterministically and offload 10 MB+ quartet decoding into Web Workers so operator consoles never leak RAM or freeze.
Read full technical details
Every URL.createObjectURL(blob) call pins the underlying PDF Uint8Array buffer in browser memory until the page unloads or URL.revokeObjectURL(url) runs. Long-lived warehouse or support consoles inspecting hundreds of shipping labels per shift steadily accumulate hundreds of megabytes of orphaned PDF buffers unless every React/Preact/Vue effect cleanup tracks and revokes prior handles.
On documents larger than 5–10 MB, calling atob() plus a synchronous charCodeAt loop on the main UI thread drops animation frames. Streaming 1 MiB quartet blocks inside an isolated module Web Worker keeps clicks responsive while validating the leading 5 magic bytes (%PDF- -> [0x25, 0x50, 0x44, 0x46, 0x2D]) before mounting the viewer.
If you need to inspect a carrier response immediately—or rotate an upside-down 4x6 label modulo 360 and copy a PNG snapshot—paste the raw API JSON straight into Base64Doc without writing boilerplate.
Production Reference Code
export function base64ToPdfBlobUrl(rawInput) {
const clean = rawInput
.replace(/^data:application\/pdf;base64,/i, '')
.replace(/\\\//g, '/')
.replace(/\s+/g, '')
.replace(/-/g, '+')
.replace(/_/g, '/');
const padded = clean + '='.repeat((4 - (clean.length % 4)) % 4);
const binary = atob(padded);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
const blob = new Blob([bytes], { type: 'application/pdf' });
return URL.createObjectURL(blob); // Remember URL.revokeObjectURL(url) on unmount!
}Step-by-Step Local Workflow
Strip Data URI, Quotes & Escapes
Remove data:application/pdf;base64, headers, quotes, whitespace, and JSON \/ slashes, and normalize URL-safe -/_ characters before decoding.
Decode Into Uint8Array Buffer
Convert the cleaned ASCII payload into an unsigned 8-bit byte array (offloading 10 MB+ buffers into a Web Worker).
Check %PDF- Leading Octets
Confirm bytes 0..4 equal 0x25 0x50 0x44 0x46 0x2D before passing the buffer into an iframe or PDF.js canvas.
Mount & Revoke Blob URL Cleanly
Build a Blob with type application/pdf, render via URL.createObjectURL(blob), and invoke URL.revokeObjectURL on unmount.
Test Directly in Browser Workbench
Frequently Asked Questions
Why does my Base64 PDF iframe render blank even though the Base64 string is valid?
Yes, Chrome and Edge enforce URL length caps on data:application/pdf;base64 URIs above ~2 MB. Switching to a Blob URL via URL.createObjectURL resolves the blank frame immediately.
Does JavaScript atob() handle URL-Safe Base64 with - and _ characters?
No, window.atob() throws InvalidCharacterError on - and _ and requires divisible-by-4 length. Replace - with +, _ with /, and pad missing = before calling atob().
How do I verify a decoded Uint8Array is actually a PDF before saving?
Check that bytes 0..4 equal [0x25, 0x50, 0x44, 0x46, 0x2D] (%PDF-). Base64Doc verifies 66 binary signatures automatically before allowing download.
When should I move JavaScript Base64 decoding into a Web Worker?
Move decoding to a Web Worker whenever payloads exceed 5–10 MB or when scanning large API JSON responses with multiple embedded documents.
What happens if I forget to call URL.revokeObjectURL()?
The decoded PDF buffer stays pinned in browser RAM until the browser tab reloads, causing progressive memory growth in single-page dashboards.