2

我正在尝试使用 nodejs 填写 pdf 表单。

我尝试使用 node-pdftk 包进行相同的操作。执行以下步骤:

已安装 pdftk for windows 将路径映射到环境变量 PATH 已安装 node-pdf 包

`const express = require('express')
const app = express()
const cors = require('cors')
const pdftk = require('node-pdftk');
const bodyParser = require('body-parser');
var multer = require('multer'); // v1.0.5
var upload = multer();

app.listen(8000, () => {
console.log('Server started!')
});
var pdfPath='OoPdfFormExample.pdf';
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
var formdata = {
'Given Name': 'Swetha',
'Family Name': 'Gulapannavar'
}
app.post('/api/file', upload.array(), (req, res, next) => {
//var buffer=JSON.stringify(Buffer.from(req.body));
var buffer=Buffer.from(req.body)
    pdftk
    .input(pdfPath)
    .fillForm(formdata)
    .flatten()
    .output()
    .then(buffer => {
        // Still returns a buffer
        res.type('application/pdf'); // If you omit this line, file will 
download
        res.send(buf);
        })
    .catch(err => {
        res.send(err.message)
        // handle errors
    });
});`

但是当我尝试执行相同的操作时出现以下错误。写入 EPIPE 错误。

4

1 回答 1

0

这可能是由于 node-pdftk 无法找到 PDFtk 二进制文件造成的;您的 PATH 变量可能没有为运行您的 Web 服务的帐户正确设置。您可以使用 node-pdftk 的配置功能直接在应用程序内部设置 bin 路径,该功能在 node-pdftk npm项目页面上有简要描述。如果这不起作用,请尝试配置 tempDir 路径。

于 2020-10-12T20:27:50.727 回答