TLDR
I want to increase the time available to manipulate the DOM before the print version is generated.
Use case
In a website I work on, we have embedded charts from a 3rd party provider (Qlik). We now want to be able to print the page with the charts using the browser's native print functionality.
The charts need to be redrawn in a different size before printing, otherwise they will look cut off.
The scripts from the 3rd party do subscribe to a resize event, which I can trigger using e.g. window.dispatchEvent(new Event('resize'));
. Or alternatively I can directly call qlik.resize();
.
Problem
On print, if I attempt to trigger a redraw on 'beforeprint', I see the redrawn charts only in the actual webpage, whereas the print preview still shows the old version of the charts before the redraw, or a broken half-redrawn version.
Observations
It seems that starting with the 'beforeprint' event, I still have ~210 milliseconds to alter the DOM, after that further changes have no effect on the printed output.
I get to the 210ms with the following experiment:
// (This sample uses jQuery, adjust as you see fit)
const $display = $('h2#chart-section');
window.addEventListener('beforeprint', function () {
console.log('beforeprint', window.matchMedia('print').matches);
$display.append(' beforeprint');
for (let dt = 0; dt < 1000; dt += 10) {
window.setTimeout(function () {
console.log('beforeprint t0', window.matchMedia('print').matches);
$display.append(' t' + dt);
}, dt);
}
});
The h2
in the printed version will be like '...t210', whereas in the screen version it goes up to '...t990', even while the print preview is open.
Also interestingly the window.matchMedia('print').matches
always gives false
.
Question
How can I force more time to pass before the print version is "frozen"?