简体   繁体   English

如何使用 ReactJS 在浏览器中链接下载为 PDF

[英]How to link for download as PDF in browser using ReactJS

I am looking to provide the link to download the invoice, i first save all the invoices in an array and when the user click any particular invoice it will download in the browser(keep tracking using index number).我希望提供下载发票的链接,我首先将所有发票保存在一个数组中,当用户单击任何特定发票时,它将在浏览器中下载(使用索引号继续跟踪)。 but doing this i am facing issue as the file is not saving and showing as.html format, any suggestion please但是这样做我遇到了问题,因为文件没有保存并显示为.html 格式,请提供任何建议

Snippet of code 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 for console.log(allpdf) console.log(allpdf) 的Screenshot

在此处输入图像描述

in mysql invoice_document is saved as blob in mysql invoice_document保存为blob 在此处输入图像描述

on node.js serversidenode.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)}

The problem is that they are Buffer s, not Blob s:问题是它们是Buffer s,而不是Blob s:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM