← Blog
Base64 PDF JavaScript Python

Base64 to PDF: Best Method by Use Case (Online, Browser, Node, Python)

Choose the right Base64-to-PDF workflow for your use case: quick online decode, browser download, Node.js backend handling, or Python file export.

· GoGood.dev

You see this pattern all the time in development: an API returns a long Base64 string, a backend stores a PDF in JSON, or a webhook payload contains data:application/pdf;base64,.... The next step is usually the same: turn that string back into a real PDF file you can open, save, email, or inspect.

This guide is the decision-maker page for the cluster. Instead of focusing on one environment, it helps you pick the fastest working route for your case: quick online decode, browser-side export, Node.js backend handling, or Python scripting.

TL;DR: If you just need the file now, use GoGood.dev Base64 Converter. If the PDF must stay inside a web app, use the browser method. If it belongs in backend automation, use Node.js or Python.


What Does “Base64 to PDF” Mean?

Base64 is a text encoding for binary data. A PDF file is binary. When a system needs to move that PDF through a text-only channel — JSON, HTML, email, environment variables, logs, or form payloads — it often converts the file into a Base64 string first.

A real example looks like this:

{
  "documentName": "invoice-2026-05.pdf",
  "mimeType": "application/pdf",
  "data": "JVBERi0xLjQKJcfsj6IKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZyAvUGFnZXMgMiAwIFIgPj4KZW5kb2Jq..."
}

Or as a data URI:

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

To convert Base64 to PDF, you decode that text back into bytes and save those bytes with a .pdf filename.

This comes up in a few very common cases:

  • API responses that embed generated PDFs
  • browser apps that receive Base64 from a backend
  • email/document systems
  • test fixtures and debugging payloads
  • integrations where files are transported inside JSON

Method 1: Fastest path for one-off decoding

If you do not need code, this is the fastest path.

  1. Open GoGood.dev Base64 Converter
  2. Paste the Base64 string or full data URI
  3. Let the tool detect the file type
  4. Download the decoded PDF

This is the right move when you are:

  • debugging a payload quickly
  • verifying whether the Base64 content is valid
  • extracting a PDF from an API response
  • checking whether the issue is your code or the source data

If the result opens correctly in the browser or PDF viewer, your Base64 input is valid. If it fails there too, the source string is probably incomplete or malformed.


Method 2: Browser app flow

When you are building a web app, the browser approach usually means: decode the string, wrap it in a Blob, then trigger a download.

function base64ToPdf(base64String, fileName = 'document.pdf') {
  const cleanBase64 = base64String.replace(/^data:application\/pdf;base64,/, '');
  const byteCharacters = atob(cleanBase64);
  const byteNumbers = Array.from(byteCharacters, char => char.charCodeAt(0));
  const byteArray = new Uint8Array(byteNumbers);

  const blob = new Blob([byteArray], { type: 'application/pdf' });
  const url = URL.createObjectURL(blob);

  const link = document.createElement('a');
  link.href = url;
  link.download = fileName;
  link.click();

  URL.revokeObjectURL(url);
}

// Usage
base64ToPdf('JVBERi0xLjQKJcfsj6I...', 'invoice.pdf');

If the string might include a data URI prefix

Many systems return the full data:application/pdf;base64,... value. Strip it first:

const cleanBase64 = input.replace(/^data:[^;]+;base64,/, '');

That regex is safer than hard-coding only application/pdf, especially when the payload source is inconsistent.

Open the PDF in a new tab instead of downloading

function openBase64Pdf(base64String) {
  const cleanBase64 = base64String.replace(/^data:[^;]+;base64,/, '');
  const byteCharacters = atob(cleanBase64);
  const byteNumbers = Array.from(byteCharacters, char => char.charCodeAt(0));
  const byteArray = new Uint8Array(byteNumbers);
  const blob = new Blob([byteArray], { type: 'application/pdf' });
  const url = URL.createObjectURL(blob);

  window.open(url, '_blank');
}

This is useful when you want users to preview the PDF before saving it.


Method 3: Convert Base64 to PDF in Node.js

In Node.js, Buffer makes this much easier than the browser version.

const fs = require('fs');

function base64ToPdf(base64String, outputPath) {
  const cleanBase64 = base64String.replace(/^data:[^;]+;base64,/, '');
  const pdfBuffer = Buffer.from(cleanBase64, 'base64');
  fs.writeFileSync(outputPath, pdfBuffer);
}

// Usage
base64ToPdf(
  'JVBERi0xLjQKJcfsj6IKMSAwIG9iago8PC9UeXBlL0NhdGFsb2c+Pg==',
  './output/report.pdf'
);

Async version

const fs = require('fs').promises;

async function base64ToPdfAsync(base64String, outputPath) {
  const cleanBase64 = base64String.replace(/^data:[^;]+;base64,/, '');
  const pdfBuffer = Buffer.from(cleanBase64, 'base64');
  await fs.writeFile(outputPath, pdfBuffer);
}

This is the version you want in services, scripts, and workers where the decoded file needs to be stored or forwarded.

Example: extract PDF from an API response

const fs = require('fs');

const response = {
  fileName: 'contract.pdf',
  fileData: 'JVBERi0xLjQKJcfsj6IKMSAwIG9iago8PC9UeXBlL0NhdGFsb2c+Pg=='
};

const pdfBuffer = Buffer.from(response.fileData, 'base64');
fs.writeFileSync(response.fileName, pdfBuffer);

That pattern appears constantly in admin tools and integration work.


Method 4: Convert Base64 to PDF in Python

Python has this built in through the standard base64 module.

import base64


def base64_to_pdf(base64_string: str, output_path: str) -> None:
    clean_base64 = base64_string.split(',')[-1]
    pdf_bytes = base64.b64decode(clean_base64)

    with open(output_path, 'wb') as pdf_file:
        pdf_file.write(pdf_bytes)


base64_to_pdf('JVBERi0xLjQKJcfsj6IKMSAwIG9iago8PC9UeXBlL0NhdGFsb2c+Pg==', 'report.pdf')

If you are decoding PDFs inside ETL jobs, scripts, or backend tasks, this is usually enough.

Validate before writing

import base64


def is_valid_base64(value: str) -> bool:
    try:
        base64.b64decode(value.split(',')[-1], validate=True)
        return True
    except Exception:
        return False

This helps catch broken input before you create a bad PDF file on disk.


Common Problems When Decoding Base64 to PDF

1. The PDF is corrupted and will not open

This is the most common problem. Usually it is one of these:

  • the Base64 string is truncated
  • you forgot to strip the data:...;base64, prefix
  • the source string contains whitespace or line breaks
  • the input is not actually a PDF

A quick sanity check: valid PDFs usually begin with bytes that represent %PDF. If the decoded file does not start with that, the source data may be wrong.

2. You saved the file with the wrong extension

The decoded bytes might be fine, but if you save them as .txt or .bin, your OS may not open them with a PDF viewer. Make sure the file name ends in .pdf.

3. Browser atob() throws an error

That usually means the Base64 string contains invalid characters or was copied with extra whitespace.

Try cleaning the string first:

const cleanBase64 = input.replace(/\s+/g, '').replace(/^data:[^;]+;base64,/, '');

4. The downloaded PDF is blank

A blank PDF often means the source file itself was blank before encoding, or your app decoded the wrong field. In API payloads, double-check that you are using the actual file data field and not metadata.


When to Use an Online Tool vs Code

Use an online tool when:

  • you need the PDF once
  • you are debugging quickly
  • you want to verify whether the source Base64 is valid
  • you do not want to write throwaway code

Use code when:

  • this is part of an app workflow
  • users need automatic download or preview
  • you are processing files in bulk
  • the Base64 arrives regularly from APIs or queues

For quick manual work, GoGood.dev Base64 Converter is faster. For product code, decode in the environment where the file is needed.


FAQ

How do I decode Base64 to PDF online?

Paste the Base64 string into GoGood.dev Base64 Converter, let it detect the file type, then download the result as a PDF.

Can I convert Base64 to PDF in JavaScript without a server?

Yes. In the browser, decode with atob(), create a Uint8Array, wrap it in a Blob, then download or preview it.

Why is my decoded Base64 PDF corrupted?

Most of the time the string is incomplete, includes an unstripped data URI prefix, or is not actually PDF data. Remove whitespace, strip the prefix, and verify the source field.

Can Node.js save a Base64 string directly as a PDF?

Yes. Buffer.from(base64String, 'base64') gives you the bytes, and fs.writeFileSync() saves them as a real file.

What tool should I use if I just want the PDF now?

Use GoGood.dev Base64 Converter. It is the fastest way to confirm the data is valid and extract the file without writing code.


If you work with document APIs, this problem shows up over and over. The workflow does not need to be complicated: validate the string, strip the prefix if needed, decode it, and save the bytes as a PDF. For quick one-off work, start with GoGood.dev Base64 Converter.

Related: Convert Base64 to PDF in JavaScript · Convert Base64 to PDF in Python · Why Your Base64 PDF Is Corrupted · How to Convert Base64 Back to a File · How to Convert an Image to Base64 · When to Use Base64