Force pdf download

AI-generated summary

In a support thread titled ‘Force pdf download,’ Rafael_Melo_Silva seeks a way to force PDF downloads instead of opening them in an iOS app, especially for files hosted on S3. He notes the challenge of modifying nearly 3,000 existing PDF links. thoka suggests using the download HTML attribute via a theme component, referencing a guide on forcing file downloads. itsbhanusharma recommends using Discourse’s remap feature to bulk-update links, cautioning to test on a staging site first. Don provides a working JavaScript solution using api.decorateCookedElement to automatically add the download attribute to all .pdf links in attachments, tested successfully on iOS. He notes the code can be customized for other attributes or behaviors. The solution targets attachment links and can be added via a theme component’s header section. The thread also references a similar topic for context here.

I am using s3 for host my files on my discourse community, i made ios app and i need to download pdf files instead of open in the app

I found a solution for a link download:

<a href="download-url?wtn-download-file=true"> download File</a>

But for pdf uploaded on s3 i don`t know what to do.

Edit: removing my answer as I misunderstood the topic :person_facepalming:

Thanks thoka :+1:

Hm. It seems, he demands exactly the opposite of my feature request …

According to this source, you should be able to achieve the desired behavior by adding download to these links via a theme component.

I need to find a way to change all my pdf links… it`s almost 3000 links

Should be a matter of discourse remap

Word of caution: Test it first on a staging site. It might not work as expected.

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: