jsPDF页面拆分超过页面大小后会中断内容

时间:2018-12-19 10:39:10

标签: jquery pdf jspdf html2canvas

我有这个代码。谁能告诉我如何在不影响内容的情况下拆分页面?

$('#export').click(function(e) {
  e.preventDefault();
  var options = {
    pagesplit: true,
    margin: {
      top: 10,
      right: 10,
      bottom: 10,
      left: 10,
      useFor: 'page'
    }
  };
  var pdf = new jsPDF('p', 'mm', 'a4');
  pdf.internal.scaleFactor = 5;
  pdf.page = 1;
  //  var width = "1000px"
  //   var height = "1000px"
  pdf.addHTML($("#pdfdownload"), 0, 0, options, function() {
    // function footer() {
    var pageCount = pdf.internal.getNumberOfPages();
    for (i = 0; i < pageCount; i++) {
      pdf.setPage(i);
      pdf.text(10, 10, pdf.internal.getCurrentPageInfo().pageNumber + "/" + pageCount);
    };
    pdf.save('Audit.pdf');
  });
});

1 个答案:

答案 0 :(得分:0)

let HTML_Width = $("#target").width();
            let HTML_Height = $("#target").height();
            let top_left_margin = 1;
            let PDF_Width = HTML_Width + (top_left_margin * 2);
            let PDF_Height = (PDF_Width * 1.5) + (top_left_margin * 2);
            let canvas_image_width = HTML_Width;
            let canvas_image_height = HTML_Height;
            let totalPDFPages = Math.ceil(HTML_Height / PDF_Height) - 1;
            let user = this.auth_user;
            html2canvas($("#target")[0], {allowTaint: true}).then(function (canvas) {
                canvas.getContext('2d');
                let imgData = canvas.toDataURL("image/jpeg", 1.0);
                let pdf = new jsPDF('p', 'pt', [PDF_Width, PDF_Height]);

                pdf.addImage(imgData, 'JPG', top_left_margin, top_left_margin, canvas_image_width, canvas_image_height);

                let counter = 0;

                for (let i = 1; i <= totalPDFPages; i++) {
                    counter++;
                    pdf.addPage(PDF_Width, PDF_Height);
                    pdf.addImage(imgData, 'JPG', top_left_margin, -(PDF_Height * i) + (top_left_margin * 4), canvas_image_width, canvas_image_height);
                }
                pdf.save(user + ".pdf");
            });

对我有用