使用 NextJS 9 即时返回 PDF
returning a PDF on the fly using NextJS 9
我正在尝试弄清楚如何从 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);
目前无法从 API 个路由或页面读取文件。
我正在尝试弄清楚如何从 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);
目前无法从 API 个路由或页面读取文件。