Base64 shows up constantly in work that has nothing to do with encoding: embedding an image in CSS, stuffing a certificate into an API call, packing a config into an environment variable. It's also where people waste the most time, because half the answers online assume you already have base64 installed.
The one-liner per platform
Linux / macOS — both ship with the coreutils version:
base64 -w 0 file.pdf
The -w 0 disables line wrapping. Without it you get a 76-column wrap, which breaks JSON and most API payloads. On macOS the flag is -b 0 instead:
base64 -b 0 file.pdf
Windows PowerShell — no coreutils, but .NET has you covered:
[Convert]::ToBase64String([IO.File]::ReadAllBytes("C:\path\file.pdf"))
Windows cmd — certutil can do it, with an annoying header:
certutil -encode file.pdf output.b64
The output includes -----BEGIN CERTIFICATE----- lines you have to strip manually. That's the part everyone forgets and then wonders why the API returns 400.
Encoding a string instead of a file
Different operation entirely, and the flags differ:
echo -n "username:password" | base64
The -n matters. Without it, echo appends a newline, and you end up with dXNlcjpwYXNzCg== instead of dXNlcjpwYXNz. That trailing Cg== is a line feed. Every Basic Auth header built that way fails silently until someone notices the extra byte.
This is the single most common Base64 mistake I see in support tickets. If your Authorization: Basic header gets rejected and the credentials are definitely correct, check for that trailing newline first.
Encoding a Kubernetes secret
Worth calling out separately, because the syntax trips people up:
echo -n "my-secret-value" | base64 kubectl create secret generic my-secret --from-literal=key=value
Base64 is encoding, not encryption. Anyone who can read the secret can decode it in one command. If you're using it to protect something, you have a security problem, not an encoding problem.
Doing it in the browser
For small files or one-off strings, skipping the terminal entirely is faster. Our Base64 Encoder & Decoder handles text, files and images in the browser — nothing is uploaded, which matters when the file contains client data you shouldn't be pasting into random sites.
The same page does URL encoding too, which is the other half of the job: %20 vs + for spaces, %2F vs / in paths. Getting those wrong produces links that work in your test environment and break in production.