简体   繁体   中英

how to download pdf in nodejs + express?

I am trying to fill pdf and that pdf need to download.but I am not able to download that pdf I am using this package

https://www.npmjs.com/package/node-pdftk

from the client, I am requesting like this

Get Request

http://localhost:3000/api/joining-form/navasd.sharma@asda.com

export const sendGetRequest = (url, config = {}) => {
    return axios.get(url, config);
}

在此输入图像描述

const buf = await pdftk
                .input('templates/joining-form.pdf')
                .fillForm(data)
                .flatten()
                .output()
            res.send(buf);

在此输入图像描述

currently getting this output

%PDF-1.6
%âãÏÓ
1 0 obj 
<<
/FormType 1
/Subtype /Form
/Resources 
<<
/Font 
<<
/Helv 2 0 R
>>
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
>>
/Type /XObject
/BBox [0 0 362.4 28.8]
/Filter /FlateDecode
/Length 87
/Matrix [1 0 0 1 0 0]
>>
stream

In your client side, you can create a downloadable link base on the request response,

Try this:

sendGetRequest(
  "http://localhost:3000/api/joining-form/navasd.sharma@asda.com",
  {
    responseType: "blob" // important
  }
).then(response => {
  const url = window.URL.createObjectURL(new Blob([response.data]));
  const link = document.createElement("a");
  link.href = url;
  link.setAttribute("download", "file.pdf");
  document.body.appendChild(link);
  link.click();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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