Force pdf download

Hello :wave:

I think this topic is same: Force download pdf on IOS

For this you have to use api.decorateCookedElement.

Something like this should work :arrow_down_small:

Place it to the Header section to a component.

<script type="text/discourse-plugin" version="0.8.42">
  api.decorateCookedElement(
    element => {
      const pdfFiles = element.querySelectorAll("a.attachment[href$='.pdf']");

      if (!pdfFiles.length) {
        return;
      }

      pdfFiles.forEach((pdf) => {
        pdf.setAttribute("download", "");
      });
    },
    { id: "download-pdf", onlyStream: true }
  );
</script>

I’ve tested it on my device and it works for me but if you want to use other attribute etc… you can easily change it… I hope it helps :slight_smile:

2 Likes