Skip to main content
PDFBase

PDF to Base64 Converter

Local

Encode a PDF file as a Base64 string for APIs, JSON, and embedding

All Tools
PDF to Base64
Local
Tool Details
Input formatsPDF
Output formatBASE64
Max file size50 MB
Max files1
ProcessingLocal

Privacy First

Your files are processed entirely in your browser. Nothing is uploaded to any server.

How do I convert a PDF to a Base64 string?

Select the PDF, choose the output shape you actually need — raw Base64, a data URI, a JSON envelope, XML, an HTML embed, object, iframe or download link, or a CSS url() value — and encode. You get back a file already in that form, ready to paste straight into the destination. Encoding is done by your browser, so the PDF never reaches a server.

About the PDF to Base64 Converter

Convert PDF files to Base64-encoded strings directly in your browser and take the output in the shape the destination actually expects: raw Base64, a data URI, a JSON envelope, XML, a ready-to-paste HTML embed, object, iframe or download link, or a CSS url() value. Raw output can be wrapped at 76 or 64 characters per line for MIME and PEM-style transports.

  • Convert a PDF to Base64 before sending it in a JSON API request, and take the output already wrapped in the JSON envelope so there is nothing left to hand-assemble.
  • Produce a data:application/pdf;base64 URI for an embed, an email template or a test fixture, without pasting the prefix on yourself and getting the MIME type wrong.
  • Get a working HTML embed, object, iframe or download link for the PDF, so the page renders it without any server route or object URL bookkeeping.
  • Wrap raw output at 76 or 64 characters for a MIME transport, a PEM-style block, or a system that rejects lines longer than a few hundred bytes.
  • Check before you send whether the encoded string will fit a request-body limit, since Base64 grows the payload by roughly a third.

How to use the PDF to Base64 tool

  1. 1

    Upload PDF file

    Select PDF file from your device or drag file into the upload area.

  2. 2

    Choose settings

    Choose settings such as Output format, Line wrapping before processing.

  3. 3

    Convert to BASE64

    Run the PDF to Base64 tool to create the BASE64 output.

  4. 4

    Download the result

    Download the finished file. The original file stays on your device and is not uploaded.

Why is the Base64 string bigger than the PDF?

Because Base64 spends four characters to represent every three bytes, so the encoded form is about a third larger than the original file — that is arithmetic, not overhead you can tune away. The exact encoded length is ceil(bytes / 3) × 4. Plan for it when the payload has to fit inside a request-size limit, a column width, or a message-queue cap. A 6 MB PDF becomes roughly 8 MB of text, which is enough to trip default body-size limits on plenty of API gateways.

Which output format should you pick?

Raw Base64 for a JSON field or database column where the receiving code adds its own prefix. Data URI when the string goes into an iframe or embed src, an anchor download attribute, or an API field documented as taking a data URI. The JSON and XML envelopes when you want the filename, MIME type and byte length travelling alongside the payload rather than assumed. The four HTML options and the CSS url() value are complete, working snippets for rendering the document in a page without any server route. Sending a prefix where none is expected is one of the most common causes of corrupt-file errors.

When should Base64 be wrapped at 76 or 64 characters?

Wrap at 76 when the string is going into a MIME body, because RFC 2045 defines that line length and some mail transports still fold or reject longer lines. Wrap at 64 for PEM-style blocks and for systems that inherited that convention from OpenSSL. Leave it unwrapped everywhere else. Wrapping is offered only for raw output on purpose: a newline inside a data URI, a JSON string, an HTML attribute or a CSS url() breaks the parser reading it, so choosing a wrapped width with any of those formats selected leaves the payload on one line and says so.

Should you Base64 a PDF for an API at all?

Sometimes it is the only option, and sometimes it is a habit worth questioning. It is genuinely the right answer when the transport is JSON-only, when you are embedding a document inside a signed payload, or when you need a self-contained test fixture. It is the wrong answer for large files, where multipart upload or a pre-signed URL avoids both the size inflation and the memory cost of holding the whole document as a string.

Are my files uploaded when encoding a PDF as Base64?

No, and this is the tool where the usual alternative is most alarming. Encoding a PDF normally means pasting a confidential document into the first web form that appears in search results, which hands the entire file to a stranger in one action. Here the bytes are read locally and encoded by the browser, so a signed agreement or a customer record becomes a Base64 string without ever being transmitted.

Encode a PDF as Base64 in code

The same conversion in six languages. All of them read the file as bytes and Base64-encode those bytes — the only thing that varies is which standard library call does it. Nothing here needs a package install.

JavaScript (browser)

FileReader gives you a data URI directly; slice off the prefix if you want raw Base64.

const file = document.querySelector("input[type=file]").files[0]

const dataUri = await new Promise((resolve, reject) => {
  const reader = new FileReader()
  reader.onload = () => resolve(reader.result)
  reader.onerror = () => reject(reader.error)
  reader.readAsDataURL(file)
})

// "data:application/pdf;base64,JVBERi0xLjcK..."
const base64 = dataUri.slice(dataUri.indexOf(",") + 1)

JavaScript (fetch a URL)

When the PDF is already on a URL, go through an ArrayBuffer rather than a Blob round-trip.

const response = await fetch("/invoice.pdf")
const bytes = new Uint8Array(await response.arrayBuffer())

let binary = ""
for (let i = 0; i < bytes.length; i += 0x8000) {
  binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000))
}
const base64 = btoa(binary)

