图片转PDF
说明 单张图片或多张图片转换成1个pdf
接口说明
- 请求地址
https://pdf-api.pdfai.cn/v1/pdf/image_to_pdf
- 请求方式
POST
请求Body
JSON格式
{
"app_key": "app_key_test",
"input": [
"https://static.pdfai.cn/static/example/files/1.jpg",
"https://static.pdfai.cn/static/example/files/2.png"
]
}
字段说明
名称 | 类型 | 必须 | 描述 |
---|---|---|---|
app_key | String | 是 | app_key_test ,固定值 |
input | Json | 是 | 输入文件,可访问互联网url,比如["https://static.pdfai.cn/static/example/files/1.jpg","https://static.pdfai.cn/static/example/files/2.png"] |
代码例子:支持 PHP、Node、Js、Go、C++、Python、Java 等
CURL
curl -X 'POST' \
'https://pdf-api.pdfai.cn/v1/pdf/image_to_pdf' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"app_key": "app_key_test",
"input": [
"https://static.pdfai.cn/static/example/files/1.jpg",
"https://static.pdfai.cn/static/example/files/2.png"
]
}'
PHP
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://pdf-api.pdfai.cn/v1/pdf/image_to_pdf',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'accept: application/json',
'Content-Type: application/json'
],
CURLOPT_POSTFIELDS => json_encode([
'app_key' => 'app_key_test',
'input' => [
'https://static.pdfai.cn/static/example/files/1.jpg',
'https://static.pdfai.cn/static/example/files/2.png'
]
])
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Node
const request = require('request');
const options = {
url: 'https://pdf-api.pdfai.cn/v1/pdf/image_to_pdf',
method: 'POST',
headers: {
'accept': 'application/json',
'Content-Type': 'application/json'
},
json: {
app_key: 'app_key_test',
input: [
'https://static.pdfai.cn/static/example/files/1.jpg',
'https://static.pdfai.cn/static/example/files/2.png'
]
}
};
request(options, function(error, response, body) {
if (error) {
console.error('Error:', error);
} else {
console.log('Response:', body);
}
});
Js
fetch('https://pdf-api.pdfai.cn/v1/pdf/image_to_pdf', {
method: 'POST',
headers: {
'accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
app_key: 'app_key_test',
input: [
'https://static.pdfai.cn/static/example/files/1.jpg',
'https://static.pdfai.cn/static/example/files/2.png'
]
})
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})
.catch(error => {
console.error('Error:', error);
});
Golang
const request = require('request');
const options = {
url: 'https://pdf-api.pdfai.cn/v1/pdf/image_to_pdf',
method: 'POST',
headers: {
'accept': 'application/json',
'Content-Type': 'application/json'
},
json: {
app_key: 'app_key_test',
input: [
'https://static.pdfai.cn/static/example/files/1.jpg',
'https://static.pdfai.cn/static/example/files/2.png'
]
}
};
request(options, function(error, response, body) {
if (error) {
console.error('Error:', error);
} else {
console.log('Response:', body);
}
});
C++
#include <curl/curl.h>
#include <string>
#include <iostream>
// 回调函数处理响应数据
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* userp) {
userp->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main() {
CURL* curl = curl_easy_init();
std::string response;
if(curl) {
// 构造请求数据
const char* json = "{\"app_key\":\"app_key_test\","
"\"input\":[\"https://static.pdfai.cn/static/example/files/1.jpg\","
"\"https://static.pdfai.cn/static/example/files/2.png\"]}";
// 设置请求头
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "accept: application/json");
// 配置CURL选项
curl_easy_setopt(curl, CURLOPT_URL, "https://pdf-api.pdfai.cn/v1/pdf/image_to_pdf");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
// 执行请求
CURLcode res = curl_easy_perform(curl);
// 检查错误
if(res != CURLE_OK) {
std::cerr << "Error: " << curl_easy_strerror(res) << std::endl;
} else {
std::cout << "Response: " << response << std::endl;
}
// 清理
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
return 0;
}
Python
import requests
import json
url = 'https://pdf-api.pdfai.cn/v1/pdf/image_to_pdf'
headers = {
'accept': 'application/json',
'Content-Type': 'application/json'
}
data = {
'app_key': 'app_key_test',
'input': [
'https://static.pdfai.cn/static/example/files/1.jpg',
'https://static.pdfai.cn/static/example/files/2.png'
]
}
try:
response = requests.post(url, headers=headers, json=data)
print('Response:', response.json())
except Exception as e:
print('Error:', str(e))
Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class ImageToPdfExample {
public static void main(String[] args) {
try {
URL url = new URL("https://pdf-api.pdfai.cn/v1/pdf/image_to_pdf");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("accept", "application/json");
conn.setDoOutput(true);
// 构造请求数据
String jsonInput = "{\"app_key\":\"app_key_test\"," +
"\"input\":[" +
"\"https://static.pdfai.cn/static/example/files/1.jpg\"," +
"\"https://static.pdfai.cn/static/example/files/2.png\"" +
"]}";
// 发送请求
try (OutputStream os = conn.getOutputStream()) {
byte[] input = jsonInput.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
// 读取响应
try (BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println("Response: " + response.toString());
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
返回结果
{
"code": 200,
"data": {
"file_url": "https://static.pdfai.cn/static/example/out/imageToPdf_1.pdf"
},
"code_msg": "成功"
}
- code 等于 200 代表成功,其他值代表失败