Ng2-pdf-viewer: Download option

Created on 29 May 2017  路  6Comments  路  Source: VadimDez/ng2-pdf-viewer

In my application I need people to be able to download the PDF file that I'm displaying, is there a way to do that?

stale

Most helpful comment

For who is still looking for a solution:

Typescript:

private pdf: PDFDocumentProxy;

onLoaded(pdf: PDFDocumentProxy) {
    this.pdf = pdf;
}

download() {
    this.pdf.getData().then((u8) => {
        let blob = new Blob([u8.buffer], {
            type: 'application/pdf'
        });
        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
            // IE11 and Edge
            window.navigator.msSaveOrOpenBlob(blob, 'Certificate.pdf');
        } else {
            // Chrome, Safari, Firefox, Opera
            let url = URL.createObjectURL(blob);
            this.openLink(url);
            // Remove the link when done
            setTimeout(function () {
                window.URL.revokeObjectURL(url);
            }, 5000);
        }
    });
}

private openLink(url: string) {
    let a = document.createElement('a');
    // Firefox requires the link to be in the body
    document.body.appendChild(a);
    a.style.display = 'none';
    a.href = url;
    a.download = 'Certificate.pdf';
    a.click();
    // Remove the link when done
    document.body.removeChild(a);
}

All 6 comments

@kevinariasf
Do you use an URL or Uint8Array for the source?

@thwefl I use an Uint8Array, btw I need them to be able to print it too

I did with simply call this function on click

printpdf() {
this.winRef.open(this.pdfSrc).print();
}

Are you thinking of adding a download option? I use a URL for the source. right now I navigate them away from this viewer and just href them to a new tab using the URL. this gives them all the viewing, downloading and printing we want.... but it defeats the purpose of having the viewer which we want.

For who is still looking for a solution:

Typescript:

private pdf: PDFDocumentProxy;

onLoaded(pdf: PDFDocumentProxy) {
    this.pdf = pdf;
}

download() {
    this.pdf.getData().then((u8) => {
        let blob = new Blob([u8.buffer], {
            type: 'application/pdf'
        });
        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
            // IE11 and Edge
            window.navigator.msSaveOrOpenBlob(blob, 'Certificate.pdf');
        } else {
            // Chrome, Safari, Firefox, Opera
            let url = URL.createObjectURL(blob);
            this.openLink(url);
            // Remove the link when done
            setTimeout(function () {
                window.URL.revokeObjectURL(url);
            }, 5000);
        }
    });
}

private openLink(url: string) {
    let a = document.createElement('a');
    // Firefox requires the link to be in the body
    document.body.appendChild(a);
    a.style.display = 'none';
    a.href = url;
    a.download = 'Certificate.pdf';
    a.click();
    // Remove the link when done
    document.body.removeChild(a);
}

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

UlricW picture UlricW  路  3Comments

Gianlu82b19 picture Gianlu82b19  路  4Comments

GarciaFreelancer picture GarciaFreelancer  路  6Comments

xyrintech picture xyrintech  路  5Comments

Vigneshjana picture Vigneshjana  路  3Comments