html api 在线阅读,HTML to PDF API

$> curl -i https://api.sejda.com/v2/html-pdf\

--fail --silent --show-error \

--header "Content-Type: application/json" \

--header "Authorization: Token: api_Y0URAP1K3YH3R3" \

--data '{"url": "https://airtable.com", "viewportWidth":1200 }' > out.pdf

// npm install request

const request = require('request');

const fs = require('fs');

var opts = {

uri: 'https://api.sejda.com/v2/html-pdf',

headers: {

'Authorization' : 'Token: ' + apiKey,

},

json: {

'url': 'https://airtable.com',

'viewportWidth': 1200

}

};

request.post(opts)

.on('error', function(err){

return console.error(err);

})

.on('response', function(response) {

if (response.statusCode === 200) {

response.pipe(fs.createWriteStream('/tmp/out.pdf'))

.on('finish', function () {

console.log('PDF saved to disk');

});

} else {

return console.error('Got code: ' + response.statusCode);

}

});

import requests

url = 'https://api.sejda.com/v2/html-pdf'

r = requests.post(url, json = {

'url': 'https://airtable.com',

'viewportWidth': 1200

}, headers = {

'Authorization': 'Token: {}'.format(apiKey)

})

open('/tmp/out.pdf', 'wb').write(r.content)

document.getElementById('downloadPdfBtn')

.addEventListener('click', function(e){

SejdaJsApi.htmlToPdf({

filename: 'out.pdf',

/* leave blank for one long page */

pageSize: 'a4',

publishableKey: 'api_public_y0urap1k3yh3r3',

htmlCode: document.querySelector('html').innerHTML,

/* url: window.location.href */

always: function(){

// PDF download should have started

}

});

})

/* Don't forget to configure your API Allowed domains, on your account page. */

org.apache.httpcomponents

httpclient

4.5.8

import org.apache.http.HttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.ContentType;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClientBuilder;

CloseableHttpClient httpClient = HttpClientBuilder.create().build();

HttpPost request = new HttpPost("https://api.sejda.com/v2/html-pdf");

request.setHeader(HttpHeaders.AUTHORIZATION, "Token: " + apiKey);

request.setEntity(new StringEntity(

"{\"url\": \"https://airtable.com\", \"viewportWidth\":1200 }",

ContentType.APPLICATION_JSON));

HttpResponse response = httpClient.execute(request);

if (response.getStatusLine().getStatusCode() == 200) {

try (InputStream in = response.getEntity().getContent();

OutputStream out = new FileOutputStream(new File("/tmp/out.pdf"))) {

byte[] buffer = new byte[8 * 1024];

int bytesRead;

while ((bytesRead = in.read(buffer)) != -1) {

out.write(buffer, 0, bytesRead);

}

}

}

$url = "https://api.sejda.com/v2/html-pdf";

$content = json_encode(array('url' => 'https://airtable.com'));

$curl = curl_init($url);

curl_setopt($curl, CURLOPT_HEADER, false);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_HTTPHEADER, array(

"Content-type: application/json",

"Authorization: Token: " . $apiKey));

curl_setopt($curl, CURLOPT_POST, true);

curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($status == 200) {

$fp = fopen("out.pdf", "w");

fwrite($fp, $response);

fclose($fp);

print("PDF saved to disk");

} else {

print("Error: failed with status $status, response $response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));

}

using System.Net.Http;

using Newtonsoft.Json.Linq;

var baseUrl = "https://api.sejda.com/v2/html-pdf";

using (var client = new HttpClient())

{

client.DefaultRequestHeaders.TryAddWithoutValidation(

"Authorization", "Token: " + apiKey

);

var json = new JObject();

json.Add("url", "https://airtable.com");

json.Add("viewportWidth", 1200);

//json.Add("htmlCode", File.ReadAllText(@"c:\doc.html", Encoding.UTF8));

var content = new StringContent(json.ToString(), Encoding.UTF8, "application/json");

using (var res = await client.PostAsync(baseUrl, content))

{

var statusCode = (int)res.StatusCode;

if (statusCode == 200)

{

var httpStream = await res.Content.ReadAsStreamAsync();

var outputFilePath = Path.GetTempFileName();

using (var fileStream = File.Create(outputFilePath))

using (var reader = new StreamReader(httpStream))

{

httpStream.CopyTo(fileStream);

fileStream.Flush();

}

// outputFilePath contains the PDF output

}

else

{

// retry, then handle error

throw new Exception("Unexpected response code: " + statusCode);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值