Document Conversion API
Convert PDF, Word, PowerPoint, Excel, and OpenDocument files with one POST request — including turning a PDF back into an editable DOCX or XLSX.
cURL
curl -X POST \ .../api/v1/convert.php \ -H "Authorization: Bearer ..." \ -F "category=document" \ -F "target=PDF" \ -F "file=@report.docx" \ -o output.pdf
POST
https://transconvert.com/api/v1/convert.php
参数
| Field | Description |
|---|---|
Authorization | Required. "Bearer tc_live_...". |
category | Set to "document". |
target | Required. Output format code, e.g. "PDF", "DOCX", "XLSX". |
file | Required. The document to convert. |
示例
TransConvert
curl -X POST \ https://transconvert.com/api/v1/convert.php \ -H "Authorization: Bearer tc_live_your_key_here" \ -F "category=document" \ -F "target=PDF" \ -F "file=@report.docx" \ -o output.pdf
<?php $ch = curl_init('https://transconvert.com/api/v1/convert.php'); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => ['Authorization: Bearer tc_live_your_key_here'], CURLOPT_POSTFIELDS => [ 'category' => 'document', 'target' => 'PDF', 'file' => new CURLFile('report.docx'), ], ]); $response = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($status === 200) { file_put_contents('output.pdf', $response); } else { $error = json_decode($response, true); echo $error['error']['message']; }
const form = new FormData(); form.append('category', 'document'); form.append('target', 'PDF'); form.append('file', new Blob([fs.readFileSync('report.docx')]), 'report.docx'); const res = await fetch('https://transconvert.com/api/v1/convert.php', { method: 'POST', headers: { Authorization: 'Bearer tc_live_your_key_here' }, body: form, }); if (res.ok) { fs.writeFileSync('output.pdf', Buffer.from(await res.arrayBuffer())); } else { const { error } = await res.json(); console.error(error.message); }
import requests with open('report.docx', 'rb') as f: response = requests.post( 'https://transconvert.com/api/v1/convert.php', headers={'Authorization': 'Bearer tc_live_your_key_here'}, data={'category': 'document', 'target': 'PDF'}, files={'file': f}, ) if response.status_code == 200: with open('output.pdf', 'wb') as out: out.write(response.content) else: print(response.json()['error']['message'])
# gem install multipart-post require 'net/http' require 'net/http/post/multipart' url = URI('https://transconvert.com/api/v1/convert.php') File.open('report.docx') do |file| req = Net::HTTP::Post::Multipart.new url, 'category' => 'document', 'target' => 'PDF', 'file' => UploadIO.new(file, 'application/octet-stream', 'report.docx') req['Authorization'] = 'Bearer tc_live_your_key_here' res = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http| http.request(req) end if res.code == '200' File.write('output.pdf', res.body) else puts JSON.parse(res.body)['error']['message'] end end
// Gradle: implementation("com.squareup.okhttp3:okhttp:4.+") OkHttpClient client = new OkHttpClient(); RequestBody body = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("category", "document") .addFormDataPart("target", "PDF") .addFormDataPart("file", "report.docx", RequestBody.create(new File("report.docx"), MediaType.parse("application/octet-stream"))) .build(); Request request = new Request.Builder() .url("https://transconvert.com/api/v1/convert.php") .header("Authorization", "Bearer tc_live_your_key_here") .post(body) .build(); try (Response response = client.newCall(request).execute()) { if (response.isSuccessful()) { Files.write(Paths.get("output.pdf"), response.body().bytes()); } else { System.err.println(response.body().string()); } }
响应
成功时(200):返回转换后文件的原始字节,并设置相应的 Content-Type 和 Content-Disposition 响应头。失败时:返回如 {"error": {"code": "...", "message": "..."}} 结构的 JSON,并附带相应的 HTTP 状态码——请参见下方的错误说明。
| Header | Value |
|---|---|
Content-Type | 转换后文件的实际 MIME 类型(例如 image/png、application/pdf)。 |
Content-Disposition | attachment; filename="..."——建议的文件名,与普通文件下载相同。 |
Content-Length | 响应体的大小(字节)。 |
所有错误都遵循相同的 JSON 结构,例如超出额度时:
429 Too Many Requests
{
"error": {
"code": "quota_exceeded",
"message": "Monthly API allowance of 5000 conversion-minutes reached.",
"limit": 5000,
"used": 5000
}
}
错误
每个失败请求都会返回一个 JSON 错误信息,其中包含可供代码判断分支的 "code",以及便于阅读的 "message"。部分错误还包含额外字段(例如 quota_exceeded 会包含 "limit" 和 "used")。
| Status & code | When it happens |
|---|---|
401 missing_key | 未发送 Authorization 请求头。 |
401 invalid_key | 密钥不存在,或已被撤销。 |
403 account_suspended | 拥有此密钥的账户已被暂停。 |
403 plan_required | 该账户为 Free 套餐——API 访问权限需要 Basic、Lite、Pro 或 Team 套餐。 |
400 invalid_category | "category" 既不是 "image" 也不是 "document"。 |
400 missing_target | "target" 为空。 |
400 no_file | 未发送文件,或上传失败——字段名必须为 "file"。 |
413 file_too_large | 文件大小超出您所在套餐的最大上传限制。 |
429 quota_exceeded | 该套餐每月的转换分钟数已用完,将于下个自然月初重置。 |
429 concurrency_limit | 此账户同时运行的转换任务过多(与网站共用限额)——请等待其中一个完成后重试。 |
422 conversion_failed | 文件本身无法转换——"message" 中会说明原因。状态码会因原因而异:400/415/422 表示无论重试多少次,该文件或目标格式都无法成功;500/503 表示服务器端出现问题,其中 503 值得进行一次短暂重试。 |
Supported formats
可作为源格式:
PDF, DOCX, DOC, PPTX, PPT, XLSX, XLS, RTF, ODT, ODP, ODS, HTML
可作为目标格式:
PDF, DOCX, DOC, PPTX, PPT, XLSX, XLS, RTF, ODT, ODP, ODS