当我使用 Pdfkit 创建第二个 PDF 时,此文件已损坏 ExpressJS

问题描述

我正在尝试使用 PDFkit 创建 PDF 并将其直接发送以供下载而不保存文件。当我创建第一个 PDF 时,它工作正常,但是当我尝试创建第二个 PDF 时,此文件已损坏。这是我的快递代码

Express createFile.js

Controller.exportsFile = async (req,res) => {
        if (req.body.type === "pdf") {
            doc.fontSize(16)
               .font('Times-Bold')
               .text("Resultado de la búsqueda de apartamentos",{
                width: 450,align: 'center'
                })
               .moveDown();
            
            for (const row of JSON.parse(req.body.data)) {
                doc.fontSize(12).font('Times-Roman')
                   .text(`Título: ${row.Titulo} \n
                          Anunciante: ${row.Anunciante} \n
                          Descripción: ${row.Descripcion} \n
                          Teléfono: ${row.Telefonos} \n
                          Tipo de vivienda: ${row.Tipo} \n
                          Precio: ${row.Precio} € \n
                          Dirección: ${row.Direccion} \n
                        `,{
                            width: 450,align: 'center'
                        })
                    .addPage();
            }
            res.set({
                'Content-disposition': 'attachment; filename=reporte.pdf','Content-Type': 'application/pdf','Access-Control-Allow-Origin': '*'
            });
            
            doc.end();
            doc.pipe(res);
}

也许问题出在视图上,我正在使用 Pug。在链接中,我发送了在服务器中创建 PDF 所需的数据(数据集)。我使用 fetch 和 Post 方法

view.pug

const download = document.querySelector(".report");            
            download.addEventListener('click',async (e) => {
                e.preventDefault() 
                let data = download.dataset.array;
                    fetch('/apartamentos/exportar',{
                        method: 'post',headers: {
                            'Content-Type': 'application/json'
                        },body: JSON.stringify({type: "pdf",data: data}) 
                    })
                    .then(async res => ({
                            filename: 'reporte.pdf',blob: await res.blob()
                        }))
                    .then(resObj => {
                        
                        const newBlob = new Blob([resObj.blob],{ type: 'application/pdf' });

                        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                            window.navigator.msSaveOrOpenBlob(newBlob);
                        } else {

                            const objUrl = window.URL.createObjectURL(newBlob);

                            let link = document.createElement('a');
                            link.href = objUrl;
                            link.download = resObj.filename;
                            link.click();

                            setTimeout(() => { window.URL.revokeObjectURL(objUrl); },250);
                        }
                    })
                    .catch((error) => {
                        console.log('DOWNLOAD ERROR',error);
                    });                               
            });

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)