簡體   English   中英

在 Node.js 中使用遠程圖像提供動態生成的 PDF

[英]Serve dynamically generated PDF with remote images in Node.js

我正在嘗試創建一個 Node 服務器,該服務器使用PDFKit即時生成 PDF。 PDF 是基於來自 POST 請求(通過Express )的參數生成的。 其中一個參數指定一個圖像 URL,服務器下載該 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 已損壞(根據預覽)。 任何有關為什么會發生這種情況的提示都非常受歡迎。

在調試這個問題時,我發現了幾件事:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM