Convert Word and Excel files to PDF with an API
Maybe customers upload contracts as Word files, or your app makes price lists in Excel, and you need them as PDF without running LibreOffice on your web servers. One request to the PDFDispatch API does the conversion.
The request
Send the file as files to /v1/convert/office:
curl --fail-with-body \
--header "Authorization: Bearer $PDD_KEY" \
--form files=@contract.docx \
--output contract.pdf \
https://api.pdfdispatch.com/v1/convert/office
The response body is the PDF. The key (pdd_...) comes from pdfdispatch.com/app/keys.
The file's extension decides how it is read, so keep it right. .docx, .xlsx, .pptx, the older .doc, .xls
and .ppt, OpenDocument (.odt, .ods, .odp), .rtf and .csv all work, and many more. File
types has the full list.
In Python
import os
import requests
with open("contract.docx", "rb") as f:
res = requests.post(
"https://api.pdfdispatch.com/v1/convert/office",
headers={"Authorization": f"Bearer {os.environ['PDD_KEY']}"},
files=[("files", ("contract.docx", f))],
timeout=90,
)
res.raise_for_status()
with open("contract.pdf", "wb") as out:
out.write(res.content)
In PHP
<?php
$ch = curl_init('https://api.pdfdispatch.com/v1/convert/office');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Authorization: Bearer '.getenv('PDD_KEY')],
CURLOPT_POSTFIELDS => ['files' => new CURLFile('contract.docx')],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 90,
]);
$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_RESPONSE_CODE) !== 200) {
exit("Conversion failed: $pdf\n");
}
file_put_contents('contract.pdf', $pdf);
Excel: one sheet per page
A wide spreadsheet is cut into many small pages by default, the same as when you print it. Send
singlePageSheets=true to put each sheet on one page:
curl --fail-with-body \
--header "Authorization: Bearer $PDD_KEY" \
--form files=@prices.xlsx \
--form singlePageSheets=true \
--output prices.pdf \
https://api.pdfdispatch.com/v1/convert/office
Several files in one PDF
Send more than one file and add merge=true. They are joined in the order of their file names, so number them:
curl --fail-with-body \
--header "Authorization: Bearer $PDD_KEY" \
--form files=@1-cover.docx \
--form files=@2-terms.docx \
--form files=@3-prices.xlsx \
--form merge=true \
--output offer.pdf \
https://api.pdfdispatch.com/v1/convert/office
Without merge, several files come back as a ZIP with one PDF each.
PDFs for the archive
Contracts and invoices often have to be kept for years. Add pdfa=PDF/A-2b and the result is a PDF/A file, the ISO
format for long-term archiving: fonts are embedded and nothing depends on outside resources.
Will it look like Word?
Most business documents come out the same. LibreOffice does the conversion, not Microsoft Office, and fonts make the biggest difference: Calibri and Cambria are replaced by Carlito and Caladea, which have the same letter widths, so lines and pages break in the same places. A font that is not installed is replaced by a similar one. If a document uses an unusual font, embed it in the Word file. Complex macros and some SmartArt can come out differently, so test your own templates once.
For documents you make yourself, HTML often gives you more control. See HTML to PDF in Laravel or HTML to PDF in Node.js.
Next
- Office documents: every field of this route.
- PDF tools: passwords, watermarks and more, also in the same request.
- Limits and errors: file sizes, time limits and error codes.