I want to print a page, so I searched the forums and followed the proposed solution:
- the calling form triggers an event, when the “print button” is clicked, like:
def lnk_print_starpvda_click(self, **event_args):
"""This method is called when the link is clicked"""
self.cst_starpvda_print_1.raise_event('x-stampa_starpvda')
- the same form uses a custom component, cst_starpvda_print in my case
- in the custom component cst_starpvda_print, the x-stampa_starpvda event is managed:
self.set_event_handler('x-stampa_starpvda', lambda **e: self.call_js('printStarpvda',self.starpvda,self.session))
- the custom component is made up simply of an empty, custom HTML component, where I define the Javascript function printStarpvda that will render the HTML page and open the browser print window:
<script>
function printStarpvda(starpvda,sessione){
var printWindow = window.open('_blank');
var is_chrome = Boolean(printWindow.chrome);
printWindow.document.open('text/html');
printWindow.document.write('<body>');
printWindow.document.write('<img textaling="center" align="center" src=""></img>');
// some cool html rendering here
printWindow.document.write('</body>');
printWindow.document.close();
if (is_chrome) {
printWindow.onload = function() { // wait until all resources loaded
printWindow.focus(); // necessary for IE >= 10
printWindow.print(); // change window to mywindow
printWindow.close(); // change window to mywindow
};
}
else {
printWindow.document.close(); // necessary for IE >= 10
printWindow.focus(); // necessary for IE >= 10
printWindow.print();
printWindow.close();
}
}
</script>
Everything worked fine up to now. But the rendered HTML was too simple.
I improved the function and now, when I click the “print” link on the calling page, I get the error:
Cannot find the JavaScript function printStarpvda
But there it is, I can see it if I inspect the running app with F12…
How is that?
Thanks