Node.js 幻影html到pdf下载pdf到客户端

Node.js 幻影html到pdf下载pdf到客户端,node.js,pdf,express,Node.js,Pdf,Express,我在express Node服务器中使用phantom html to pdf库,并且我能够生成pdf,但是它只是在页面上显示为jibberish负载 将此页面另存为.pdf时,我正试图对其进行编码,因此,除了将文件作为下载推送到客户端之外,其他一切都正常工作 我的代码是: router.get('/generatePdf', function(req, res, next) { // handle the form that is submitted to this page.

我在express Node服务器中使用phantom html to pdf库,并且我能够生成pdf,但是它只是在页面上显示为jibberish负载

将此页面另存为
.pdf
时,我正试图对其进行编码,因此,除了将文件作为下载推送到客户端之外,其他一切都正常工作

我的代码是:

router.get('/generatePdf', function(req, res, next) {
    // handle the form that is submitted to this page.
    var conversion = require("phantom-html-to-pdf")();
    conversion({
        numberOfWorkers: 2,
        tmpDir: "os/tmpdir",
        phantomPath: require("phantomjs-prebuilt").path,
        url: "https://google.com"
    }, function(err, pdf) {
        console.log(pdf.logs);
        console.log(pdf.numberOfPages);

        pdf.stream.pipe(res) // this is the issue! 

    });
})

我相信我在
pdf.stream.pipe(res)
函数上做错了什么,如果说实话,我并不真正理解这个函数

您可能需要设置
内容类型
标题,以便客户端知道它正在获取PDF文件。
res.setHeader('Content-Type','application/PDF')
pdf.stream.pipe(res)
works:)您知道如何在新选项卡中打开它吗?