How to Embed a PDF as Base64 in JSON, HTML or an API Request

What Base64 encoding does to a PDF, why it adds about a third to the size, how data URIs work, and how to encode or decode PDFs in a browser or terminal.

Updated September 29, 2026 5 min read

Use the free PDF to Base64Encode a PDF as Base64 or data URI, or decode Base64 back to PDF

Developers meet Base64-encoded PDFs all the time. An e-signature API asks you to send the document as a Base64 string inside JSON. An invoicing system returns the invoice as a long block of letters and numbers. A test fixture needs a small PDF embedded directly in the code. Or a support ticket contains a string starting with JVBERi0 and someone asks what it is.

This guide explains what Base64 is, why it is used for PDFs, the size overhead and data URI format, how to encode and decode PDFs in the browser, and the equivalent commands on Windows, macOS and Linux.

What Base64 does

A PDF is binary data: a sequence of bytes, many of which are not printable characters. Formats such as JSON, XML, HTML and email were designed for text and can mangle or reject raw binary. Base64 solves this by representing every 3 bytes of binary data as 4 characters from a safe alphabet: A-Z, a-z, 0-9, + and /, with = used as padding at the end.

That has two consequences:

  • The encoded text is about 33% larger than the original file (4 characters for every 3 bytes), plus a little more if line breaks are added.
  • Nothing is compressed or encrypted. Base64 is an encoding, not security. Anyone can decode it.

Recognising a Base64 PDF

Every PDF starts with the bytes %PDF-. Encoded in Base64, that beginning becomes JVBERi0. If you see a string starting with those characters, it is almost certainly a PDF.

Data URIs

A data URI packs the file type and the Base64 data into a single string:

data:application/pdf;base64,JVBERi0xLjcK...

This can be used in an HTML <iframe> or <embed> to show a PDF without a separate file. Be aware that many modern browsers block opening data: URLs directly in the address bar for security reasons, and very long data URIs are unwieldy, so use them for small documents.

Line wrapping

Email (MIME) traditionally wraps Base64 at 76 characters per line. JSON APIs almost always expect a single unbroken line. Sending wrapped Base64 to an API that does not expect it is a common cause of "invalid document" errors.

Encode or decode a PDF in your browser

Our PDF to Base64 tool works in both directions and runs entirely in your browser, which is important if the PDF contains personal data.

PDF → Base64

  1. Open PDF to Base64 and stay on the PDF → Base64 tab.
  2. Drop a PDF onto the upload area (up to 50 MB). The tool shows the original size and the length of the encoded text.
  3. Turn on Data URI prefix (data:application/pdf;base64,) if you are embedding the result in HTML.
  4. Turn on Wrap lines at 76 characters (MIME) only if you need email-style line breaks.
  5. Click Copy Base64, or Download .txt to save the string as a file.

If the file does not start like a PDF, you are warned, but it is still encoded.

Base64 → PDF

  1. Switch to the Base64 → PDF tab.
  2. Paste the Base64 text or a full data:application/pdf;base64,… URI. Whitespace and line breaks are ignored, and URL-safe Base64 (using - and _) is accepted.
  3. The tool checks that the text is valid Base64 and that it decodes to a real PDF. If it does, you see the file size, page count and thumbnails of the first pages (up to 12).
  4. Click Download PDF to save it.

Clear error messages tell you if the text contains invalid characters, or if it decodes correctly but is not a PDF.

Encoding and decoding on the command line

Linux (GNU coreutils)

base64 -w 0 invoice.pdf > invoice.b64
base64 -d invoice.b64 > invoice.pdf

-w 0 disables line wrapping, which is what most APIs want.

macOS

base64 -i invoice.pdf -o invoice.b64
base64 -D -i invoice.b64 -o invoice.pdf

Recent macOS versions also accept -d for decoding.

Windows PowerShell

[Convert]::ToBase64String([IO.File]::ReadAllBytes("C:\docs\invoice.pdf"))
[IO.File]::WriteAllBytes("C:\docs\out.pdf", [Convert]::FromBase64String((Get-Content C:\docs\invoice.b64 -Raw)))

Command-line tools are the better choice inside scripts and build pipelines. The browser tool is quicker for one-off checks, especially the preview of decoded pages.

Tips and pitfalls

A typical JSON payload

Most document APIs expect the encoded file as a string field alongside a file name and type. The exact field names vary by provider, but the shape usually looks like this:

{
  "fileName": "invoice-1042.pdf",
  "contentType": "application/pdf",
  "content": "JVBERi0xLjcK..."
}

When a request fails, first check that content is one continuous line without a data URI prefix, and that nothing was cut off when the string was copied.

Mind request size limits

A 10 MB PDF becomes roughly 13.3 MB of Base64 text. APIs and web servers often cap request bodies. Compress large documents with Compress PDF before encoding.

Strip prefixes when an API wants raw Base64

Some APIs want only the Base64 characters, without data:application/pdf;base64,. Others require it. Read the API documentation and toggle the prefix accordingly.

Don't treat Base64 as protection

Encoded PDFs are as readable as the original to anyone who decodes them. Use proper encryption or Protect PDF if confidentiality matters.

Other file types

For images, use Image to Base64 or Base64 to Image. For general text and files, the Base64 Converter handles plain strings.

Frequently asked questions

Why is my Base64 string bigger than the PDF?

Base64 uses 4 characters to represent every 3 bytes, so the text is about one third larger than the original file.

How do I tell if a Base64 string is a PDF?

PDF data encoded in Base64 starts with JVBERi0, which decodes to %PDF-.

Should I include the data URI prefix?

Include it when embedding in HTML or CSS. Leave it out for most JSON APIs unless their documentation says otherwise.

Why does my decoded PDF not open?

The string may be truncated, contain extra characters, or be a different file type. The decoder checks for a valid PDF header and reports which problem it found.

Is Base64 encryption?

No. It is a reversible encoding for moving binary data through text formats and provides no security.

Try it now: PDF to Base64

Loading tool…