Printing

2,973 views
Skip to first unread message

Andre Freller

unread,
Jan 15, 2007, 9:35:55 AM1/15/07
to Google-We...@googlegroups.com

Following the thread about printing at

 http://groups-beta.google.com/group/Google-Web-Toolkit/browse_frm/thread/53624cb05aa85624/247e890d73a76948?lnk=gst&q=print&rnum=2#247e890d73a76948

I created a simple class that can print any HTML, DOM.Element or Widget without opening a new window.

Regards,
Freller

/**
 * <pre>
 * Generic printing class
 * can be used to print the Window it self, DOM.Elements, UIObjects (Widgets) and plain HTML
 *
 * Usage:
 *      You must insert this iframe in your host page:
 *              <iframe id="__printingFrame" style="width:0;height:0;border:0"></iframe>
 *
 *      Window:
 *              Print.it();
 *
 *      Objects/HTML:
 *              Print.it(RootPanel.get("myId"));
 *              Print.it(DOM.getElementById("myId"));                                                                                             
 *              Print.it("Just <b>Print.it()</b>!");
 *
 *      Objects/HTML using styles:
 *              Print.it("<link rel='StyleSheet' type='text/css' media='paper' href='/paperStyle.css'>", RootPanel.get('myId'));
 *              Print.it("<style type='text/css' media='paper'> .newPage { page-break-after: always; } </style>",
 *                                                                              "Hi<p class='newPage'></p>By");
 * </pre>
 */

package org.gwtcommons.user.client;

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.UIObject;

public class Print {

    public static native void it() /*-{
        $wnd.print();
    }-*/;

    public static native void it(String html) /*-{
        var frame = $doc.getElementById('__printingFrame');
        if (!frame) {
            $wnd.alert("Error: Can't find printing frame.");
            return;
        }
        frame = frame.contentWindow;
        var doc = frame.document;
        doc.open();
        doc.write(html);
        doc.close();
        frame.focus();
        frame.print();
    }-*/;

    public static void it(UIObject obj) {
        it("", obj.getElement().toString());
    }

    public static void it(Element element) {
        it("", element.toString());
    }

    public static void it(String style, String it) {
        it("<it><header>"+style+"</header><body>"+it+"</body></it>");
    }

    public static void it(String style, UIObject obj) {
        it(style, obj.getElement().toString());
    }

    public static void it(String style, Element element) {
        it(style, element.toString ());
    }

} // end of class Print



Chris Ravel

unread,
Jan 23, 2007, 9:13:07 AM1/23/07
to Google Web Toolkit
Hi Freller!

That's a very useful class, it just did what I needed, that's great!

Chris

Andre Freller

unread,
Jan 23, 2007, 10:31:32 AM1/23/07
to Google-We...@googlegroups.com

Hi Chris, I'm glad to know.

Just after posting I found a small bug when forcing a specific style sheet:

Replace:

    public static void it(String style, String it) {
        it("<it><header>"+style+"</header><body>"+it+"</body></it>");
    }

With:

    public static void it(String style, String it) {

        it("<html><header>"+style+"</header><body>"+it+"</body></ html>");
    }


Freller

shi...@gmail.com

unread,
Feb 7, 2007, 10:30:24 PM2/7/07
to Google Web Toolkit
Hi Freller

appliciate your greate job !!!

Andre Freller

unread,
Feb 9, 2007, 7:32:54 AM2/9/07
to Google-We...@googlegroups.com

Thanks!

ak.agonia

unread,
Apr 3, 2007, 4:19:17 AM4/3/07
to Google Web Toolkit
why,when i try to print,the result is a white page,with only header of
page??

this is my code:

