【问题标题】:Can't customize the name of PDF file generated by 'html-pdf' npm module无法自定义 'html-pdf' npm 模块生成的 PDF 文件的名称
【发布时间】:2018-09-07 10:04:42
【问题描述】:

我正在使用 html-pdf 节点模块通过 nodemailer 生成 pdf 和电子邮件。一切都是工作文件。我现在面临的唯一一个问题是我无法设置生成的 pdf 文件的名称。我附上了生成pdf函数的代码。

const generatePdf = (data, user, startDate, endDate, linkRes) => {
  return new Promise((res, rej) => {
    console.log("*&&&&& Here in generate Report", linkRes);
    const body = renderToString( <App meetingsData={data} userData={user} startDate={startDate} endDate={endDate} linkRes={linkRes} /> );
    const title = 'Therify Meeting Report';
    let returnObj = {};
    const phantomPath = require('witch')('phantomjs-prebuilt', 'phantomjs');
    const html = Html({body, title});
    console.log("*&&&&& path", phantomPath);

    
    const options = {
      format: 'A4',
      orientation: 'portrait',
      // phantomPath: `${phantomPath}`,
      header: {
        "height": "3mm",
      },
      footer: {
        "height": "5mm",
        "contents": {
          default: '<span style="color: #444;">{{page}}</span>/<span>{{pages}}</span>',
        }
      },
    };
    
    pdf.create(html, options).toBuffer(function (err, file) {
      console.log('This is a file:', file);
      if(err) {
        returnObj.data = `Pdf Generation Failed`;
        returnObj.status = 500;
        res(returnObj);
      }else {
        buffer = file;
        returnObj.data = `Pdf Generated Successfully`;
        returnObj.status = 200;
        res(returnObj);
      }
    });
  });
}

生成的 pdf 文件的名称设置为 'attachment-1.pdf'。我想改变它。这是生成的pdf的图片。

【问题讨论】:

    标签: javascript reactjs pdf html-pdf


    【解决方案1】:

    这样使用

    将以下代码保留在 pdf.create 函数中,希望它能正常工作

    res.setHeader("Content-Disposition","attachment;Abc.pdf");
    

    【讨论】:

    • 实际上,我得到了它的工作。 pdf 文件的名称未在 Nodemailer 中设置。所以 Nodemailer 使用默认值作为文件名。我解决了这个问题。谢谢,顺便说一句。
    猜你喜欢
    • 2016-08-12
    • 2015-09-24
    • 2018-08-16
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 2014-04-15
    • 2022-10-05
    相关资源
    最近更新 更多