Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.0 kB
1
Indexable
Never
downloadPDF() {
        const pdf = new jsPDF('p', 'mm', 'a4', false);
        const content = this.invoiceDiv.nativeElement;
        const footer = this.footerDiv.nativeElement;

        // Set page dimensions (a4 size)
        const imgWidth = 210;
        const pageHeight = 295;

        html2canvas(content).then((canvas) => {
            const imgData = canvas.toDataURL('image/png', 1); // convert content to img data
            const imgHeight = (canvas.height * imgWidth) / canvas.width;
            let heightLeft = imgHeight; // keep track of amount of content left to be pushed to pdf
            let position = 0;

            // Add content to first page
            pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight); // add a certain portion of the image data to pdf
            if (heightLeft >= pageHeight) {
                // this whole page will be needed
                heightLeft -= pageHeight;
            } else {
                // there will be space left in the page after filling in the content
                heightLeft = pageHeight - heightLeft;
            }

            // Add content to subsequent pages
            if (imgHeight > pageHeight) {
                // need more than 1 page
                while (heightLeft >= 0) {
                    position = heightLeft - imgHeight; // find out the position where the next portion of the img data will be attached
                    pdf.addPage();
                    pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
                    if (heightLeft >= pageHeight) {
                        heightLeft -= pageHeight;
                    } else {
                        heightLeft = pageHeight - heightLeft; // all contents added. calc how much space is left in the last page
                        break;
                    }
                }
            }

            html2canvas(footer).then((footerCanvas) => {
                const footerImgData = footerCanvas.toDataURL('image/png');
                const footerImgHeight = (footerCanvas.height * imgWidth) / footerCanvas.width;

                // Check if there is enough space for footer on current page
                if (heightLeft - footerImgHeight > 0) {
                    position += pageHeight - footerImgHeight + 3; // add the footer at the end of the page (+3 to handle jantrik truti)
                    pdf.addImage(footerImgData, 'PNG', 0, position, imgWidth, footerImgHeight);
                } else {
                    // Add new page and add footer there
                    pdf.addPage();
                    position = 0;
                    pdf.addImage(footerImgData, 'PNG', 0, position, imgWidth, footerImgHeight);
                }
                const padZerosPipe = new PadZerosPipe();
                pdf.save('invoice-' + padZerosPipe.transform(this.invoiceData.InvoiceNo, 3) + '.pdf');
            });
        });
    }