Stampa= new Button("Stampa");
Stampa.addClickListener(new ClickListener(){

public void onClick(Widget arg0) {

Print.it("<link rel='StyleSheet'
type='text/css'

href='widgets.css'>",tableA);

}

});

where tableA is asortable-rocket,with this css:

.rocket-sortableTable {
background-color: white;

}

.rocket-zebraFlexTable-oddRow {
background-color: #CCCCFF;
color: #330000;

}

.rocket-zebraFlexTable-evenRow {
background-color: #FFCCFF;
color: #330000;

}

.rocket-sortableTable-columnHeader {
font-weight: bold;
text-transform: uppercase;
background-color: white;
color: FFFFFF;

}

.rocket-sortableTable-columnHeader-sortable {
cursor: pointer;

}

.rocket-sortableTable-columnHeader-sorted {
cursor: default;
font-weight: bold;
color: #ff0000;
background-color: #bbbbff;
text-transform: uppercase;

}

.rocket-sortableTable-sortedColumn {
cursor: default;

}

.rocket-sortableTable-sortDirectionArrows {
cursor: pointer;

}

??????????
this is a result of system.out.println of my table:
<TABLE class=" rocket-zebraFlexTable rocket-sortableTable"
__eventBits="1" onchange="null" onload="null" onerror="null"><TBODY>
<TR class=" rocket-zebraFlexTable-oddRow rocket-zebraFlexTable-
heading">
<TD>
<TABLE cellSpacing=0 cellPadding=0 __hash="15121283">
<TBODY>
<TR>
<TD style="VERTICAL-ALIGN: top" align=left>
<DIV class="gwt-Label rocket-sortableTable-columnHeader rocket-
sortableTable-columnHeader-sortable" __eventBits="125" onchange="null"
onload="null" onerror="null">Codice Cl.</DIV></TD>
<TD style="VERTICAL-ALIGN: top" align=left><IMG class="gwt-Image
rocket-sortableTable-sortDirectionArrows" height=16 src="./up.gif"
width=18 __eventBits="98429" onchange="null"></TD></TR></TBODY></
TABLE></TD>
<TD class=" rocket-sortableTable-sortedColumn">
<TABLE cellSpacing=0 cellPadding=0 __hash="2136319">
<TBODY>
<TR>
<TD style="VERTICAL-ALIGN: top" align=left>
<DIV class="gwt-Label rocket-sortableTable-columnHeader rocket-
sortableTable-columnHeader-sortable" __eventBits="125" onchange="null"
onload="null" onerror="null">Cognome</DIV></TD>
<TD style="VERTICAL-ALIGN: top" align=left><IMG class="gwt-Image
rocket-sortableTable-sortDirectionArrows" height=16 src="./up.gif"
width=18 __eventBits="98429" onchange="null"></TD></TR></TBODY></
TABLE></TD>
<TD>
<TABLE cellSpacing=0 cellPadding=0 __hash="1874098">
<TBODY>
<TR>
<TD style="VERTICAL-ALIGN: top" align=left>
<DIV class="gwt-Label rocket-sortableTable-columnHeader rocket-
sortableTable-columnHeader-sortable" __eventBits="125" onchange="null"
onload="null" onerror="null">Nome</DIV></TD>
<TD style="VERTICAL-ALIGN: top" align=left><IMG class="gwt-Image
rocket-sortableTable-sortDirectionArrows" height=16 src="./up.gif"
width=18 __eventBits="98429" onchange="null"></TD></TR></TBODY></
TABLE></TD></TR>
<TR class=" rocket-zebraFlexTable-evenRow">
<TD>
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="17722462">93963</DIV></TD>
<TD class=" rocket-sortableTable-sortedColumn">
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="32285792">Aristotele</DIV></TD>
<TD>
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="18601230">aris</DIV></TD></TR>
<TR class=" rocket-zebraFlexTable-oddRow">
<TD>
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="19417131">66071</DIV></TD>
<TD class=" rocket-sortableTable-sortedColumn">
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="19107749">Cira</DIV></TD>
<TD>
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="28039371">pierpaolo</DIV></TD></TR>
<TR class=" rocket-zebraFlexTable-evenRow">
<TD>
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="19277425">86664</DIV></TD>
<TD class=" rocket-sortableTable-sortedColumn">
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="27655609">Del Piero</DIV></TD>
<TD>
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="11834918">alex</DIV></TD></TR>
<TR class=" rocket-zebraFlexTable-oddRow">
<TD>
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="32349505">1699</DIV></TD>
<TD class=" rocket-sortableTable-sortedColumn">
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="15669880">Garibaldi </DIV></TD>
<TD>
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="28490946">Giuseppe</DIV></TD></TR>
<TR class=" rocket-zebraFlexTable-evenRow">
<TD>
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="25595509">44342</DIV></TD>
<TD class=" rocket-sortableTable-sortedColumn">
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="15907500">Gates</DIV></TD>
<TD>
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="29204960">Bill</DIV></TD></TR>
<TR class=" rocket-zebraFlexTable-oddRow">
<TD>
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="13540318">65630</DIV></TD>
<TD class=" rocket-sortableTable-sortedColumn">
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="5978200">Maldini</DIV></TD>
<TD>
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="13640927">Paolo</DIV></TD></TR>
<TR class=" rocket-zebraFlexTable-evenRow">
<TD>
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="19188912">1111</DIV></TD>
<TD class=" rocket-sortableTable-sortedColumn">
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="16408949">torino</DIV></TD>
<TD>
<DIV class=gwt-Label __eventBits="125" onchange="null" onload="null"
onerror="null" __hash="1331989">giovanni</DIV></TD></TR></TBODY></
TABLE>


On Feb 9, 2:32 pm, "Andre Freller" <andre.frel...@gmail.com> wrote:
> Thanks!

imanol00

unread,
Apr 16, 2007, 7:54:17 AM4/16/07
to Google Web Toolkit
Hello people!!

I have been working in an application requesting printing features,
and when I found this class I was so happy...

But I encountered a problem when I was trying to print a styled
Widget. I use the Print.it(String style, UIObject obj) passing the
style as the example says ( Print.it("<link rel='StyleSheet'


type='text/css' media='paper' href='/paperStyle.css'>",

RootPanel.get('myId')); ), with no result.

Then I thought it was another problem. I found out a strange <header>
tag in that function, as you will see:

public static void it(String style, String it)
{
it("<html><header>"+style+"</header><body>"+it+"</body></
html>");
}

I tried to change <header> with <head>, with no result again...
I almost give up now....does anybody know anything about this???

Thanks in advance!!!

Andre Freller

unread,
Apr 16, 2007, 9:55:16 AM4/16/07
to Google-We...@googlegroups.com

Hi there,

Some people are reporting problems printing with external style links.
I still unable to reproduce the problem in my environment.
Jose claims here http://groups.google.com/group/Google-Web-Toolkit/browse_frm/thread/5bf6997b00dca94a/3acd6b200f77f81f#3acd6b200f77f81f
that he solved the problem introducing a confirm() dialog in the native javascript code.

I think it is a timing problem related to formatting the page using complex html and/or external styles.

Can you try this code below  and report if it works in your case?

Regards,
Freller


import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;

import com.google.gwt.user.client.ui.UIObject;

public class Print {

    public static native void it() /*-{                                                                                                           
        $wnd.print();                                                                                                                             
    }-*/;

    public static native void doit(String html) /*-{                                                                                              
        var frame = $doc.getElementById('__printingFrame');                                                                                       
        if (!frame) {                                                                                                                             
            $wnd.alert("Error: Can't find printing frame.");                                                                                      
            return;                                                                                                                               
        }                                                                                                                                         
        frame = frame.contentWindow;                                                                                                              
        var doc = frame.document;                                                                                                                 
        doc.open();                                                                                                                               
        doc.write(html);                                                                                                                          
        doc.close();                                                                                                                              
                                                                                                                                                  
        for (var cii=0; cii < 100000; cii++){}                                                                                                    
        frame = $doc.getElementById('__printingFrame');                                                                                           
        frame = frame.contentWindow;                                                                                                              
                                                                                                                                                  
        frame.focus();                                                                                                                            
        frame.print();                                                                                                                            
    }-*/;


    public static void it(String html) {
        try {
            doit(html);
        } catch (Throwable exc) {
            Window.alert(exc.getMessage());

        }   
    }
    public static void it(UIObject obj) {
        it("", obj.getElement ().toString());
    }

    public static void it(Element element) {
        it("", element.toString());
    }

    public static void it(String style, String it) {
        it("<html><head>"+style+"</head>\n<body>"+it+"</body></html>");
    }
    
    public static void it(String style, UIObject obj) {
        it(style, obj.getElement().toString());
    }
    
    public static void it(String style, Element element) {
        it(style, element.toString());

imanol00

unread,
Apr 16, 2007, 10:51:51 AM4/16/07
to Google Web Toolkit
Hi Andre!

Thanks for your help, but I can't get it working yet the style thing.
Also, with your code we get the famous message "This script is part of
module...". I have to make a question with the javascript "question =
confirm("Print document?");" in order to make it works.

I don't think there is no one who had printed a Widget with CSS... I
need help! I think it would be very useful for a lot of GWT
developers.

Thank you guys!!

Will Davis

unread,
Jan 22, 2014, 10:45:12 AM1/22/14
to google-we...@googlegroups.com, Google-We...@googlegroups.com
I am having a strange problem, I have figured out how to print my widget with the style. However, when I try to print a second widget, I get a blank page. I only have this problem in firefox, I havn't tested in IE, and chrome seems to work every time. Just to be clear I try to print a widget on my site, it works fine, then I navigate to another page and try to print that one and it is blank. It won't work again until I reload the website. Any ideas? Do I need to reset something?
Reply all
Reply to author
Forward
0 new messages