快速开始
三步完成您的第一个 API 调用。
- 在以下位置创建 API 密钥 账户 › API 密钥
- 向某个 v1 端点发送带有 Authorization: Bearer 标头的 multipart/form-data POST 请求。
- 检查 JSON 响应 — 成功的调用将返回 15 分钟内有效的签名下载 URL。
基础 URL
https://whatapdf.com/api/v1
身份验证
每个请求必须包含 Authorization 标头,其中 API 密钥作为 Bearer 令牌。
Authorization: Bearer pdn_live_a1b2c3d4e5f6...保密您的生产密钥。像对待密码一样对待它们。切勿提交到源代码控制或暴露在客户端代码中。
密钥格式
pdn_live_<32 hex>— 生产密钥。计入您计划的速率限制。pdn_test_<32 hex>— 测试密钥。具有相同的速率限制;用于在日志中区分环境。
速率限制
速率限制按用户、按滚动 1 小时窗口计算。每个响应都包含 X-RateLimit-* 标头,以便您规划重试。
| 计划 | 每小时请求数 |
|---|---|
| Free | 10 |
| Premium | 1,000 |
| Business | 5,000 |
| Enterprise | 无限制 |
响应标头
X-RateLimit-Limit— 您计划的每小时上限。X-RateLimit-Remaining— 当前窗口剩余的调用数。X-RateLimit-Reset— 窗口重置时的 Unix 时间戳。Retry-After— 等待的秒数,仅在 429 响应中发送。
错误
所有错误均以 JSON 格式返回,包含稳定的代码和可读消息。
{
"error": {
"code": "rate_limited",
"message": "Rate limit exceeded for your plan (free: 10/hour)."
},
"meta": { "request_id": "8f9a2b3c1d4e5f6a" }
}错误代码
| Code | HTTP | 含义 |
|---|---|---|
unauthorized | 401 | 缺失、格式错误或已撤销的 API 密钥。 |
rate_limited | 429 | 已超出您计划的每小时速率限制。 |
invalid_input | 400 | 请求正文或参数验证失败。 |
payload_too_large | 413 | 文件或合并的有效负载超出您计划的大小限制。 |
internal_error | 500 | 意外的服务器错误。可安全地以退避方式重试。 |
POST /compress
在可用时使用 Ghostscript 压缩 PDF,否则使用 FPDI 回退。返回签名下载 URL。
POSThttps://whatapdf.com/api/v1/compress
参数 (multipart/form-data)
| 名称 | 类型 | 描述 |
|---|---|---|
| file * | file | 要压缩的 PDF 文件。 |
| level | string | 压缩预设。 low, medium, high |
代码示例
curl -X POST https://whatapdf.com/api/v1/compress \
-H "Authorization: Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-F "[email protected]" \
-F "level=medium"const form = new FormData();
form.append('file', fileInput.files[0]);
form.append('level', 'medium');
const res = await fetch('https://whatapdf.com/api/v1/compress', {
method: 'POST',
headers: { 'Authorization': 'Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' },
body: form,
});
const json = await res.json();
console.log(json.data.download.url);import requests
with open('input.pdf', 'rb') as f:
r = requests.post(
'https://whatapdf.com/api/v1/compress',
headers={'Authorization': 'Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'},
files={'file': f},
data={'level': 'medium'},
)
print(r.json())<?php
$ch = curl_init('https://whatapdf.com/api/v1/compress');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Authorization: Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'],
CURLOPT_POSTFIELDS => [
'file' => new CURLFile('input.pdf', 'application/pdf'),
'level' => 'medium',
],
]);
$res = json_decode(curl_exec($ch), true);
curl_close($ch);
print_r($res);响应示例
{
"data": {
"filename": "report_compressed.pdf",
"original_size": 5242880,
"compressed_size": 2621440,
"savings_percent": 50.0,
"method": "ghostscript",
"download": {
"url": "/api/get-pdf.php?file=...&exp=...&sig=...",
"expires_in": 900
}
},
"meta": { "request_id": "8f9a2b3c1d4e5f6a" }
}POST /merge
按上传顺序将 2–20 个 PDF 合并为单个文档。
POSThttps://whatapdf.com/api/v1/merge
参数
| 名称 | 类型 | 描述 |
|---|---|---|
| files[] * | file[] | 两个或更多 PDF 文件(multipart 数组)。 |
代码示例
curl -X POST https://whatapdf.com/api/v1/merge \
-H "Authorization: Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-F "files[][email protected]" \
-F "files[][email protected]" \
-F "files[][email protected]"const form = new FormData();
for (const f of fileInput.files) form.append('files[]', f);
const res = await fetch('https://whatapdf.com/api/v1/merge', {
method: 'POST',
headers: { 'Authorization': 'Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' },
body: form,
});
console.log(await res.json());import requests
files = [
('files[]', open('a.pdf', 'rb')),
('files[]', open('b.pdf', 'rb')),
('files[]', open('c.pdf', 'rb')),
]
r = requests.post(
'https://whatapdf.com/api/v1/merge',
headers={'Authorization': 'Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'},
files=files,
)
print(r.json())<?php
$ch = curl_init('https://whatapdf.com/api/v1/merge');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Authorization: Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'],
CURLOPT_POSTFIELDS => [
'files[0]' => new CURLFile('a.pdf', 'application/pdf'),
'files[1]' => new CURLFile('b.pdf', 'application/pdf'),
'files[2]' => new CURLFile('c.pdf', 'application/pdf'),
],
]);
echo curl_exec($ch);
curl_close($ch);POST /split
按页面范围将 PDF 拆分为一个或多个部分。每个部分都附带自己的签名下载 URL。
POSThttps://whatapdf.com/api/v1/split
参数
| 名称 | 类型 | 描述 |
|---|---|---|
| file * | file | 源 PDF。 |
| ranges * | string | 逗号分隔的页面范围。一个范围 = 一个输出部分。 e.g. 1-3,5,7-9 |
代码示例
curl -X POST https://whatapdf.com/api/v1/split \
-H "Authorization: Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-F "[email protected]" \
-F "ranges=1-3,5,7-9"const form = new FormData();
form.append('file', fileInput.files[0]);
form.append('ranges', '1-3,5,7-9');
const res = await fetch('https://whatapdf.com/api/v1/split', {
method: 'POST',
headers: { 'Authorization': 'Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' },
body: form,
});
console.log(await res.json());import requests
with open('input.pdf', 'rb') as f:
r = requests.post(
'https://whatapdf.com/api/v1/split',
headers={'Authorization': 'Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'},
files={'file': f},
data={'ranges': '1-3,5,7-9'},
)
for part in r.json()['data']['parts']:
print(part['pages'], part['download']['url'])<?php
$ch = curl_init('https://whatapdf.com/api/v1/split');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Authorization: Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'],
CURLOPT_POSTFIELDS => [
'file' => new CURLFile('input.pdf', 'application/pdf'),
'ranges' => '1-3,5,7-9',
],
]);
print_r(json_decode(curl_exec($ch), true));
curl_close($ch);POST /ocr
使用 Tesseract OCR 从图像中提取文本。支持 20 多种语言。
POSThttps://whatapdf.com/api/v1/ocr
参数
| 名称 | 类型 | 描述 |
|---|---|---|
| image * | file | 要 OCR 的图像(PNG、JPEG、TIFF、WebP 或 BMP)。 |
| language | string | Tesseract 语言代码。默认 eng |
代码示例
curl -X POST https://whatapdf.com/api/v1/ocr \
-H "Authorization: Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-F "[email protected]" \
-F "language=eng"const form = new FormData();
form.append('image', imageInput.files[0]);
form.append('language', 'eng');
const res = await fetch('https://whatapdf.com/api/v1/ocr', {
method: 'POST',
headers: { 'Authorization': 'Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' },
body: form,
});
const { data } = await res.json();
console.log(data.text);import requests
with open('scan.png', 'rb') as f:
r = requests.post(
'https://whatapdf.com/api/v1/ocr',
headers={'Authorization': 'Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'},
files={'image': f},
data={'language': 'eng'},
)
print(r.json()['data']['text'])<?php
$ch = curl_init('https://whatapdf.com/api/v1/ocr');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Authorization: Bearer pdn_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'],
CURLOPT_POSTFIELDS => [
'image' => new CURLFile('scan.png', 'image/png'),
'language' => 'eng',
],
]);
$res = json_decode(curl_exec($ch), true);
echo $res['data']['text'];
curl_close($ch);