How to print the contents of an Ext 4 panel is relatively straightforward. The basic algorithm is as follows:
- Instantiate a hidden iframe
- Dynamically copy the CSS of your app into the iFrame
- Dynamically copy the contents of the panel into the iFrame
- Call the window.print() method on the iFrame
- Destroy the iFrame
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
| Ext.define( 'MyApp.view.override.Panel' , { override: 'Ext.panel.Panel' , print: function (pnl) { if (!pnl) { pnl = this ; } // instantiate hidden iframe var iFrameId = "printerFrame" ; var printFrame = Ext.get(iFrameId); if (printFrame == null ) { printFrame = Ext.getBody().appendChild({ id: iFrameId, tag: 'iframe' , cls: 'x-hidden' , style: { display: "none" } }); } var cw = printFrame.dom.contentWindow; // instantiate application stylesheets in the hidden iframe var stylesheets = "" ; for ( var i = 0; i < document.styleSheets.length; i++) { stylesheets += Ext.String.format( '' , document.styleSheets[i].href); } // various style overrides stylesheets += '' .concat( "" ); // get the contents of the panel and remove hardcoded overflow properties var markup = pnl.getEl().dom.innerHTML; while (markup.indexOf( 'overflow: auto;' ) >= 0) { markup = markup.replace( 'overflow: auto;' , '' ); } var str = Ext.String.format( '{0}{1}' ,stylesheets,markup); // output to the iframe cw.document.open(); cw.document.write(str); cw.document.close(); // remove style attrib that has hardcoded height property cw.document.getElementsByTagName( 'DIV' )[0].removeAttribute( 'style' ); // print the iframe cw.print(); // destroy the iframe Ext.fly(iFrameId).destroy(); } }); |
Once you’ve loaded the override, you can simply call the panel.print() method.
Comments
Post a Comment