How to convert a Word document to Base64
- Drop a
.docxor.docfile on the box or choose it with the file picker. Several files at once are fine. - Output as starts on Plain Base64. JSON wraps it with the file name, MIME type and size; HTML
<a download>makes a download link. - Tick Wrap at 76 only for a hand-built MIME email.
- Copy output.
Save the document before encoding it. The tool reads the file as it is on disk, so edits still open in Word are not included.
DOCX or DOC: check the first characters
| File | What it is | Base64 starts with |
|---|---|---|
.docx | ZIP container with XML parts | UEsDB |
.docm (with macros) | same ZIP container | UEsDB |
.doc | Compound File Binary (OLE2) | 0M8R4KGxGuE |
encrypted .docx | encrypted package in a compound file | 0M8R4KGxGuE |
If a service rejects your “DOCX” as invalid and the Base64 starts with 0M8R4KGxGuE, you are sending either an old .doc renamed to .docx or a password-protected file. If it starts with JVBERi0, someone saved a PDF with a .docx name. PCFET0NUWVBF or PGh0bWw means HTML, often a download that returned an error page.
Where Base64 Word files go
- E-signature and document-generation APIs take the document as Base64 in a JSON body, together with a file name and MIME type, and often return a filled or signed document the same way.
- Transactional email APIs accept attachments as Base64 strings in JSON. The raw MIME version of the same thing uses 76-character lines; see Base64 in email.
- Conversion services that turn DOCX into PDF accept the source file inline when it is small.
Base64 is a transport format and protects nothing: anyone who intercepts the string can decode the document with Base64 to Word. For confidential files, rely on TLS and access control, or encrypt the document. See encoding vs encryption.
Encode a DOCX in PHP
<?php
$path = 'contract.docx';
$payload = json_encode([
'filename' => basename($path),
'contentType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'content' => base64_encode(file_get_contents($path)),
], JSON_UNESCAPED_SLASHES);
Without JSON_UNESCAPED_SLASHES, PHP writes every / in the Base64 as \/. That is valid JSON and decodes correctly, but it makes the string longer and breaks naive text comparisons. The key names are placeholders for whatever your API uses. More in Base64 in PHP.
For Excel files the same approach applies, with a different MIME type; see Excel to Base64. If the receiver can take PDF, PDF to Base64 avoids layout differences between Word versions.
FAQ
What MIME type goes with a Base64 DOCX?
application/vnd.openxmlformats-officedocument.wordprocessingml.document. The older .doc format is application/msword. APIs often need the type in a separate field, and some use it to pick a converter.
Why does my DOCX Base64 start with UEsDB?
A DOCX is a ZIP archive of XML files, and every ZIP starts with PK\x03\x04, which is UEsDB in Base64. An old binary .doc starts with 0M8R4KGxGuE instead.
My password-protected DOCX starts with 0M8R4KGxGuE. Is it broken?
No. When Word encrypts a DOCX, it stores the encrypted package inside the older compound-file container, so it gets the .doc-style header. The file is fine; the receiver needs the password to open it.