Base64 Encoder / Decoder3 min readOct 21, 2026

How to Encode Binary Files (Audio, WOFF2 Fonts, PDFs) to Base64 in JavaScript

Step-by-step tutorial on converting Blobs, ArrayBuffers, WOFF2 web fonts, and audio files into Base64 strings using FileReader and modern Web APIs.

Direct Summary (TL;DR)

Binary assets like WOFF2 web fonts, MP3/WAV audio clips, and PDFs can be converted to Base64 data URIs using the FileReader.readAsDataURL() API in the browser or Buffer.toString('base64') in Node.js, allowing single-file distribution and zero-latency playback.

Interactive Mickytool Utility

Solve this problem now with our free Base64 Encoder / Decoder

Try it instantly in your browser — zero tracking, 100% client-side privacy.

Open Base64 Encoder / Decoder

Specification & Architecture Reference

How to Encode Binary Files (Audio, WOFF2 Fonts, PDFs) to Base64 in JavaScript — Technical Summary
Asset TypeMIME Type PrefixStandard Inlining Use Case
WOFF2 Fontdata:font/woff2;charset=utf-8;base64,Preventing FOIT (Flash of Invisible Text) in @font-face
MP3 / WAV Audiodata:audio/mp3;base64,Instant UI click sounds and game sound effects without latency
PDF Documentdata:application/pdf;base64,Embedding client-side generated invoices in <iframe>
WebP Imagedata:image/webp;base64,Above-the-fold critical banner placeholders

Inlining Binary Assets in Web Apps

In modern web development, inlining binary files can solve specific performance and portability challenges: - Eliminating the **Flash of Invisible Text (FOIT)** by embedding critical font subsets directly in `@font-face` rules. - Guaranteeing instant sound playback in interactive games without network fetch latency. - Generating offline single-file HTML reports containing embedded PDF certificates or charts.

In-Browser File to Base64 Conversion The cleanest native browser method to read a user file or `Blob` into a Base64 Data URI is `FileReader`:

javascript
function fileToBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = (error) => reject(error);
  });
}

Embedding a WOFF2 Font in CSS Once encoded, you can embed your font directly in CSS:

css
@font-face {
  font-family: 'InterSubset';
  src: url('data:font/woff2;charset=utf-8;base64,d09GMgABAAAAAA...') format('woff2');
  font-weight: 400;
  font-display: swap;
}

Warning on Asset Size Avoid inlining fonts or audio clips larger than 50 KB. Because Base64 adds a 33% size penalty and increases memory consumption during parsing, reserve this technique for targeted micro-assets.

Need to quickly convert binary files or inspect existing Data URIs? Use Mickytool's **Base64 Encoder / Decoder** for client-side encoding with zero file uploads.

Interactive Mickytool Utility

Solve this problem now with our free Base64 Encoder / Decoder

Execute this workflow immediately without saving data on remote servers.

Open Base64 Encoder / Decoder

Frequently Asked Questions

Can I extract the pure Base64 payload from a data URI?

Yes. Split the string on the comma: dataUri.split(',')[1] yields the raw Base64 string without the MIME header prefix.

Does inlining fonts hurt Lighthouse performance scores?

If the inlined font file is large (>30KB), it increases CSS bundle size and harms your First Contentful Paint (FCP) and Speed Index metrics.

Related Engineering Guides