Skip to content

PDF to Base64 Converter

Encode a PDF file into a Base64 string, or decode Base64 back into a PDF that you can preview and download.

Runs in your browser Free, no sign-up
Loading tool workspace...

What is PDF to Base64 Converter?

This PDF to Base64 converter works in both directions. In Encode mode, choose a PDF and get its contents as a Base64 string, the format most APIs use to send PDFs inside JSON, such as e-signature, invoicing, document-generation and email services.

In Decode mode, paste Base64 PDF data, for example from an API response or a database field, and the document appears in an embedded viewer so you can check it immediately, with a button to download it as a .pdf file. A data:application/pdf;base64, prefix is handled automatically.

How to use PDF to Base64 Converter

  1. Choose Encode to turn a PDF into Base64, or Decode to turn Base64 into a PDF.
  2. To encode, select a PDF file and copy the Base64 output.
  3. To decode, paste the Base64 string, with or without the data: prefix.
  4. Preview the PDF in the viewer and click Download PDF to save it.

Key features

  • Encode PDF files to Base64
  • Decode Base64 back to a PDF
  • Built-in PDF preview for decoded documents
  • Download decoded PDFs
  • Handles data:application/pdf;base64 prefixes
  • Runs entirely in your browser

Common use cases

  • Sending PDFs to APIs that accept Base64 in JSON
  • Viewing PDF documents returned as Base64 by invoicing or signing APIs
  • Recovering PDFs stored as Base64 in databases or logs
  • Embedding PDFs in test fixtures
  • Checking whether a Base64 string really contains a PDF

Technical details & standards

Encoding reads the PDF locally with the FileReader API and Base64-encodes its bytes (RFC 4648), increasing the size by about 33%. Decoding converts the Base64 back into the original bytes and displays them in an embedded viewer using a temporary object URL.

A valid PDF always starts with %PDF, which is JVBERi0 in Base64, so a Base64 string that does not begin with JVBERi0 is probably not a PDF or has extra characters at the start. Nothing is uploaded: encoding, decoding and preview all happen in your browser, which matters for invoices, contracts and other sensitive documents.

Convert a PDF to Base64 in Java and Python

Server code often has to send a PDF as Base64 in JSON, for e-signature, email and document APIs. Both snippets read the file as bytes, encode it, and decode it back to a copy.

Java (11+)

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;

public class PdfToBase64 {
    public static void main(String[] args) throws Exception {
        byte[] pdf = Files.readAllBytes(Path.of("document.pdf"));
        String base64 = Base64.getEncoder().encodeToString(pdf);
        System.out.println(base64.substring(0, 8)); // JVBERi0x, the encoded %PDF-1 header

        // Base64 back to a PDF file
        Files.write(Path.of("copy.pdf"), Base64.getDecoder().decode(base64));
    }
}

Use Base64.getMimeEncoder() instead if the receiving system expects lines of 76 characters.

Python

import base64

with open("document.pdf", "rb") as f:
    b64 = base64.b64encode(f.read()).decode("ascii")   # starts with 'JVBERi0x'

with open("copy.pdf", "wb") as f:
    f.write(base64.b64decode(b64))

Frequently asked questions

How do I convert Base64 to PDF?

Choose Decode and paste the Base64 string. The PDF appears in the viewer; click Download PDF to save it.

How can I tell if Base64 data is a PDF?

PDF files start with %PDF-, which is JVBERi0 in Base64. If your string starts with JVBERi0, it is almost certainly a PDF.

How do I convert a PDF to Base64 in code?

JavaScript (Node.js): fs.readFileSync("file.pdf").toString("base64"). Python: base64.b64encode(open("file.pdf", "rb").read()). Command line: base64 -w 0 file.pdf.

Why doesn't my decoded PDF open?

The Base64 is probably truncated or has extra characters, such as quotes or escaped line breaks from JSON. Check that it starts with JVBERi0 and clean it with Base64 Repair.

How do I convert a PDF to Base64 in Java?

Read the file with Files.readAllBytes(Path.of("document.pdf")) and pass the bytes to Base64.getEncoder().encodeToString(), both part of the standard library since Java 8 (Path.of needs Java 11). The result should start with JVBERi0, the Base64 form of %PDF-; the full example is in the code section above.

Is my data safe?

Processing happens locally in your browser. Tool input is not sent to CodeUnpack servers.

Does it work offline?

Once the page has loaded, this tool can process input without an internet connection.

More encoders & decoders tools

View all