Welcome to PDF.to API documentation
To get a developer key please go todeveloper portal
Authorization: <api_key>
Convert PDF to and from anything and more
Replace the convert_to from pdf-docx to any of the formats below
import requests
import time
import shutil
headers = {"Authorization": "api_key"}
file_list = ["/path/to/file.pdf"]
params = {
"lang": "en",
"convert_to": "pdf-docx",
"ocr": False
}
def download_file(url, local_filename, api_url):
url = f"{api_url}/{url.strip('/')}"
with requests.get(url, stream=True) as r:
with open(local_filename, "wb") as f:
shutil.copyfileobj(r.raw, f)
return local_filename
def convert_files(params, headers):
r = requests.get(
url="https://www.pdf.to/apis/"
)
if r.status_code != 200:
print("We couldn't get the API name")
print(r.status_code)
print(r.content)
return None, None
api_url = r.json().get("api")
files = [eval(f'("files", open("{file}", "rb"))') for file in file_list]
r = requests.post(
url=f"{api_url}/v1/convert/",
files=files,
data=params,
headers=headers
)
if r.status_code != 200:
print(r.status_code)
print(r.content)
return None, None
return r.json(), api_url
def get_results(params, api_url):
if params.get("error"):
print(params)
return params.get("error")
r = requests.post(
url=f"{api_url}/v1/results/",
data=params
)
data = r.json()
finished = data.get("finished")
if not finished:
if int(data.get("queue_count")) > 0:
print("queue: %s" % data.get("queue_count"))
time.sleep(5)
get_results(params, api_url)
return
for f in data.get("files"):
print(f.get("url"))
download_file(f.get("url"), f.get("filename"), api_url)
return {"finished": "files downloaded"}
resp, api_url = convert_files(params, headers)
get_results(resp, api_url)
Request (Get API URL):
curl https://www.pdf.to/apis/
Response:
{api: <api_url>}
Request (Upload files for conversion):
curl -F "lang=en" -F "convert_to=pdf-png" -F "files=@/path/to/testfile.pdf" -F "files=@/path/to/testfile.pdf" -F "files=@/path/to/testfile.pdf" -H "Authorization: <api_key>" <api_url>/v1/convert/
Response:
{uuid: <conversion_uuid>}
Request (Get download URLs):
curl -F "uuid=<conversion_uuid>" -H "Authorization: <api_key>" <api_url>/v1/results/
Response:
{"files": [{"download_url": <download_url>, filename: <filename>, "url": <url>}], "failed": [], "finished": true, "queue_count": 0, "errors": []}
<?php
ini_set("display_errors", 1);
ini_set("display_startup_errors", 1);
error_reporting(E_ERROR | E_PARSE);
$headers = array("Authorization: api_key");
$file_list = ["/path/to/file.pdf"];
function download_file($url, $filename, $api_url){
$curl = curl_init();
$url = $api_url.trim($url, "/");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
$data = curl_exec($curl);
$error = curl_error($curl);
curl_close ($curl);
# Make sure destionation path exists
$destination_path = "/path/to/file/";
$destination_file = fopen($destination_path . $filename, "w+");
fwrite($destination_file, $data);
fclose($destination_file);
}
function convert_files($file_list, $headers, $convert_to) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.pdf.to/apis/");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($curl);
curl_close($curl);
$content = json_decode($content);
$api_url = $content->api;
$post_data["lang"] = "en";
$post_data["convert_to"] = $convert_to;
foreach ($file_list as $index => $file) {
$post_data["file[$index]"] = curl_file_create(
realpath($file),
mime_content_type($file),
basename($file)
);
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $api_url."/v1/convert/");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($curl);
curl_close($curl);
return array(json_decode($content), $api_url);
}
function get_results($params, $api_url, $headers) {
if ($params->error) {
print_r($params->error);
return;
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $api_url."/v1/results/");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_decode(json_encode($params), true));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$content = json_decode(curl_exec($curl));
curl_close($curl);
if ($content->finished == false) {
if (intval($content->queue_count) > 0) {
print_r("queue: $content->queue_count");
}
sleep(5);
$results = get_results($params, $api_url, $headers);
return;
}
foreach ($content->files as $f) {
download_file($f->url, $f->filename, $api_url);
}
}
$convert_to = "pdf-docx";
list($resp, $api_url) = convert_files($file_list, $headers, $convert_to);
get_results($resp, $api_url, $headers);
?>
const request = require('request');
const fs = require('fs');
let file_list = ['/path/to/file.pdf'];
function convertFiles(file_list, convert_to) {
request({
url: "https://www.pdf.to/apis/",
method: 'get',
}, function(err, res, body) {
if (err) {
console.error(err);
return err;
}
let api_url = JSON.parse(body).api;
let formData = {
'lang': 'en',
'convert_to': convert_to
};
for (var i = 0; i < file_list.length; i ++) {
formData['files'] = fs.createReadStream(file_list[i]);
}
request({
url: `${api_url}/v1/convert/`,
method: 'post',
formData: formData,
headers: {
"Authorization": "api_key",
"Content-Type": "multipart/form-data",
}
}, function(err, res, body) {
if (err) {
console.error(err);
return err;
}
getResults(JSON.parse(body), api_url);
});
});
}
function getResults(data, api_url) {
if (data.error) {
console.error(data);
return data.error;
}
request({
url: `${api_url}/v1/results/`,
method: 'post',
formData: data
}, function(e, r, body) {
response = JSON.parse(body);
if (!response.finished) {
setTimeout(
function() {
getResults(data, api_url);
}, 1000
);
return;
}
console.log("Download URLs:");
response.files.forEach(function (item) {
if (item.url.startsWith('/')) {
item.url = item.url.substring(1, item.url.length - 1);
}
console.log(`${api_url}/${item.url}`);
});
})
}
let convert_to = "pdf-docx"
convertFiles(file_list, convert_to);
Will return
/path/to/local/file_processed.docx
['pdf-csv', 'doc-jpeg', 'word-jpeg', 'pdf-pptx', 'word-xls', 'webp-doc', 'doc-xlsx', 'psd-docx', 'pdf-word', 'word-excel', 'tiff-pdf', 'xls-pdf', 'pdf-xlsx', 'doc-png', 'pdf-docx', 'html-pdf', 'word-svg', 'docx-html', 'word-epub', 'doc-xls', 'powerpoint-pdf', 'gif-pdf', 'webp-docx', 'jpg-word', 'odt-pdf', 'jpg-docx', 'docx-jpeg', 'svg-docx', 'docx-xls', 'pdf-split', 'word-pptx', 'doc-webp', 'jpg-doc', 'svg-word', 'pptx-pdf', 'pdf-jpeg', 'word-webp', 'pdf-psd', 'docx-webp', 'png-word', 'doc-tiff', 'epub-pdf', 'pdf-bmp', 'doc-svg', 'word-gif', 'docx-tiff', 'pdf-webp', 'doc-pdf', 'word-bmp', 'doc-html', 'jpg-pdf', 'pdf-gif', 'xlsx-pdf', 'jpeg-doc', 'pdf-xls', 'pdf-odt', 'pdf-compress', 'docx-svg', 'webp-pdf', 'pdf-doc', 'word-pdf', 'pdf-repair', 'pdf-epub', 'pdf-organize', 'pdf-zip', 'word-powerpoint', 'tiff-doc', 'svg-pdf', 'png-doc', 'jpeg-pdf', 'webp-word', 'doc-epub', 'word-html', 'pdf-ppt', 'docx-doc', 'pdf-powerpoint', 'docx-jpg', 'bmp-word', 'docx-gif', 'docx-bmp', 'gif-word', 'pdf-image', 'bmp-pdf', 'word-jpg', 'pdf-html', 'pdf-txt', 'bmp-docx', 'png-pdf', 'docx-epub', 'pdf-unlock', 'psd-pdf', 'doc-psd', 'psd-word', 'image-pdf', 'docx-png', 'doc-jpg', 'word-zip', 'word-ppt', 'jpeg-word', 'docx-ppt', 'bmp-doc', 'gif-doc', 'pdf-svg', 'pdf-tiff', 'png-docx', 'pdf-png', 'docx-psd', 'doc-bmp', 'tiff-docx', 'word-xlsx', 'word-png', 'docx-pdf', 'ppt-pdf', 'docx-pptx', 'doc-gif', 'csv-pdf', 'pdf-jpg', 'gif-docx', 'tiff-word', 'word-psd', 'excel-pdf', 'pdf-excel', 'psd-doc', 'jpeg-docx', 'svg-doc', 'word-tiff', 'docx-xlsx', 'txt-pdf', 'docx-zip', 'doc-zip', 'doc-powerpoint', 'docx-powerpoint', 'docx-excel', 'doc-excel', 'jpeg-png', 'jpeg-ico', 'jpeg-svg', 'jpeg-jpg', 'jpeg-bmp', 'jpeg-jfif', 'jpeg-zip', 'jpeg-tiff', 'jpeg-webp', 'jpeg-gif', 'jpeg-psd', 'jpg-jpeg', 'png-jpeg', 'bmp-jpeg', 'svg-jpeg', 'tiff-jpeg', 'webp-jpeg', 'psd-jpeg', 'gif-jpeg', 'jpeg-compress', 'jpg-png', 'jpg-ico', 'jpg-svg', 'jpg-jpeg', 'jpg-bmp', 'jpg-jfif', 'jpg-zip', 'jpg-tiff', 'jpg-webp', 'jpg-gif', 'jpg-psd', 'jpeg-jpg', 'png-jpg', 'bmp-jpg', 'svg-jpg', 'tiff-jpg', 'webp-jpg', 'psd-jpg', 'gif-jpg', 'jpg-compress', 'word-doc', 'word-docx', 'mp3-wav', 'mp3-m4a', 'mp3-flac', 'mp3-wma', 'mp3-aiff', 'mp3-aac', 'mp3-mp2', 'mp3-m4r', 'mp3-ogg', 'mp3-ac3', 'mp3-amr', 'mp3-dts', 'mp3-opus', 'mp3-mp4', 'mp3-avi', 'mp3-wmv', 'mp3-mpeg', 'mp3-mov', 'mp3-mpg', 'mp3-divx', 'mp3-webm', 'mp3-flv', 'mp3-3gp', 'mp3-vob', 'mp3-av1', 'mp3-m4v', 'mp3-hls', 'mp3-m3u8', 'mp3-mkv', 'mp3-zip', 'wav-mp3', 'm4a-mp3', 'flac-mp3', 'wma-mp3', 'aiff-mp3', 'aac-mp3', 'mp2-mp3', 'm4r-mp3', 'ogg-mp3', 'ac3-mp3', 'amr-mp3', 'dts-mp3', 'opus-mp3', 'mp4-mp3', 'avi-mp3', 'wmv-mp3', 'mpeg-mp3', 'mov-mp3', 'mpg-mp3', 'divx-mp3', 'webm-mp3', 'flv-mp3', 'vob-mp3', 'av1-mp3', 'm4v-mp3', 'mkv-mp3', 'mp3-mpeg2', 'mp4-mp3', 'mp4-wav', 'mp4-m4a', 'mp4-flac', 'mp4-wma', 'mp4-aiff', 'mp4-aac', 'mp4-mp2', 'mp4-m4r', 'mp4-ogg', 'mp4-ac3', 'mp4-amr', 'mp4-dts', 'mp4-opus', 'mp4-avi', 'mp4-wmv', 'mp4-mpeg', 'mp4-mov', 'mp4-mpg', 'mp4-divx', 'mp4-webm', 'mp4-flv', 'mp4-3gp', 'mp4-vob', 'mp4-av1', 'mp4-m4v', 'mp4-hls', 'mp4-m3u8', 'mp4-mkv', 'mp4-gif', 'mp4-webp', 'mp4-zip', 'wav-mp4', 'm4a-mp4', 'flac-mp4', 'wma-mp4', 'aiff-mp4', 'aac-mp4', 'mp2-mp4', 'm4r-mp4', 'ogg-mp4', 'ac3-mp4', 'amr-mp4', 'dts-mp4', 'opus-mp4', 'mp4-mp4', 'avi-mp4', 'wmv-mp4', 'mpeg-mp4', 'mov-mp4', 'mpg-mp4', 'divx-mp4', 'webm-mp4', 'flv-mp4', 'vob-mp4', 'av1-mp4', 'm4v-mp4', 'mkv-mp4', 'webp-mp4', 'png-jpg', 'png-ico', 'png-svg', 'png-jpeg', 'png-bmp', 'png-jfif', 'png-zip', 'png-tiff', 'png-webp', 'png-gif', 'png-psd', 'jpeg-png', 'jpg-png', 'bmp-png', 'svg-png', 'tiff-png', 'webp-png', 'psd-png', 'gif-png', 'png-compress', 'webm-mp3', 'webm-wav', 'webm-m4a', 'webm-flac', 'webm-wma', 'webm-aiff', 'webm-aac', 'webm-mp2', 'webm-m4r', 'webm-ogg', 'webm-ac3', 'webm-amr', 'webm-dts', 'webm-opus', 'webm-avi', 'webm-wmv', 'webm-mpeg', 'webm-mov', 'webm-mpg', 'webm-divx', 'webm-flv', 'webm-3gp', 'webm-vob', 'webm-av1', 'webm-m4v', 'webm-hls', 'webm-m3u8', 'webm-mkv', 'webm-gif', 'webm-webp', 'webm-zip', 'webm-webp', 'wav-webm', 'm4a-webm', 'flac-webm', 'wma-webm', 'aiff-webm', 'aac-webm', 'mp2-webm', 'm4r-webm', 'ogg-webm', 'ac3-webm', 'amr-webm', 'dts-webm', 'opus-webm', 'avi-webm', 'wmv-webm', 'mpeg-webm', 'mov-webm', 'mpg-webm', 'divx-webm', 'webm-webm', 'flv-webm', 'webp-webm', 'vob-webm', 'av1-webm', 'm4v-webm', 'mkv-webm', 'gif-mp4', 'gif-webm', 'webp-jpg', 'webp-ico', 'webp-svg', 'webp-jpeg', 'webp-bmp', 'webp-jfif', 'webp-zip', 'webp-tiff', 'webp-webp', 'webp-gif', 'webp-psd', 'jpeg-webp', 'jpg-webp', 'bmp-webp', 'svg-webp', 'tiff-webp', 'webp-webp', 'psd-webp', 'gif-webp', 'webp-compress', 'mkv-mp3', 'mkv-wav', 'mkv-m4a', 'mkv-flac', 'mkv-wma', 'mkv-aiff', 'mkv-aac', 'mkv-mp2', 'mkv-m4r', 'mkv-ogg', 'mkv-ac3', 'mkv-amr', 'mkv-dts', 'mkv-opus', 'mkv-avi', 'mkv-wmv', 'mkv-mpeg', 'mkv-mov', 'mkv-mpg', 'mkv-divx', 'mkv-webm', 'mkv-flv', 'mkv-3gp', 'mkv-vob', 'mkv-av1', 'mkv-m4v', 'mkv-hls', 'mkv-m3u8', 'mkv-mkv', 'mkv-gif', 'mkv-webp', 'mkv-zip', 'wav-mkv', 'm4a-mkv', 'flac-mkv', 'wma-mkv', 'aiff-mkv', 'aac-mkv', 'mp2-mkv', 'm4r-mkv', 'ogg-mkv', 'ac3-mkv', 'amr-mkv', 'dts-mkv', 'opus-mkv', 'mkv-mkv', 'avi-mkv', 'wmv-mkv', 'mpeg-mkv', 'mov-mkv', 'mpg-mkv', 'divx-mkv', 'webm-mkv', 'flv-mkv', 'vob-mkv', 'av1-mkv', 'm4v-mkv', 'mkv-mkv', 'webp-mkv', 'epub-pdf', 'epub-mobi', 'epub-kindle', 'epub-azw3', 'epub-fb2', 'epub-cbz', 'epub-cbr', 'epub-doc', 'epub-docx', 'epub-word', 'epub-html', 'epub-rtf', 'epub-text', 'epub-png', 'epub-svg', 'epub-jpg', 'epub-bmp', 'epub-zip', 'epub-gif', 'epub-tiff', 'pdf-epub', 'image-removebackground', 'image-backgroundremover', 'png-removebackground', 'jpg-removebackground', 'jpeg-removebackground', 'mov-mp3', 'mov-wav', 'mov-m4a', 'mov-flac', 'mov-wma', 'mov-aiff', 'mov-aac', 'mov-mp2', 'mov-m4r', 'mov-ogg', 'mov-ac3', 'mov-amr', 'mov-dts', 'mov-opus', 'mov-avi', 'mov-wmv', 'mov-mpeg', 'mov-mpg', 'mov-divx', 'mov-webm', 'mov-flv', 'mov-3gp', 'mov-vob', 'mov-av1', 'mov-m4v', 'mov-hls', 'mov-m3u8', 'mov-mkv', 'mov-gif', 'mov-webp', 'mov-zip', 'wav-mov', 'm4a-mov', 'flac-mov', 'wma-mov', 'aiff-mov', 'aac-mov', 'mp2-mov', 'm4r-mov', 'ogg-mov', 'ac3-mov', 'amr-mov', 'dts-mov', 'opus-mov', 'mov-mov', 'avi-mov', 'wmv-mov', 'mpeg-mov', 'mpg-mov', 'divx-mov', 'webm-mov', 'flv-mov', 'vob-mov', 'av1-mov', 'm4v-mov', 'mkv-mov', 'webp-mov', 'gif-mov' ] To make multiple simply put more files in your list