crabbly / Print.js

A tiny javascript library to help printing from the web.
MIT License
4.32k stars 674 forks source link

PrintJS with fetch/blob for PDF file not working #574

Closed ghost closed 3 years ago

ghost commented 3 years ago

I am unable to get the print dialog popping up and there is no error in the console. I assume it's to do with the blob not being converted properly, but after researching, it looks like I do have all the elements for it to work. Any ideas?

const onPrint = () => {
    const url =
      "https://www.homes4u.co.uk/wp-content/uploads/2020/09/3.22-Move-Out-Guide.pdf";
    fetch(url)
      .then((response) => {
        response.blob();
      })
      .then((blob) => {
        let pdfBlob = new Blob([blob], { type: "application/pdf" });
        let blobUrl = URL.createObjectURL(pdfBlob);

        printJS({
          printable: blobUrl,
          type: "pdf",
          showModal: true,
          onError: (err) => {
            console.log(err, "err");
          },
        });
      })
      .catch((err) => console.log(err, "err"));
  };