Translate

Donnerstag, 12. September 2019

JavaScript download a PDF file

downloadPdf: function (data, fileName) {

        const link = document.createElement("a");

        const blob = base64ToBlob(data);

        link.href = window.URL.createObjectURL(blob);

        link.download = fileName;

        link.dispatchEvent(new MouseEvent(`click`));

}

base64ToBlob: function (base64) {

        const binary = atob(base64.replace(/\s/g, ""));

        const len = binary.length;

        const buffer = new ArrayBuffer(len);

        const view = new Uint8Array(buffer);

        for (let i = 0; i < len; i++) {

            view[i] = binary.charCodeAt(i);

        }

        return new Blob([view], { type: "application/pdf" });
}

Keine Kommentare:

Kommentar veröffentlichen