Upload a file
  1. base64hub
  2. File tools
  3. Base64 to PDF

Base64 to PDF

Paste the Base64 from an API response, an e-invoice or an email attachment, and the PDF opens on this page with a download button. Decoding and preview happen in your browser.

Free no sign-up, no limits Local nothing uploaded Checks version, pages, title, encryption Updated Oct 27, 2026
Input –Output –Size –

Processed in your browser. This page cannot send your data anywhere – how we know

How to decode Base64 to PDF

  1. Paste the string into Base64 or data URI. A full data:application/pdf;base64,… URI works, and so do line breaks, surrounding quotes, URL-safe Base64 (- and _) and missing = padding. For a long string saved in a file, use Open .txt file or drop the file on the box; anything over 4 MB is decoded without being shown in the text area.
  2. The preview opens below the buttons. Decoding starts as you paste; untick Live if you would rather press Decode yourself.
  3. Check File name. It starts as decoded, but takes the name from a name= parameter in the data URI or from the text file you opened: invoice.pdf.b64.txt becomes invoice.pdf.
  4. Press Download file. The link open it in a new tab under the preview shows the same PDF full-size.

A sample PDF is loaded when the page opens, so you can see what a working result looks like before pasting your own.

Where Base64 PDFs come from

JSON has no way to carry raw bytes, so services that return documents over a JSON API send them as a Base64 string, often in a field called something like content, document, file or label.

  • Label and logistics APIs return the printable shipping label as a Base64 PDF. The same request can often produce ZPL or PNG instead, depending on a format parameter, so a “PDF” field is not always a PDF (see the table further down).
  • E-signature services return the signed document, and frequently a separate audit certificate, as Base64 in the completion response or webhook.
  • E-invoices in the UBL XML format embed a human-readable PDF copy in an element such as <cbc:EmbeddedDocumentBinaryObject mimeCode="application/pdf" filename="INV-1001.pdf">. Copy the text between the tags; the filename attribute tells you what to type in File name.
  • In the raw source of an email, a PDF attachment is a MIME part with Content-Transfer-Encoding: base64 and lines of 76 characters. Paste just the encoded block, or open the whole message in the EML decoder, which lists every attachment. Base64 in email explains the structure.
  • Documents stored in database BLOB columns are often exported as text. PostgreSQL’s encode(data, 'base64') and MySQL’s or MariaDB’s TO_BASE64() both insert a line break after every 76 characters; the decoder skips them.

For a JSON response with several encoded fields, decode Base64 inside JSON finds and decodes them all at once. To go the other way, from a PDF file to a string, use PDF to Base64.

What the preview shows

FieldWhere it comes from
PDF versionThe header line, %PDF-1.7 for example
PagesCounted from the file’s page objects (see below)
TitleThe document information title, when it is stored as plain text
EncryptedPresent when the file has an encryption dictionary: a password to open, or only permission restrictions
SHA-256Hash of the decoded bytes. Compare it with sha256sum invoice.pdf on the sender’s side to prove the file arrived intact

The preview itself is your browser’s PDF viewer, showing the decoded bytes from a local blob: URL. Chrome, Edge, Firefox and desktop Safari display it inline. It stays blank when the browser is set to download PDFs instead of opening them (Chrome: Settings, Privacy and security, Site settings, Additional content settings, PDF documents; Firefox: Settings, General, Applications, Portable Document Format), and many mobile browsers cannot render a PDF inside a page at all. In those cases open it in a new tab or Download file still work, and the info list above the preview is filled in either way.

How the page count works

The tool does not render the PDF to count pages. It counts the page objects (/Type /Page) in the first 32 MB of the file. If none are visible, it reads the /Count entry of the page tree, and if that is hidden too, it shows unknown. Two situations make the number differ from the viewer’s:

  • Too many pages. A PDF that was edited, annotated or signed after it was created is often saved incrementally: changed pages are appended as new objects and the old versions remain in the file. In a test file with 2 pages and one rewritten page, the count is 3.
  • Unknown. Since PDF 1.5, objects can be packed into compressed object streams, and then neither the page objects nor the page tree can be read without decompressing them.

