PDF转图片
PDF转图片
说明 将1个PDF中指定的页面拆分成单个图片(PNG)格式或长图(JPG)格式,并可以设置图片的质量大小。
接口说明
请求地址
request_url
参考基础 > request_url
请求参数,POST请求
名称 | 类型 | 必须 | 描述 |
---|---|---|---|
method | String | 是 | pdf.splitToImage,固定值 |
token | String | 是 | 可自行创建或更新,参考基础 > token |
async | String | 否 | 任务方式,默认0,代表同步;1代表异步,异步需要设置回调地址,或者主动查询结果 |
input | String | 是 | 输入文件,可访问互联网url,比如 https://static.easyyun.com/static/example/files/one_page.pdf |
options | String | 是 | json格式,比如{"quality": "middle", "page": "1"} |
options字段说明
- quality 必须。图片质量,可选值:high、middle、low
- page 必须。支持1;1-3;2,3;1-N,N代表最后一页,默认1-N;-1,长图
响应参数,类型为JSON
名称 | 类型 | 示例值 | 描述 |
---|---|---|---|
request_id | String | xxx | 任务id |
code | String | 200 | 见code说明 |
data | String | {"file_url": "xxx"} | json格式消息内容 |
code_msg | String | "成功" | code描述 |
PDF转图片请求代码示例
- CURL
- PHP
- Node
- Golang
curl -X POST 'https://test-rest-api.easyyun.com/v1/router/rest' \
-d 'method=pdf.splitToImage' \
-d 'token=395a25d6fa758bfb6c0d3da007a8b189' \
-d 'async=0' \
-d 'input=https://static.easyyun.com/static/example/files/four_pages.pdf' \
-d 'options={"quality": "middle", "page": "1-N"}'
<?php
// PDF转图片
$url = 'https://test-rest-api.easyyun.com/v1/router/rest';
$method = 'pdf.splitToImage';
//测试token,无法更新,使用参考 https://www.easyyun.com/docs/api/base#token
$token = '395a25d6fa758bfb6c0d3da007a8b189';
$input = 'https://static.easyyun.com/static/example/files/four_pages.pdf';
// $options: quality图片质量为中,1-N为多页
$options = json_encode([
'quality' => 'middle',
'page' => '1-N', // (多张图片)
]);
$body = [
'method' => $method,
'token' => $token,
'input' => $input,
'options' => $options
];
$body_string = http_build_query($body, '', '&');
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body_string);
$response = curl_exec($curl);
echo $response;
?>
// PDF转图片
var request = require("request");
var url = "https://test-rest-api.easyyun.com/v1/router/rest";
var method = "pdf.splitToImage";
//测试token,无法更新,使用参考 https://www.easyyun.com/docs/api/base#token
var token = "395a25d6fa758bfb6c0d3da007a8b189";
var input = "https://static.easyyun.com/static/example/files/four_pages.pdf";
// options: quality图片质量为中,1-N为多页
var options = JSON.stringify({quality: 'middle',page: '1-N'});
var body = "method=" + method + "&token=" + token + "&input=" + input + "&options=" + options;
request({
url: url,
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded",
},
body: body
}, function(error, response, body) {
console.log(body);
});
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// PDF转图片
func main() {
url := "https://test-rest-api.easyyun.com/v1/router/rest"
method := "pdf.splitToImage"
// token,测试token,无法更新,使用参考 https://www.easyyun.com/docs/api/base#token
token := "395a25d6fa758bfb6c0d3da007a8b189"
// input 远程文件地址
input := "https://static.easyyun.com/static/example/files/four_pages.pdf"
// options: quality图片质量为中,1-N为多页
options := "{\"quality\": \"middle\", \"page\": \"1-N\"}"
payload := strings.NewReader("method=" + method + "&token=" + token + "&input=" + input + "&options=" + options)
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("content-type", "application/x-www-form-urlencoded")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
响应示例
{
"request_id": "xxx",
"code": "200",
"data": {
"file_url": ["https://static.easyyun.com/static/example/out/splitImage_1.png","https://static.easyyun.com/static/example/out/splitImage_2.png","https://static.easyyun.com/static/example/out/splitImage_3.png","https://static.easyyun.com/static/example/out/extract/splitImage_4.png"]
},
"code_msg": "请求成功"
}