【问题标题】:How to link for download as PDF in browser using ReactJS如何使用 ReactJS 在浏览器中链接下载为 PDF
【发布时间】:2020-07-29 01:17:47
【问题描述】:

我希望提供下载发票的链接,我首先将所有发票保存在一个数组中,当用户单击任何特定发票时,它将在浏览器中下载(使用索引号继续跟踪)。但是这样做我遇到了问题,因为文件没有保存并显示为 .html 格式,请提供任何建议

Snippet的代码

function Useraccount({ userinfo, userstatus, allInvoices }) {
  let allPdf = [];

  allInvoices.map((datax) => {
    let onlypdf = datax.invoice_document;

    allPdf.push(onlypdf);
  });
  console.log(allpdf); // screenshot of this console below

  return (
    <>
      <TableContainer component={Paper}>
        <Table>
          <TableHead>
            <TableRow>
              <TableCell>Invoice No </TableCell>
              <TableCell> Date of Purchase</TableCell>
              <TableCell> Description</TableCell>
              <TableCell> Invoice</TableCell>
            </TableRow>
          </TableHead>
          <TableBody>
            {allInvoices.map((eachInvoice, index) => {
              console.log(eachInvoice);
              let invoiceNo1 = eachInvoice.invoiceNo;
              let date_of_purchase1 = eachInvoice.date_of_purchase;

              return (
                <TableRow key={invoiceNo1}>
                  <TableCell>{invoiceNo1}</TableCell>
                  <TableCell>{date_of_purchase1}</TableCell>
                  <TableCell>TBO</TableCell>
                  <TableCell>
                    <a href='#' download={allPdf[index]}>
                      invoiceNo:{invoiceNo1}
                    </a>
                  </TableCell>
                </TableRow>
              );
            })}
          </TableBody>
        </Table>
      </TableContainer>
    </>
  );
}

Screenshot 用于 console.log(allpdf)

in mysql invoice_document 保存为blob

node.js 服务器端

app.post("/api/invoice", (req, res) => {
 pdf.create(pdfTemplate(customer_dataand_Itemsbought), {}).toFile(`./${user_details.Invoice_No_latest}.pdf`, function (err, res) {}

connection.query(
                  "UPDATE users_basket SET invoice_document=? WHERE invoiceNo=?;",
                  [`${__dirname}/${user_details.Invoice_No_latest}.pdf`, user_details.Invoice_No_latest],
                  function (err, results) {}
 connection.query("SELECT * FROM  users_basket WHERE users_user_id=?;", [user_id], function (err, results) { res.json(results)}

【问题讨论】:

标签: javascript arrays reactjs object pdf


【解决方案1】:

问题是他们是Buffers,而不是Blobs:

<a href="#" href={new Blob([allPdf[index].toString()], { type: "application/pdf"})} download={`invoice-${invoiceNo1}.pdf`}>
  invoiceNo:{invoiceNo1}
</a>

【讨论】:

  • 感谢 Elias,我面临同样的问题,因为文件显示失败且不存在文件。虽然当我在控制台中看到时,我可以看到每次迭代... invoice_document: {type: "Buffer", data: Array(56)} (如上代码所示)。还想提一下,我正在从保存为 blob 类型的 mysql 中检索。
  • 我已经更新了上面的服务器端配置,在我保存到mysql之前我可以看到本地存储中生成的pdf文件
  • 我更新了我的答案,虽然你应该查看你的数据库,根据你的截图返回的文件只有 56 字节(我认为它们是文件名的原因),还要指定你想要返回的列防止泄漏,看stackoverflow.com/a/5959238/11986202
猜你喜欢
  • 2020-11-19
  • 2022-11-14
  • 2016-03-24
  • 2018-12-11
  • 1970-01-01
  • 2018-02-14
  • 1970-01-01
  • 2018-07-13
  • 2016-12-14
相关资源
最近更新 更多