When the two disagree, the viewer’s page count is the right one.

When the result is not a PDF

If the decoded bytes are something else, a note above the fields says what was found, for example “This looks like HTML, not PDF.”, with a Download as .html button. The beginning of the Base64 often explains what happened:

Base64 starts withWhat you have
JVBERi0A PDF (%PDF-)
SlZCRVJpMHA PDF encoded twice: the Base64 of JVBERi0…
H4sIgzip-compressed data
PCFET0NUWVBF, PCFkb2N0eXBl, PGh0bWwAn HTML page, usually an error or login page
eyJJSON, often an error message such as {"error":…
XlhBA ZPL label (^XA) for thermal printers
iVBORw0KGgoA PNG image

For a double-encoded string or gzip data, the tool shows a second note with Decode again, which replaces the input with the inner layer; Back returns to what you pasted. For a damaged or unrecognized file, the note reads “This doesn’t look like PDF” and names the detected type, and the extension stays .pdf so you can still download the bytes and try another reader.

Errors before decoding have their own causes:

  • Escapes from raw JSON, such as \/ (PHP’s json_encode writes slashes this way) or a literal \n between lines, are removed automatically. If another \ escape still triggers Invalid character \, use Repair Base64, or parse the JSON first and copy the value.
  • Invalid character % means the string was URL-encoded. The escapes for +, / and = (%2B, %2F, %3D) are fixed automatically; for anything else, run the string through the URL decoder first.
  • A PDF that is detected but shows nothing is usually truncated. The header survives, so detection succeeds, but the end of the file, where the cross-reference table lives, is missing. See Base64 errors for the other usual suspects.

Decode Base64 to PDF in code

Python, from a JSON response:

import base64, json

with open("response.json") as f:
    b64 = json.load(f)["document"]

if b64.startswith("data:"):
    b64 = b64.split(",", 1)[1]

pdf = base64.b64decode(b64)
if not pdf.startswith(b"%PDF-"):
    raise ValueError(f"not a PDF, starts with {pdf[:16]!r}")

with open("document.pdf", "wb") as f:
    f.write(pdf)

b64decode silently drops characters outside the standard alphabet, so a URL-safe string with - and _ decodes to garbage without an error. Use base64.urlsafe_b64decode for those. Node.js accepts both alphabets in one call:

import { readFile, writeFile } from "node:fs/promises";

const { document } = JSON.parse(await readFile("response.json", "utf8"));
const pdf = Buffer.from(document.replace(/^data:[^,]*,/, ""), "base64");

if (pdf.toString("latin1", 0, 5) !== "%PDF-") throw new Error("not a PDF");
await writeFile("document.pdf", pdf);

In a shell, jq -r matters: without -r the value keeps its quotes and base64 -d stops with “invalid input”.

jq -r '.document' response.json | base64 -d > document.pdf

The key document is a placeholder for whatever your API calls the field. More patterns are on Base64 in Python, Base64 in Node.js and Base64 in Bash.

FAQ

How do I convert a Base64 string to a PDF file?

Paste the string into the box above, check the preview and press Download file. From JSON, copy only the value between the quotes; a data:application/pdf;base64, prefix and line breaks can stay.

Why does the decoded PDF open as damaged or blank?

Almost always the string was cut short by a log line limit, a database column or a copy-paste limit, so the end of the file is missing. A complete PDF ends with %%EOF; compare the decoded size with what the sender reports.

Can I view a password-protected PDF?

Yes. The info list shows Encrypted: yes, and the built-in viewers of Chrome and Firefox ask for the password inside the preview. The downloaded file is unchanged and opens with the same password.

Why does the tool say my Base64 is not a PDF?

The decoded bytes do not start with %PDF-. Most often the API sent an error page or a JSON message instead of the document; the warning names what it found and offers to save it with that extension so you can read it.

Is it safe to decode a confidential PDF here?

The PDF is decoded and displayed inside your browser, and the page’s Content Security Policy blocks it from making network requests other than Cloudflare’s page-view count. You can even go offline after the page has loaded and keep using it.