What I want is very simple, the user gets a printed result like WYSIWYG. After different approaches, I came to the idea of Image Replacement.
The idea: since the generated SVG doesn't response to width changes, we have to use a responsive image instead of the SVG to get the perfect print result.
So here is the implementation. :)
HTML:
<div id='timeline'></div>
<div id='timeline_print'></div>
CSS:
@media print {
#timeline_print {
width: 100%;
}
}
JavaScript(assumed that jQuery is loaded, this.chart refers to the Google Charts object):
var beforePrint = function() {
$("#timeline_print").html('<img src="' + this.chart.getImageURI() + '">');
$("#timeline").addClass('hidden');
};
var afterPrint = function() {
$("#timeline_print").empty();
$("#timeline").removeClass('hidden');
};
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}
window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
No comments:
Post a Comment