Base64 encode/decode · Guide
Decode Base64 to PDF: Recovering a Document From a Payload
PDFs arrive Base64-encoded constantly — from invoicing APIs, e-signature platforms, report generators — because JSON has no binary type. Recovering the file is simple once you know what to strip and how to check the result.
Strip the prefix first
If the string starts with data:application/pdf;base64,, everything up to and including the comma is a data URI wrapper, not part of the payload. Decoding with it included produces a corrupt file.
A correct PDF payload begins with JVBERi0, which is Base64 for %PDF-. That prefix is the quickest way to confirm you have a PDF before decoding anything.
Decoding
# command line
base64 -d payload.b64 > document.pdf
# from a JSON field
jq -r '.pdf' response.json | base64 -d > document.pdf// browser — Blob, not a data URI
const bytes = Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
const url = URL.createObjectURL(new Blob([bytes], { type: 'application/pdf' }));
window.open(url);
URL.revokeObjectURL(url);Verifying the result
A valid PDF starts with the bytes %PDF- and ends with %%EOF. Check both — a file that opens the first page and then errors is usually truncated at the end:
head -c 8 document.pdf # expect %PDF-1.x
tail -c 8 document.pdf # expect %%EOFTruncation is the most common failure, and it usually happens upstream — a JSON string field cut to a column width, or a log line clipped. Compare the payload length against the expected file size times 1.33.
Frequently asked questions
How do I know the payload is a PDF?
It starts with JVBERi0, which decodes to %PDF-.
The PDF opens blank — why?
Usually the data URI prefix was included in the decode, or the payload is incomplete.
Can I open the PDF without saving it?
Yes. Create a Blob with type application/pdf and open the object URL in a new tab.
Ready to try it?
Open the free browser-based Base64 encode/decode and apply what you just read — no sign-up, runs locally.
Open the Base64 encode/decode tool