Skip to content

Instantly share code, notes, and snippets.

@yourivdlans
Created November 14, 2011 12:55
Show Gist options
  • Select an option

  • Save yourivdlans/1363897 to your computer and use it in GitHub Desktop.

Select an option

Save yourivdlans/1363897 to your computer and use it in GitHub Desktop.
pdfcrowd split table
var pdfcrowd = {
splitTable: function(tableClass, printableHeight, breakClass) {
var tables = $("." + tableClass);
for(var i=0; i<tables.length; ++i) {
var splitIndices = [0];
var table = $(tables[i]);
var thead = table.children("thead");
var rows = table.children("tbody").children();
var tableTop = table.offset().top;
// find the preceding page break (if any)
var precedingPageBreakTop = 0;
var pageBreaks = $("." + breakClass);
for (var j=pageBreaks.length-1; j>=0; --j) {
var pageBreakTop = $(pageBreaks[j]).offset().top;
if (pageBreakTop < tableTop) {
precedingPageBreakTop = pageBreakTop;
break;
}
}
var currHeight = (tableTop - precedingPageBreakTop) % printableHeight;
rows.each(function(i, row) {
currHeight += $(rows[i]).outerHeight();
if (currHeight > printableHeight) {
splitIndices.push(i);
currHeight = $(rows[i]).outerHeight();
}
});
splitIndices.push(undefined);
// replaceWith returns the table
var wrapper = $('<div></div>');
table = table.replaceWith(wrapper);
table.empty();
for (var j=0; j<splitIndices.length-1; ++j) {
var newTable = table.clone();
// When a header exists, append to first table
if ( i == 0 && j == 0 && thead.length != 0 ){
$("<thead>" + thead.html() + "</thead>").appendTo(newTable);
}
$("<tbody />").appendTo(newTable);
rows.slice(splitIndices[j], splitIndices[j+1])
.appendTo(newTable.children('tbody'));
newTable.appendTo(wrapper);
if (splitIndices[j+1] !== undefined) {
$('<div class="' + breakClass + '"></div>').appendTo(wrapper);
}
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment