How do I get a multi-page pdf from a website using jsPDF and HTML2Canvas?

前端 未结 5 1906
生来不讨喜
生来不讨喜 2021-02-19 05:03

I have a script that uses HTML2Canvas to take a screenshot of a div within the page, and then converts it to a pdf using jsPDF.

The problem is the pdf that

5条回答
  •  日久生厌
    2021-02-19 05:40

    This is my aproach, also with jspdf and html2canvas which did a very good job for me:

    I am using a wrapper node which holds the content to be converted to pdf:

      Download PDF

    This is the JS integration ( I only checked this versions ) and call:

    
    
    
    

    And here you can see the pdf generation. I encapsulated the generation part in an extra js object (you need to include this file also):

    //assuming jquery is in use
    
    let pdfView = function(){
        //assuming documentSlug is a constant set earlier 
        this.documentSlug = documentSlug;
        //assuming documentTitle is a constant set earlier
        this.documentTitle = documentTitle;
    
        //in this html node the part which shall be converted to pdf
        this.nodeId = "main-content";
    };
    
    pdfView.prototype.generate = function(){
        let self = this;
        this.prepareDocumentToPrint();
    
        //variables
        let HTML_Width = $("#"+self.nodeId).width();
        let HTML_Height = $("#"+self.nodeId).height();
        let top_left_margin = 15;
        let PDF_Width = HTML_Width+(top_left_margin*2);
        let PDF_Height = (PDF_Width*1.5)+(top_left_margin*2);
    
        this.pdfWidth = PDF_Width;
        this.pdfHeight = PDF_Height;
    
        let canvas_image_width = HTML_Width;
        let canvas_image_height = HTML_Height;
    
        let totalPDFPages = Math.ceil(HTML_Height/PDF_Height)-1;
    
        html2canvas($("#"+self.nodeId)[0],{allowTaint:true, scale: 2}).then(function(canvas) {
            canvas.getContext('2d');
    
            //console.log(canvas.height+"  "+canvas.width);
    
            let imgData = canvas.toDataURL("image/jpeg", 1.0);
            let pdf = new jsPDF('p', 'mm',  [PDF_Width, PDF_Height]);
            pdf.addImage(imgData, 'JPG', top_left_margin, top_left_margin,canvas_image_width,canvas_image_height);
    
            pdf = self.addPdfHeader(pdf);
            pdf = self.addPdfFooter(pdf, 1);
    
            for (let i = 1; i <= totalPDFPages; i++) {
                pdf.addPage(PDF_Width, PDF_Height);
    
                pdf.addImage(imgData, 'JPG', top_left_margin, -(PDF_Height*i)+(top_left_margin*4) + top_left_margin,canvas_image_width,canvas_image_height);
    
                pdf = self.addPdfHeader(pdf);
                pdf = self.addPdfFooter(pdf, (i+1));
            }
    
            pdf.save(self.documentSlug+".pdf");
    
            self.resetDocumentAfterPrint();
    
        });
    };
    
    //this one sets a fixed viewport, to be able to 
    //get the same pdf also on a mobile device. I also
    //have a css file, which holds the rules for the 
    //document if the body has the .printview class added
    pdfView.prototype.prepareDocumentToPrint = function(){
        let viewport = document.querySelector("meta[name=viewport]");
        viewport.setAttribute('content', 'width=1280, initial-scale=0.1');
        $('body').addClass("printview");
        window.scrollTo(0,0);
    };
    
    pdfView.prototype.addPdfHeader = function(pdf){
    
        pdf.setFillColor(255,255,255);
        pdf.rect(0, 0, this.pdfWidth, 40, "F");
    
        pdf.setFontSize(40);
        pdf.text("Document: "+this.documentTitle+" - Example Lmtd. Inc. (Whatsoever)", 50, 22);
    
        return pdf;
    };
    
    pdfView.prototype.addPdfFooter = function(pdf, pageNumber){
    
        pdf.setFillColor(255,255,255);
        pdf.rect(0, pdf.internal.pageSize.height - 40, this.pdfWidth, this.pdfHeight, "F");
    
        pdf.setFontSize(40);
        pdf.text("Seite "+pageNumber, 50, pdf.internal.pageSize.height - 20);
        return pdf;
    };
    
    //and this one resets the doc back to normal
    pdfView.prototype.resetDocumentAfterPrint = function(){
        $('body').removeClass("printview");
        let viewport = document.querySelector("meta[name=viewport]");
        viewport.setAttribute('content', 'device-width, initial-scale=1, shrink-to-fit=no');
    };
    

    my css example for the .printview case:

    body.printview #header,
    body.printview footer
    {
        display: none !important;
    }
    
    body.printview{
        margin-top: 0px !important;
    }
    
    body.printview #main-content{
        margin-top: 0px !important;
    }
    
    body.printview h1{
        margin-top: 40px !important;
    }
    

    enjoy

    Contributions to @Code Spy for the Link you provided, which was the basis for this aporach.

提交回复
热议问题