Printing

When I try basic printing, or actually trying to get the print dialog to pop up, the loading icon comes to the top right corner and nothing happens. Any idea why?

How are you trying to open the print dialog? Do you have some custom JavaScript?

At least I was able to get the dialog open by pressing CTRL+P (Mac Firefox 3 and Safari 3).

That works for me too, but when I try to do it in the code it goes into some loop. Something like this i have tried:

PrinterJob pjob = PrinterJob.getPrinterJob();
PageFormat pf = pjob.defaultPage();
pjob.setPrintable(new PrintableClass(), pf);
try {
    if (pjob.printDialog()) {
        pjob.print();
    }
} catch (PrinterException e) {
}

…and many other ways, but none of them works. Can you tell me how it should be done? :roll:

PrinterJob is a Java AWT class and printDialog() would open a print dialog in the server… AWT is a regular desktop UI library and you can’t use that through a web browser.

Are you trying to do server-side or client-side printing? I suppose you might be able to do server-side printing with AWT, possibly, but the printing dialog would be an obvious problem.

Client-side. Never done printing to web, so that’s why I’m pretty lost here :slight_smile:

There isn’t yet a built-in way to print the current page in the web browser, but you could just add a Label and put a regular HTML button (or link or whatever) inside it and call the
print()
function in the
onClick
event.

mywindow.addComponent(new Label("<input type='button' onClick='print()' value='Click to Print'/>", Label.CONTENT_XHTML));

This would print the current page. If you want to print, for example, some report, you could open another page and print that. Or have the user download a PDF and print that.

Thanks. That solved the problem :smiley:

If you want to have a print button that opens a separate window that has some special content you want to print, you could do as follows:


// Create a window that contains stuff you want to print.
final Window printWindow = new Window("Window to Print");

// Have some content to print.
printWindow.addComponent(new Label("Here's some content to print."));

// To execute the print() JavaScript, we need to run it
// from a custom layout.
CustomLayout scriptLayout = new CustomLayout("printpage");
printWindow.addComponent (scriptLayout);

// Add the printing window as an application-level window.
main.getApplication().addWindow(printWindow);

// Open the printing window as a new browser window
Button printButton = new Button("Click to Print");
main.addComponent(printButton);
printButton.addListener(new Button.ClickListener() {
    public void buttonClick(ClickEvent event) {
        main.open(new ExternalResource(printWindow.getURL()), "_new");
    } 
});

And then have a custom layout in your theme (must set theme with setTheme()) with name
WebContent/ITMILL/themes/mytheme/layouts/printpage.html
:


<div>Some static content to print.</div>

<script type='text/javascript'>
print();
</script>

You need the CustomLayout because it’s a way you can execute custom JavaScript in the browser. Interestingly, it
must
have some other content besides the block, such as the

element above.

Is there some specific reason why this doesn’t work on Safari (OSX)? For example in featurebrowser, if you type “javascript:print()” it just prints a blank page. I’ve also tried window.print(), this.print() and self.print().

As to the post about the CustomLayout with the javascript, in some cases the page might be still loading when the print() function is executed, leading to the page being printed too early.

You guys might be interested to see this:
http://dev.itmill.com/ticket/2123 Printing in Toolkit applications: Add AbstractComponent.print() method

Not yet done, but your voice counts :slight_smile:

Where did you type that? If you are using it from GWT, maybe you should try $wnd.print()?

Printing from Safari with current Toolkit version really is broken. Even if you do it from File->Print.

Something works, something not. Main layout is not printed. Popupwindows prints ok.

I tracked it for a minute and it seems that Safari can’d deal with the following css:


.i-app {
    height:100%;
}

Serving printer css that unsets that height is a possible workaround.

Printing in all should be handled somehow bit better in our css. For example in FF following code prints only visible rows. Natural way would of course be to print them all.



        final Window main = new Window("Hello window");
        setMainWindow(main);
        main.setLayout(new GridLayout());
        for (int i = 0; i < 200; i++) {
            main.addComponent(new Label("fdsjkfds" + i));
        }

It’s a Safari feature, you can type javascript in the address bar with “javascript:”. In the application, I use CustomLayout and a Label as as the previous posts showed.

Thanks Matti for the answers, I’ll try getting rid of the 100% height.

Hi!

As I started to fight with the issue, I couldn’t resist to stop without some level of solution. In [5561]

  • [5562]
    I added some print media specific css to cope with Safari issue and the above example by me.

Try copying those to your themes styles.css file.

cheers,
matti