PDFLAyer使用网址?

时间:2018-07-16 21:53:11

标签: node.js

我有这段代码,就像标题说的那样,我希望使用由document_html插入的document_url下来,有人知道如何做吗?我已经尝试将代码扩展到我想要的qhat,但是什么也没有发生。任何帮助都将很棒

let request = require("request");
    let fs = require('fs');

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({
    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('path/name.pdf');
        stream.once('open', function (fd) {
            stream.write(body);
            stream.end();
        });
        console.log('File written')
        return;
    }            
})

0 个答案:

没有答案