使用NextJS 9快速返回PDF

问题描述 投票:0回答:1

我试图弄清楚如何从nextjs 9 api向客户端发送动态创建的pdf。我不确定这是nextjs错误,文件路径错误还是webpack错误。指导会很棒。

文件路径= /pages/api/pdf.js

var fs = require('fs')
export default function handle(req, res) {
    console.log(req.body) // The request body
    console.log(req.query) // The url querystring
    console.log(req.cookies) // The passed cookies
        var stream = fs.createReadStream(destinationPDF);
        var filename = "filled.pdf"; 
        // Be careful of special characters

        filename = encodeURIComponent(filename);
        // Ideally this should strip them

        res.setHeader('Content-disposition', 'inline; filename="' + filename + '"');
        res.setHeader('Content-type', 'application/pdf');

        stream.pipe(res);

}

错误错误:无法打开PDF文件:../../public/static/85.pdf解析为localhost:3000 / static / 85.pdf

关注问题:

 let data = _babel_runtime_corejs2_core_js_object_assign__WEBPACK_IMPORTED_MODULE_0___default()(fdfData, fieldNames);
express pdf webpack next.js
1个回答
0
投票

目前无法从API路由或页面读取文件。

https://github.com/zeit/next.js/pull/8334

© www.soinside.com 2019 - 2024. All rights reserved.