【问题标题】:Serve dynamically generated PDF with remote images in Node.js在 Node.js 中使用远程图像提供动态生成的 PDF
【发布时间】:2014-05-18 05:21:14
【问题描述】:

我正在尝试创建一个使用PDFKit 即时生成 PDF 的 Node 服务器。 PDF 是根据来自 POST 请求的参数生成的(通过Express)。其中一个参数指定了一个图像 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 已损坏(根据预览)。任何关于为什么会发生这种情况的提示都非常受欢迎。

【问题讨论】:

    标签: javascript node.js express node-pdfkit


    【解决方案1】:

    在调试这个问题的过程中,我发现了几件事:

    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;
        });
    });
    

    【讨论】:

    • 您如何处理客户端部分以使用户能够下载生成的pdf?我正在尝试动态生成并且不想保存到文件系统,而只是为用户启动下载。谢谢。
    • 我相信将Content-Disposition 标头更改为inline; filename=" 之类的内容会起作用。告诉浏览器显示 PDF。
    • 如果我想将页面/网址转换为 pdf。说 www.google.com。这仍然适用吗?
    • @ShenLance 据我所知,不,此答案不适用于通过快照网页生成的 PDF。
    • 如何处理客户端的响应?
    【解决方案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});
    

    【讨论】:

    • 这对我很有用。我喜欢它的简单。我最终改变的唯一一件事(除了最后一行中的变量名称和尺寸)是“新缓冲区(imageBuffer,'base64');”到“新 Buffer.from(imageBuffer, 'base64');” - 使用 Buffer 的 .from 方法,因为现在不推荐使用 Buffer。
    猜你喜欢
    • 2010-11-07
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 2020-05-12
    • 1970-01-01
    • 2018-04-04
    相关资源
    最近更新 更多