Javascript 我想使用pdflayer将原始html转换为pdf
对不起,我英语不好 我想把html转换成pdf。所以我使用了“pdflayer”。Javascript 我想使用pdflayer将原始html转换为pdf,javascript,node.js,Javascript,Node.js,对不起,我英语不好 我想把html转换成pdf。所以我使用了“pdflayer”。 我写这个代码。但不起作用。 我期待pdf格式的“hello world”。 但pdf是白皮书。不显示“hello world”。 怎么了 var request = require('request'); var fs = require('fs'); var baseUrl = 'http://api.pdflayer.com/api/convert'; var accessKey = '****'; var
我写这个代码。但不起作用。
我期待pdf格式的“hello world”。
但pdf是白皮书。不显示“hello world”。
怎么了
var request = require('request');
var fs = require('fs');
var baseUrl = 'http://api.pdflayer.com/api/convert';
var accessKey = '****';
var documentHtml = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>The HTML5 Herald</title>
<meta name="description" content="The HTML5 Herald">
<meta name="author" content="SitePoint">
</head>
<body>
hello world
</body>
</html>`;
request.post(`${baseUrl}?access_key=${accessKey}`, {form: {document_html: documentHtml }},function (err, response, body) {
fs.writeFile('./download.pdf', response.body, function(err) {
if (err) console.log('error: ', err);
})
})
你应该加上
encoding: null
到您的requeust.post
方法
这是必需的,因为post请求返回二进制数据(base64)。您可以在此处查看数据类型示例:
您可以在postman上看到您的请求是返回二进制数据:https://i.stack.imgur.com/QTB18.png
下载窗口显示数据类型为“base64二进制”:https://i.stack.imgur.com/QTB18.png
如果请求返回二进制数据,则应使用编码:null
。那么变量将不会被编码。它将是一个缓冲变量
encoding—用于响应数据的setEncoding的编码。如果为null,则返回主体作为缓冲区。其他任何内容(包括undefined的默认值)都将作为编码参数传递给toString()(这意味着默认情况下这实际上是utf8)。(注意:如果需要二进制数据,则应将编码设置为空。)
来源:https://github.com/request/request
因此,您的完整代码是:
var request = require('request');
var fs = require('fs');
var baseUrl = 'http://api.pdflayer.com/api/convert';
var accessKey = '*****';
var documentHtml = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>The HTML5 Herald</title>
<meta name="description" content="The HTML5 Herald">
<meta name="author" content="SitePoint">
</head>
<body>
hello world
</body>
</html>`;
request.post(`${baseUrl}?access_key=${accessKey}`, {
form: {
document_html: documentHtml
},
//Set the encoding to buffer object
encoding: null
}, function (err, response, body) {
fs.writeFile('c://temp//myfile.pdf', response.body, function (err) {
if (err) console.log('error: ', err);
})
})
或者,您可以使用以下库 可以使用此库将PDF图层与节点一起使用。
首先,我要验证是否有传入数据。检查response.body或body是否可用-它可能未定义。在继续之前,您可能需要检查的另一件事是,在写入文件之前没有错误
var request = require('request');
var fs = require('fs');
var baseUrl = 'http://api.pdflayer.com/api/convert';
var accessKey = '*****';
var documentHtml = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>The HTML5 Herald</title>
<meta name="description" content="The HTML5 Herald">
<meta name="author" content="SitePoint">
</head>
<body>
hello world
</body>
</html>`;
request.post(`${baseUrl}?access_key=${accessKey}`, {
form: {
document_html: documentHtml
},
//Set the encoding to buffer object
encoding: null
}, function (err, response, body) {
fs.writeFile('c://temp//myfile.pdf', response.body, function (err) {
if (err) console.log('error: ', err);
})
})
let request = require("request");
let fs = require('fs');
request({
method: "POST",
url: 'http://api.pdflayer.com/api/convert',
qs: {
access_key: '****',
page_size: 'A4',
test: '1'
},
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
form: {document_html: documentHTML, '': ''},
encoding: null
}, function (error, response, body) {
if (error) {
throw error
} else {
console.log(body); // `body` is a Buffer because we told Request
// to not make it a string
var stream = fs.createWriteStream("c:\\Temp\\write.pdf");
stream.once('open', function (fd) {
stream.write(body);
stream.end();
});
console.log('File written')
return;
} ;
})