app.factory("PDF", function() { return { tableToPDF: function() { var pdf = new jsPDF('p', 'pt', 'a4'); var rows = $("table").find("tr"); var pdfContainer = $(".tmp-pdf-container"); var pdfInternals = pdf.internal; var pdfPageSize = pdfInternals.pageSize; var pdfPageWidth = pdfPageSize.width; var pdfPageHeight = pdfPageSize.height; var partialSize = 10; var contentSize = 0; var marginTop = 20; var index = 0; // partialSize var generatePartial = function() { var partial = ""; rows.each(function(i, row) { if (i >= index && i <= partialSize) { partial += "<tr>" + $(this).html() + "</tr>"; index++; if (index >= partialSize) { partialSize += 10; return false; } } }); return partial; } var processCanvases = function() { if (index >= rows.length) { pdfContainer.html(""); //pdf.output("datauri"); pdf.save("TEST.pdf"); return; } var partial = generatePartial(); // generate table with that rows var table = $(document.createElement("table")); table.append("<tbody>" + partial + "</tbody>"); // insert table to temporary div pdfContainer.html("<table class='table table-fixed-width table-condensed'>" + table.html() + "</table>"); // hide unnecessary columns pdfContainer.find(".non-printable").css("display", "none"); // generate canvas from that table html2canvas(pdfContainer, {background: "white"}).then(function(canvas) { // contentSize // 4 partial, // , if (contentSize < 2) { contentSize ++; pdf.addImage(canvas, "jpeg", 20, marginTop, pdfPageWidth-40, 0); // // 0.01 , 0.05, , marginTop += canvas.height/ (canvas.width / pdfPageHeight + (pdfPageWidth / pdfPageHeight) - 0.05); } else { pdf.addImage(canvas, "jpeg", 20, marginTop, pdfPageWidth-40, 0); // , if (index < rows.length) { pdf.addPage(); } contentSize = 0; marginTop = 0; } // next iteration processCanvases(); }); } processCanvases(); } } });
<div class="tmp-pdf-container" style="position: absolute; left: -9999; width: 1000px"></div>
.table-fixed-width { table-layout: fixed; }
Source: https://habr.com/ru/post/278047/
All Articles