PDF旋转
说明 将1个PDF文件旋转,度数值可选-270、-180、-90、90、180、270度。
接口说明
- 请求地址
https://pdf-api.pdfai.cn/v1/pdf/rotate_pdf
- 请求方式
POST
请求Body
JSON格式
{
"app_key": "app_key_test",
"input": "https://static.pdfai.cn/static/example/files/big.pdf",
"angle": 90
}
字段说明
名称 | 类型 | 必须 | 描述 |
---|---|---|---|
app_key | String | 是 | app_key_test ,固定值 |
input | String | 是 | 输入文件,可访问互联网url,比如 https://static.pdfai.cn/static/example/files/four_pages.pdf |
angle | Int | 是 | 旋转度数,值可选90、180、270度 |
代码例子:支持 PHP、Node、Js、Go、C++、Python、Java 等
CURL
curl -X 'POST' \
'https://pdf-api.pdfai.cn/v1/pdf/rotate_pdf' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"app_key": "app_key_test",
"input": "https://static.pdfai.cn/static/example/files/big.pdf",
"angle": 90
}'
PHP
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://pdf-api.pdfai.cn/v1/pdf/rotate_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/big.pdf',
'angle' => 90
])
]);
$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/rotate_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/big.pdf',
angle: 90
}
};
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/rotate_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/big.pdf',
angle: 90
})
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})
.catch(error => {
console.error('Error:', error);
});
Golang
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
// 构造请求数据
requestBody, err := json.Marshal(map[string]interface{}{
"app_key": "app_key_test",
"input": "https://static.pdfai.cn/static/example/files/big.pdf",
"angle": 90,
})
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
// 创建请求
req, err := http.NewRequest("POST", "https://pdf-api.pdfai.cn/v1/pdf/rotate_pdf", bytes.NewBuffer(requestBody))
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
req.Header.Set("accept", "application/json")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
defer resp.Body.Close()
// 读取响应
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Response:", string(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/big.pdf\","
"\"angle\":90}";
// 设置请求头
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/rotate_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/rotate_pdf'
headers = {
'accept': 'application/json',
'Content-Type': 'application/json'
}
data = {
'app_key': 'app_key_test',
'input': 'https://static.pdfai.cn/static/example/files/big.pdf',
'angle': 90
}
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 RotatePdfExample {
public static void main(String[] args) {
try {
URL url = new URL("https://pdf-api.pdfai.cn/v1/pdf/rotate_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/big.pdf\"," +
"\"angle\":90}";
// 发送请求
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/rotation_90.pdf"
},
"code_msg": "成功"
}
- code 等于 200 代表成功,其他值代表失败