Skip to content

Instantly share code, notes, and snippets.

@FreeFly19
Last active July 29, 2016 13:57
Show Gist options
  • Select an option

  • Save FreeFly19/030e69cfcbde3aede02c48d19597979a to your computer and use it in GitHub Desktop.

Select an option

Save FreeFly19/030e69cfcbde3aede02c48d19597979a to your computer and use it in GitHub Desktop.
Bootstrap prepare page to print
var COLUMN_TYPES = ["col-sm-", "col-md-", "col-lg-"];
var mapToRevert = {};
var widthBefore = null;
var collapsedElements = null;
function beforePrint() {
widthBefore = document.body.style.width;
document.body.style.width = "600px";
collapsedElements = $('.collapse');
collapsedElements.removeClass('collapse');
for(var i = 1; i <= 12; i++) {
COLUMN_TYPES.forEach(function (columnType) {
var className = columnType + i;
var elems = $('.' + className);
elems
.toggleClass(className)
.toggleClass("col-xs-" + i);
mapToRevert[className] = {elems: elems, size: i};
});
}
}
function afterPrint() {
document.body.style.width = widthBefore;
collapsedElements.addClass('collapse');
for(var className in mapToRevert) {
if(mapToRevert.hasOwnProperty(className)) {
var elems = mapToRevert[className].elems;
var columnSize = mapToRevert[className].size;
elems.addClass(className).removeClass("col-xs-" + columnSize);
}
}
}
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}
window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment