← Blog
Base64 PDF Node.js

Node.js Base64 to PDF: Buffer and fs Guide

Convert Base64 to PDF in Node.js with Buffer and fs. Includes file writes, API-response handling, and the common mistakes that corrupt PDF output.

· GoGood.dev

If you are doing Base64→PDF work on the server, Node.js Buffer is the tool you want. It turns the string back into binary data with almost no ceremony.

TL;DR: Clean the Base64 string, decode with Buffer.from(value, 'base64'), then save the result with fs.writeFileSync() or fs.promises.writeFile().

Minimal example

const fs = require('fs');

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

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);
}

Why Buffer is the right abstraction

Buffer is already binary-safe, fast, and designed for exactly this kind of work. You do not need the extra atob()Uint8Array steps that browsers require.

Common mistakes

Forgetting to remove a data URI prefix

Always clean this:

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

Assuming the field is valid Base64

Use a quick manual test with GoGood.dev Base64 Converter when debugging upstream payloads.

Saving with the wrong name

Make sure the output file ends with .pdf.

Great use cases for Node.js

  • Express/Fastify backends
  • queue workers
  • document generation pipelines
  • integration scripts
  • webhook processing

FAQ

Is Buffer enough to convert Base64 to PDF?

Yes. In Node.js, Buffer is the standard solution.

Can I decode and stream the PDF instead of saving it?

Yes. Once you have the Buffer, you can send it directly in an HTTP response.

Why not use a package?

Because Node.js already has what you need built in.


For backend workflows, this should stay simple: clean the string, decode with Buffer, write or stream the file. If you only need to inspect the payload once, use GoGood.dev Base64 Converter and confirm the data first.

Related: How to Convert Base64 to PDF Online · How to Convert Base64 to PDF in JavaScript · How to Extract a PDF from an API JSON Response · How to Convert Base64 Back to a File · How to Convert an Image to Base64 · When to Use Base64