[Fixed] Force download the pdf file – Angular 6+

Issue

I need to download the pdf file from the URL. I have tried the window.open(URL, ‘Download’) method but it does not download the file and it opens in the new browser tab.

downloadPDFFile() {
     window.open('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf', 'Download');
    }

Solution

import * as fs from 'file-saver';
   
 downloadFile(url) {
            var filename = url.split('/').pop();
            try {
                fs.saveAs(url, filename);
            }
            catch (e) {
                console.log(e)
            }
        }

Leave a Reply

(*) Required, Your email will not be published