在节点js中生成html-pdf时出现pdf,出现“ ERR_HTTP_HEADERS_SENT”错误

时间:2020-01-01 17:37:16

标签: javascript node.js html-pdf

我一直在尝试使用html-pdf在Node JS中生成pdf。我已经成功完成了此操作,但是当我将文件从Mac移至Windows继续进行该项目时,我在服务器上生成pdf时遇到了令人沮丧的“ ERR_HTTP_HEADERS_SENT”错误。我尝试了这里建议的解决方法-Node JS - Headers error when trying to create PDF-不幸的是没有运气。非常感谢任何帮助,非常感谢:)

客户端代码:

axios.post("/create-paeds-head-injury-pdf", printPdfState)
            .then(() => axios.get("fetch-pdf", { responseType: "blob" }))
            .then(res => {
                const pdfBlob = new Blob([res.data], { type: "application/pdf" });

                saveAs(pdfBlob, "paedsHi.pdf");
            });

服务器端代码:

const express = require('express');
const bodyParser = require('body-parser');
const pdf  = require('html-pdf');
const cors = require('cors');
const mongoose = require('mongoose');

const paedsHeadInjuryPDF = require('./documents/paedHeadInjuryPdf');

const app = express();

console.log('Test Server Started!');

app.use(cors());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

app.post('/create-paeds-head-injury-pdf', (req, res) => {
    pdf.create(paedsHeadInjuryPDF(req.body), {}).toFile('paedsHi.pdf', (err) => {
        if(err) {
           res.send(Promise.reject());
        }
        res.send(Promise.resolve());  
    });
});


app.get('/fetch-pdf', (req, res) => {
    res.sendFile(`${__dirname}/paedsHi.pdf`)
});

app.use(express.static('client/build'));

const path = require('path');
app.get('*', (req, res) => {
    res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
})

const port = process.env.PORT || 5000;
app.listen(port, () => console.log('Listening on port ' + port));

0 个答案:

没有答案