使用Node.js动态生成PDF并显示远程图片

8
我正在尝试创建一个使用PDFKit的Node服务器,实时生成PDF。PDF是基于通过Express的POST请求的参数生成的。其中一个参数指定了一个图像URL,服务器会下载该图像并将其注入PDF中。
现在,我的结构如下:
// Get dependencies
var express = require('express'),
http = require('http'),
fs = require('fs'),
pdfDocument = require('pdfkit');

// Get express started.
var app = express();

// Use JSON in POST body
app.use(express.json());

// Setup POST response
app.post('/post_pdf', function(req, res) {
    // Get the PDF initialized
    var doc = new pdfDocument();

    // Set some headers
    res.statusCode = 200;
    res.setHeader('Content-type', 'application/pdf');
    res.setHeader('Access-Control-Allow-Origin', '*');

    // Header to force download
    res.setHeader('Content-disposition', 'attachment; filename=Untitled.pdf');     

    // Pipe generated PDF into response
    doc.pipe(res);

    /**
     * Generate PDF contents
     */

    // Prepare write stream for image
    var image = fs.createWriteStream('image.jpeg');

    // Download image
    http.get("http://dummyimage.com/640.jpeg", function(response) {

        // Pipe response into image write stream
        // (because PDFKit needs to read from a saved file)
        response.pipe(image).on('close', function() {

            // Read data back, make sure there are no errors
            fs.readFile('image.jpeg', function(err, data) {
                if (err) throw err;

                /**
                 * Use `data` to get image info (width, height, etc.)
                 * ------------------
                 * Inject image
                 */

                // Close document and response
                doc.end();
                res.end();
                return;
            })
        });
    });
});

我有两个问题:

  • 是否有更简洁的方法来完成这个任务,或许可以减少嵌套回调函数?我完全可以添加另一个依赖项来简化生活。
  • 目前,上面的代码无法正常工作。它返回了一个PDF,但是该PDF已损坏(根据预览)。任何关于为什么会发生这种情况的提示都非常欢迎。
2个回答

8

在调试这个问题时,我发现了几件事:

PDFKit无需从文件中读取信息。它也可以接受一个Buffer

doc.image(myBuffer); // You don't have to use a path string

如果将文件直接传输到响应中,如果文件已经关闭,则手动调用response.end()会导致问题

doc.pipe(res); // Pipe document directly into the response

doc.end(); // When called, this ends the file and the response

// res.end(); <-- DON'T call res.end()
//                The response was already closed by doc.end()
return;

Request 是一个非常实用的 NodeJS 库,可以简化回调函数的嵌套层级。


更新后的代码:

var express = require('express'),
request = require('request'),
pdfDocument = require('pdfkit');

// Start Express
var app = express();

// Use JSON in POST body
app.use(express.json());

// Setup POST response
app.post('/post_pdf', function(req, res) {
    // Create PDF
    var doc = new pdfDocument();

    // Write headers
    res.writeHead(200, {
        'Content-Type': 'application/pdf',
        'Access-Control-Allow-Origin': '*',
        'Content-Disposition': 'attachment; filename=Untitled.pdf'
    });

    // Pipe generated PDF into response
    doc.pipe(res);

    // Process image
    request({
        url: 'http://dummyimage.com/640.jpeg',
        encoding: null // Prevents Request from converting response to string
    }, function(err, response, body) {
        if (err) throw err;

        // Inject image
        doc.image(body); // `body` is a Buffer because we told Request
                         // to not make it a string

        doc.end(); // Close document and, by extension, response
        return;
    });
});

2
你如何处理客户端部分以使用户能够下载生成的 PDF?我正在尝试动态生成,不想保存到文件系统,只想为用户启动下载。谢谢。 - itaylorweb
2
我相信将 Content-Disposition 标头更改为类似于 inline; filename=" 的内容会起作用。告诉浏览器显示 PDF 文件。 - Tyler Eich
如果我想把页面/网址转换成PDF文件,怎么办?比如www.google.com。这个方法还适用吗? - mr.b
据我所知,不,这个答案不适用于通过快照网页生成的PDF。 - Tyler Eich
你如何在客户端处理响应? - gabrielAnzaldo
@itaylorweb 你可以这样做:https://dev59.com/_V8e5IYBdhLWcg3wZ5us,对我有用。 - gabrielAnzaldo

2
const fetch = require('node-fetch');
const PDFDocument = require('pdfkit');
const doc = new PDFDocument({});
url = AnyImageUrl;
res = await fetch(url,{encoding: null });
imageBuffer = await res.buffer();
img = new Buffer(imageBuffer, 'base64');
doc.image(img,(doc.page.width - 525) /2, doc.y, {align: 'center', width: 125});

1
这对我非常有效。我喜欢它的简单性。我最终改变的唯一一件事(除了最后一行中的变量名称和尺寸)是将“new Buffer(imageBuffer,'base64');”更改为“new Buffer.from(imageBuffer,'base64');”-使用Buffer的.from方法,因为使用Buffer现在已被弃用。 - Cole Paciano

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接