Node.js

Buffer does the encoding; no dependency required.

import { readFile } from "node:fs/promises"

const bytes = await readFile("invoice.pdf")
const base64 = bytes.toString("base64")
const dataUri = "data:application/pdf;base64," + base64

Python

b64encode returns bytes, so decode to ASCII before putting it in JSON.

import base64

with open("invoice.pdf", "rb") as handle:
    base64_pdf = base64.b64encode(handle.read()).decode("ascii")

# 76-character MIME lines, if the transport wants them
with open("invoice.pdf", "rb") as handle:
    wrapped = base64.encodebytes(handle.read()).decode("ascii")

cURL

Encode on the shell and post the result as a JSON field.

# macOS
base64 -i invoice.pdf -o invoice.b64

# GNU coreutils (-w 0 stops it wrapping at 76 columns)
base64 -w 0 invoice.pdf > invoice.b64

curl -X POST https://api.example.com/documents \
  -H "Content-Type: application/json" \
  -d "{\"data\":\"$(cat invoice.b64)\"}"

PHP

base64_encode over the raw file contents.

<?php
$base64 = base64_encode(file_get_contents("invoice.pdf"));
$dataUri = "data:application/pdf;base64," . $base64;

C#

Convert.ToBase64String, with the option that inserts MIME line breaks.

byte[] bytes = File.ReadAllBytes("invoice.pdf");
string base64 = Convert.ToBase64String(bytes);

// 76-character lines separated by CRLF
string wrapped = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks);

How much bigger does Base64 make a PDF?

About a third bigger, always. Base64 turns every three bytes into four printable characters, so the encoded length is exactly ceil(bytes / 3) × 4, rounded up to a multiple of four with = padding. That is a 33.3% increase before any JSON escaping, and it is why a PDF that uploads fine as multipart form data is rejected when the same file is sent as an inline Base64 field.

Original PDF size compared with its Base64-encoded length
Example filePDF sizeBase64 length
One-page invoice45 KB60 KB
Ten-page contract250 KB333 KB
Scanned passport page1.2 MB1.6 MB
Illustrated 40-page report8 MB10.7 MB
Full scanned manual32 MB42.7 MB

Which request-body limits does that break?

Work backwards from the ceiling: divide the limit by 1.37 to get the largest PDF that still fits once encoded and wrapped in a JSON object.

  • Vercel serverless function request body (4.5 MB) — fits a PDF of roughly 3.4 MB or less.
  • AWS Lambda synchronous invoke payload (6 MB) — fits a PDF of roughly 4.5 MB or less.
  • Amazon API Gateway payload (10 MB) — fits a PDF of roughly 7.5 MB or less.

Above those sizes, stop sending Base64. Upload the PDF as multipart/form-data, or hand the client a pre-signed URL and send only the resulting object key in your JSON. Base64 in a request body is a convenience for small documents, not a transfer mechanism for large ones.

How to do this offline, on the command line

Every operating system already ships a Base64 encoder. If you are doing this more than once, do it on the command line.

base64 (macOS)

Built in

base64 -i invoice.pdf -o invoice.b64

BSD base64 writes one unbroken line by default.

base64 (GNU coreutils)

Built in on Linux

base64 -w 0 invoice.pdf > invoice.b64

Without -w 0 GNU base64 wraps at 76 columns, which is the MIME convention and breaks a data URI.

PowerShell

Built in on Windows

[Convert]::ToBase64String([IO.File]::ReadAllBytes("invoice.pdf")) > invoice.b64

certutil -encode also works but adds BEGIN/END header lines you then have to strip.

Frequently Asked Questions

How do I convert a PDF to Base64?
Select your PDF, choose an output format, and run the converter. PDFBase encodes the file locally and hands back the result already wrapped in the shape you picked, from raw Base64 through to a working HTML embed.
What is a Base64 PDF used for?
A Base64 PDF is useful when a PDF must travel as text, such as inside JSON, an API payload, a webhook body, a database field, an XML or SOAP envelope, or an embedded data URL.
Should I include the data URL prefix?
Choose the Data URI format when you need a complete data URL for embedding. Choose Raw Base64 when an API or database field expects only the encoded string and adds its own prefix — sending the prefix where it is not expected is the most common cause of corrupt-file errors.
How do I embed a PDF in a web page without a server?
Pick the HTML embed, object or iframe format. Each produces a complete tag whose src or data attribute is the full data URI, so the page renders the document with no upload, no route and no object-URL bookkeeping. The download-link format gives you an anchor with a working download attribute instead.
Why can I only wrap raw Base64 at 76 or 64 characters?
Because every other format needs one unbroken run of Base64. A newline inside a data URI, a JSON string, an HTML attribute or a CSS url() breaks the parser reading it. Pick a wrapped width with one of those formats selected and the tool keeps the payload on one line and tells you it did.
How big will the encoded string be?
Exactly ceil(bytes ÷ 3) × 4 characters, which is about 33% larger than the PDF. The tool warns you when the result passes a common request-body ceiling such as the 4.5 MB Vercel serverless limit or the 6 MB AWS Lambda synchronous payload.
Does the PDF get uploaded?
No. PDF to Base64 conversion runs entirely in your browser, so the PDF stays on your device.

Need More Tools?

Explore our collection of 58 free PDF tools, all with privacy-first processing.

Browse All Tools