尝试在节点中合并pdf文件:PDFUnite + Nodejs

时间:2015-08-21 23:45:39

标签: node.js pdf heroku

我在heroku上有一个网站,并且正在使用HyPDF插件,它允许您以多种方式操作pdf。具体来说,我正在尝试合并两个我拥有URL的pdf。

然而,它不起作用。我正在使用请求来提取pdf文件,然后使用request来调用PDFUnite。这也是一个回调地狱,所以如果你对如何解决这个问题有任何建议,我会非常感激!

谢谢!

request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=4010e80d-9106-4824-95ab-799ec81c7fd0', encoding: null}, function(err, res, body) {
var pdf1 = body;

    request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=28a388c6-ca0e-45a1-9aaf-9b6688c5a557', encoding: null}, function(err2, res2, body2){
        var pdf2 = body2;

        request.post(
        'https://www.hypdf.com/htmltopdf',
        {
        json: {
            user: HY_USER,
            password: HY_PASS,
            file_1: pdf1,
            file_2: pdf2
        }
        },
        function (error3, res3, body3) {

          if (!error && res2.statusCode == 200) {
            console.log('Public URL: ', body3.url);
            console.log('Number of pages: ', res3.headers['hypdf-pages']);
          }
        });
    })
});

/ ******** UDPATED ***************** /

我已使用https://gist.github.com/redfield/6724717作为指南更新了代码。区别在于代码示例使用文件,而我正在使用URL。

我试图对它进行适当的修改,但显然有些东西已经关闭......如果我使用body或body.toString('base64')作为pdf1和pdf2,那么我会收到400状态错误。否则,如果我使用res作为pdf文件,那么我得到504错误。我不确定我应该如何发送文件,所以它只是猜测和检查。感谢帮助!

request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=4010e80d-9106-4824-95ab-799ec81c7fd0', encoding: null}, function(err, res, body) {
    var pdf1 = body;

        request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=28a388c6-ca0e-45a1-9aaf-9b6688c5a557', encoding: null}, function(err2, res2, body2){
            var pdf2 = body2;

            var form = new FormData();
            form.append('user', HYPDF_USER);
            form.append('password', HYPDF_PASSWORD);
            form.append('test', 'true');
            // form.append('bucket', 'hypdf_test');
            form.append('key', 'hypdf_test.pdf');
            form.append('public', 'true');
            form.append('file1', pdf1);
            form.append('file2', pdf2);

            form.submit('https://www.hypdf.com/pdfunite', function(err, res) {
                console.log('err ', err);
                // res – response object (http.IncomingMessage)
                console.log(res.statusCode, res.resume());
            });         
        })
    });

更新#2 我已经更新了将代码与下面的@remus代码相结合的代码。但是,我仍然在request.post行上收到错误 - “无法调用方法'hasOwnProperty'为null”。有什么想法吗?

request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=4010e80d-9106-4824-95ab-799ec81c7fd0', encoding: null}, function(err, res, body) {
  var pdf1 = body.toString('base64');

    request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=28a388c6-ca0e-45a1-9aaf-9b6688c5a557', encoding: null}, function(err2, res2, body2){
        var pdf2 = body2.toString('base64');

        var form = new FormData();
        form.append('user', HY_UN);
        form.append('password', HY_PASS);
        form.append('public', 'true');
        form.append('file1', fs.createReadStream(pdf1));
        form.append('file2', fs.createReadStream(pdf1));

        request.post({
          url: 'https://www.hypdf.com/pdfunite', 
          formData: form
        }, function(e, r, body) {
          // body should be the binary result of the merged .pdf
          console.log('e', e);
          console.log('body', body);
        });

      })
  });

更新3

request.post的错误堆栈 - 我尝试了很多东西,无法摆脱它。有什么想法吗?

TypeError: Cannot call method 'hasOwnProperty' of null
    at appendFormValue (/node_modules/request/request.js:340:17)
    at Request.init (/node_modules/request/request.js:354:11)
    at new Request (/node_modules/request/request.js:140:8)
    at request (/node_modules/request/index.js:55:10)
    at Function.post (/node_modules/request/index.js:63:12)
    **at Request._callback (/server/api/emailInvoice/emailInvoice.controller.js:34:12)**
    at Request.self.callback (/node_modules/request/request.js:198:22)
    at Request.emit (events.js:98:17)
    at Request.<anonymous> (/node_modules/request/request.js:1073:14)
    at Request.emit (events.js:117:20)
    at IncomingMessage.<anonymous> (/node_modules/request/request.js:1019:12)
    at IncomingMessage.emit (events.js:117:20)
    at _stream_readable.js:929:16
    at process._tickCallback (node.js:419:13)

1 个答案:

答案 0 :(得分:1)

首先,主要问题是您尝试在上次POST请求中将二进制数据(.pdf)作为JSON发布。我想这不会起作用 - 就像它期望多部分文件数据一样?其次,修复回调地狱的一种简单方法是使用

var request = require('request');
var async = require('async');

async.parallel(function(callback) {
    pdf1: function(callback) {
        request.get({
            url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=4010e80d-9106-4824-95ab-799ec81c7fd0'
        }, function(err, r, body) {
            callback(err, body);
        });
    },
    pdf2: function(callback) {
        request.get({
            url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=28a388c6-ca0e-45a1-9aaf-9b6688c5a557'
        }, function(err, r, body) {
            callback(err, body);
        });
    },
    function(err, results) {
        var formData = {
            user: HY_USER,
            password: HY_PASS,
            file_1: fs.createReadStream(results.pdf1),
            file_2: fs.createReadStream(results.pdf2)
        }
        request.post({
            url: 'https://www.hypdf.com/pdfunite',
            formData: formData
        }, function(e, r, body) {
            // body should be the binary result of the merged .pdf
        });
    });
});

请注意二进制数据流中的fs.createReadStream和使用pdfunite的正确网址。

您可能还想查看hypdf NPM模块 - 我没有对其进行测试,但它可能比手动构建请求更容易。

**** **** UPDATE

问题在于使用FormData()。出于某种原因,request.post并不喜欢它。我还能够通过将请求流直接注入表单来删除回调。另请注意,您必须将编码设置为null。

    var finalpdf;
    var formData = {
        'user': HY_USER,
        'password': HY_PASSWORD,
        'test': 'true',
        'public': 'true',
        'file1': request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=4010e80d-9106-4824-95ab-799ec81c7fd0', encoding: null}),
        'file2': request({url: 'http://www.sesamestreet.org/cms_services/services?action=download&uid=28a388c6-ca0e-45a1-9aaf-9b6688c5a557', encoding: null})
    }


    request.post({url: 'https://www.hypdf.com/pdfunite', encoding: null,     formData: formData}, function(err, res3, body3){
       finalPdf = body3.toString('base64');
});