Base64 to PDF

Base64 to PDF: paste a Base64 string, check for the %PDF- header and the %%EOF end marker, preview the pages and download the file. A string that starts with JVBERi is a PDF. Decoding runs in your browser and nothing is uploaded.

Base64 to PDF
591 bytes · %PDF-1.4 header found, %%EOF at the end
The file is built from your bytes in this browser and is not uploaded

How to decode a Base64 string to a PDF

Paste the Base64 string, with or without a data:application/pdf;base64, prefix. The tool decodes it, checks that the bytes begin with the PDF header, shows the version and the size, and draws the document with your browser's built-in PDF viewer so you can page through it. Download PDF saves the decoded file as document.pdf.

The preview is drawn from a temporary object URL created in your browser. Nothing is uploaded, and the file never leaves your device. If the bytes are not a PDF, the tool says what they are, or shows the first bytes, so you can see whether you are looking at a different file type, a truncated string or data that was encoded twice.

A PDF file starts with the five characters %PDF- followed by the version number. Those bytes are 25 50 44 46 2D, and Base64 turns them into JVBERi0 plus a few characters for the version. The prefix is so reliable that finding JVBERi at the start of a string is the usual way to tell a PDF from any other Base64 payload. The version characters follow it.

PDF header versions as Base64
HeaderBase64 of the first 8 bytes
%PDF-1.3JVBERi0xLjM=
%PDF-1.4JVBERi0xLjQ=
%PDF-1.5JVBERi0xLjU=
%PDF-1.6JVBERi0xLjY=
%PDF-1.7JVBERi0xLjc=
%PDF-2.0JVBERi0yLjA=

Version 1.4 (JVBERi0xLjQ) and 1.7 (JVBERi0xLjc) are by far the commonest in generated documents such as invoices and reports. PDF 2.0, published as ISO 32000-2, is rarer. A string that begins with JVBERi0xLjQK has a line feed right after the version, which is the usual layout. Some producers write a binary comment on the second line to mark the file as binary, so the first Base64 characters are the same and later ones vary.

Why a decoded PDF will not open

The decoding is exact, so a PDF that fails to open was already broken in the string. The causes are few, and the tool shows a signal for most of them.

Common reasons a Base64 PDF is damaged
CauseSign in this toolFix
String cut offNo %%EOF at the end of the fileGet the full string; many forms and logs truncate long values
No JVBERi at the startNo %PDF- headerRemove a prefix, a quote or a field name before the data
JSON escapesInvalid character where a slash or newline was escapedParse the JSON string to unescape it first
Plus signs turned into spacesInvalid character at the spacesReplace the spaces with +
Encoded twiceThe first bytes are another Base64 stringDecode the result again
Binary treated as textHeader present, but the PDF viewer failsThe bytes were converted through a text encoding such as UTF-8 before encoding
URL-safe alphabetAccepted by the decoderCheck the producer; other tools may reject - and _

The text-encoding case is the hardest to spot. If a producer read the PDF into a string with an ordinary character set and then Base64-encoded the string, bytes above 7F were rewritten and the file is damaged for good. The header and %%EOF can still look right. When the same string opens in no viewer, ask for the original binary and an encoding of that.

What is inside a PDF file

A PDF is a header, a body of numbered objects, a table of their positions and a trailer. The objects hold the page tree, the pages, fonts and the content streams that draw text and graphics. The table, called xref, lists the byte offset of each object so that a reader can jump to any object without scanning the file, and the trailer names the root object. The last lines of the file are startxref, an offset, and %%EOF.

Structure of a PDF file%PDF-1.4: header: 25 50 44 46 2D in hex, JVBERi0 in Base64; 1 0 obj … endobj: body: numbered objects (catalog, pages, page, fonts, content); xref: table of byte offsets, one line per object; trailer << /Root 1 0 R >>: points at the catalog; /Size counts the objects; startxref 408 · %%EOF: where the xref starts, then the end marker%PDF-1.4header: 25 50 44 46 2D in hex, JVBERi0 in Base641 0 obj … endobjbody: numbered objects (catalog, pages, page, fonts, content)xreftable of byte offsets, one line per objecttrailer << /Root 1 0 R >>points at the catalog; /Size counts the objectsstartxref 408 · %%EOFwhere the xref starts, then the end marker
A PDF is read from the end: the reader finds %%EOF, then startxref, then the xref table. A Base64 string cut short loses exactly these last lines, which is why truncated PDFs fail to open.

A reader works from the end, which explains how a damaged string fails. If the end is cut off, it cannot find startxref, and many viewers fall back to scanning for objects, which sometimes works and shows a partial document. The example loaded in the tool is a complete one-page file of 591 bytes with five objects, which is why it opens at once and why it is a good test that your own decoding chain preserves the bytes.

