El siguiente código funciona correctamente para descargar imágenes remotas, pero no funcionó al modificarlo para descargar un archivo PDF remoto, aquí está el código que estoy usando:
<a href="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" data-remote>Remote PDF</a> <script defer type="text/javascript"> const a = document.querySelector('a[data-remote]') a.addEventListener('click', async (e) => { e.preventDefault() const file = await fetch(e.target.href); const blob = await file.blob(); const blobUrl = URL.createObjectURL(blob); const downloadLink = document.createElement("a"); downloadLink.href = blobUrl; downloadLink.download = 'file.pdf'; downloadLink.click(); }) </script>
Puede agregar la propiedad de download
a a
etiqueta y eliminar el script para descargar el pdf.
<a href="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" download >Remote PDF</a>
O si desea usar js para descargarlo, considere agregar mode:'no-cors'
cuando busque