The sample file as text

The PDF loaded in the tool is small and uncompressed, so decoding its Base64 gives text you can read. This is the whole file, 591 bytes. Larger documents look the same in outline, but their content streams are compressed with Flate and appear as binary noise between the stream and endstream lines.

The sample PDF after Base64 decoding
%PDF-1.4
1 0 obj
<< /Type /Catalog /Pages 2 0 R >>
endobj
2 0 obj
<< /Type /Pages /Kids [3 0 R] /Count 1 >>
endobj
3 0 obj
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 240 120] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>
endobj
4 0 obj
<< /Length 47 >>
stream
BT /F1 18 Tf 20 60 Td (Hello from Base64) Tj ET
endstream
endobj
5 0 obj
<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>
endobj
xref
0 6
0000000000 65535 f 
0000000009 00000 n 
0000000058 00000 n 
0000000115 00000 n 
0000000241 00000 n 
0000000338 00000 n 
trailer
<< /Size 6 /Root 1 0 R >>
startxref
408
%%EOF

Object 4 holds the drawing instructions. BT begins a text block, /F1 18 Tf selects font F1 at 18 points, 20 60 Td moves to the position, and the text in parentheses is shown by Tj. The xref table lists the offset of each of the five objects, 9 for the first, and startxref gives the offset of the table itself, 408. If any byte is added or removed before these offsets, the numbers no longer match and a strict reader repairs the file or refuses it, which is why re-encoding a PDF through anything that changes bytes damages it.

Checking a decoded PDF from the command line

After saving the file, a few commands confirm it. head -c 8 document.pdf prints the header, and tail -c 16 document.pdf should end with %%EOF. On Linux and macOS, file document.pdf reports the PDF version and, for many files, the page count. The poppler tool pdfinfo prints the title, producer, page size and number of pages, and qpdf --check document.pdf validates the structure and lists problems. A file that passes qpdf and still looks wrong in a viewer is a rendering issue, not a transport one.

Comparing with the sender's numbers is the quickest transport check. A sender that reports a file size or a checksum lets you compare the byte count shown under the input here, and a SHA-256 of the saved file, with theirs. The file as saved is the file as it was, because Base64 is exact; any difference was introduced before encoding or in the copying of the string.

Size limits around Base64 PDFs

A PDF grows by a third as Base64, and many systems limit what they accept. API gateways commonly cap payloads at several megabytes (10 MB is a familiar figure, and synchronous serverless calls are often limited to about 6 MB), form fields and web server settings have their own ceilings, and email gateways reject attachments past 10 to 25 MB, a limit that applies to the encoded size. A 7 MB PDF becomes about 9.3 MB of Base64 and may pass one limit and fail another.

If a document does not fit, compress the PDF with a tool that recompresses images, split it, or move to a binary upload, which carries the file with no growth. When a limit fails silently, the symptom is a truncated string: the decoder here shows the header but no %%EOF at the end, which is the signal to check the size limit between you and the sender.

Signed, encrypted and linearized PDFs

Base64 preserves every byte, so it is safe for PDFs that carry digital signatures: the signature covers specific byte ranges, and decoding returns them exactly. Anything that edits the bytes, such as a text conversion or a tool that rewrites line endings, breaks the signature. Encrypted PDFs have an /Encrypt entry in the trailer, and the preview asks for the password through the browser viewer. Linearized files, arranged for fast web viewing, begin with the same %PDF- header and carry a hint table at the front; their Base64 starts the same way.

PDFs from APIs: invoices, labels and reports

Many web services return documents as Base64 inside JSON, because JSON cannot hold bytes. A shipping API returns a label as a field named something like content or labelData, a billing system returns an invoice, a document service returns a signed contract. The field holds the Base64 of the whole file, and the response usually names the media type, application/pdf, in a separate field or header.

Extract the value of the field, not the whole response, and look at the first characters: if it begins with JVBERi it is a PDF. Many APIs wrap the Base64 across lines or escape slashes, both of which are handled by the decoder or are easy to fix. Large documents can reach several megabytes of text, which is normal: a 1 MB PDF is a 1.33 MB string. If the same API offers a download URL as well as a Base64 field, the URL is more efficient, and the Base64 is for systems that cannot make a second request. For other file types, see Base64 to image, and for the general decoder, Base64 decode.

Opening a PDF from an unknown source

PDFs are programs as well as documents. The format allows JavaScript, links, embedded files and form actions, and viewers have had security flaws. A file that arrives as Base64 from a source you do not trust deserves the same care as any attachment. The preview in this tool uses the viewer built into your browser, which runs in a sandbox and ignores most active features, but a downloaded file opened in a desktop reader has more power.

A PDF can also hide content that the preview does not show, such as attached files and form actions, so the rule is the same as for any attachment. Sensible habits are to check the source before opening, keep the reader updated, and use a browser viewer or an isolated environment for documents of unknown origin. If you are building an application that accepts Base64 PDFs, check the header and limit the size before saving the file, and serve it back with the correct content type and a download disposition so that it is not interpreted as a page.

Base64 to PDF in Python, JavaScript, PHP and the shell

Python and Node.js
import base64
open("document.pdf", "wb").write(base64.b64decode(b64))

require("fs").writeFileSync("document.pdf", Buffer.from(b64, "base64"));
PHP, command line and PowerShell
file_put_contents("document.pdf", base64_decode($b64));

base64 -d pdf.txt > document.pdf
[IO.File]::WriteAllBytes("document.pdf", [Convert]::FromBase64String($b64))

The same applies to every language: write the bytes to a file opened in binary mode, never through a text stream, which can rewrite line endings and corrupt the PDF. Check the result by looking at the first bytes: head -c 5 document.pdf should print %PDF-. In a browser, you can build a link to the decoded file with a Blob: create a Blob from the bytes with the type application/pdf, and use URL.createObjectURL as the address of an object or link element, as this tool does.

A PDF split across several Base64 pieces

Large PDFs sometimes arrive in parts: several JSON fields, several messages, or several lines pasted from a log. The rule for putting them back together is simple but easy to break. Join the strings first, as text, and decode the result once. Concatenating the strings is always safe; concatenating the decoded bytes of each piece is safe only if no piece ends in padding and each piece, except the last, has a length that is a multiple of four characters.

The reason is the three-bytes-to-four-characters grouping. A cut in the middle of a four-character group leaves a fragment that stands for a fraction of a byte, and decoding the fragment alone drops those bits. The table cuts the sample PDF at three positions and compares the pieces decoded separately with the whole string decoded once.

Cutting the sample PDF's Base64 string (length shown in characters)
Cut afterMultiple of 4First piece decodesPieces decoded separately and joinedSame as decoding the whole
100yescleanly591 bytes (whole: 591)yes
101nowith an error590 bytes (whole: 591)no
102nocleanly590 bytes (whole: 591)no

A cut at 100 characters is safe, because 100 is a multiple of four. A cut at 101 breaks the first piece, since a length of 4n + 1 cannot come from whole bytes. A cut at 102 decodes, but the last six bits of the first piece are discarded, and the pieces do not rebuild the file. When you are the one splitting a string, choose a piece size that is a multiple of four, such as 1,024 or 76 characters per line, which is why MIME wraps at 76. When you receive pieces, join the text, strip any whitespace, and decode once.

Showing a decoded PDF inside a web page

If you build a page that displays a PDF received as Base64, decode it in the browser and give the viewer a blob URL. The steps are the ones this tool follows: decode to a byte array, wrap it in a Blob with the type application/pdf, call URL.createObjectURL, and use the result as the source of an object or iframe element. Release it with URL.revokeObjectURL when the page no longer needs it.

A data: URL looks simpler, because the Base64 goes straight into the address, but it has limits. The URL carries the whole file, so a 5 MB PDF is a URL of more than 6.6 million characters, and browsers cap or slow down on very long ones. Chromium-based browsers also block navigating the top-level window to a data: URL, so a link that opens the PDF in a new tab can end up blank. A blob URL avoids both problems, and it also lets the user download the file with a normal file name.

JavaScript: Base64 string to a PDF blob URL
const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
const url = URL.createObjectURL(new Blob([bytes], { type: "application/pdf" }));
// <iframe src={url}> or <a href={url} download="document.pdf">
// URL.revokeObjectURL(url) when you are done

Questions about Base64 and PDFs

How do I know a Base64 string is a PDF?

It begins with JVBERi, which is the Base64 form of %PDF-. The decoder checks the same thing on the decoded bytes, reports the PDF version from the header and checks that the file ends with %%EOF.

Why does the PDF download but show a blank page or an error?

The Base64 was probably cut off, so the cross-reference table at the end of the file is missing, or the bytes were altered on the way, for example by a text encoding conversion. Compare the length with what the sender reports and check that the tool finds %%EOF at the end.

Is the PDF uploaded to a server?

No. The string is decoded in your browser and the PDF is shown from a local object URL by the browser's own PDF viewer. Nothing is sent to a server.

Can I decode a very large PDF?

The limit is browser memory. A PDF of several megabytes decodes at once. Files above a few hundred megabytes are better decoded with the command line, where base64 -d streams the data to disk.

Does the tool read the text inside the PDF?

No. It decodes the bytes, checks that they form a PDF and hands them to the browser's viewer. Text extraction is a separate job that needs a PDF library, because the text in a PDF is stored in compressed streams with font encodings.