10 Replies Latest reply on Jul 27, 2007 7:54 PM by davidfed

    How to let user print off a list?

    ellenzhao

      I'd like to let user print off a list in plain text. In the backing bean I wrote a method

      public void printShoppingList(){}
      

      But I don't know how to implement it. (The list text should be sent to a physical printer immediately) Any help will be highly appreciated!


      Regards,
      Ellen

        • 1. Re: How to let user print off a list?
          wise_guybg

          You can display it in a separate window as a printable page. The user can then print it using the browser. I think it's done this way normally

          • 2. Re: How to let user print off a list?
            ellenzhao

            Thanks! I'll do it right away!

            Just started to read this:

            http://java.sun.com/javase/6/docs/technotes/guides/jps/index.html

            But now there is no need to spend time there....Thanks!!


            Regards,
            Ellen

            • 3. Re: How to let user print off a list?
              wise_guybg

              Yes :-) I saw that on the Internet too and right before posting a link:
              http://www.google.bg/search?q=java+print

              I stopped and realized that you need printing on the client machine. The interface is the browser so you could simply use that.

              • 4. Re: How to let user print off a list?
                ellenzhao

                I tried out the "print this page" button here:

                http://www.tizag.com/javascriptT/javascriptprint.php

                The result is exactly what I want. But how to print off just a list provided by the backing bean but not the entire window? I'm very clueless on javascript, any help will be highly appreciated!


                Regards,
                Ellen

                • 5. Re: How to let user print off a list?
                  ellenzhao

                   

                  <script type="text/javascript" language="JavaScript"><!--
                  if(window.print) {
                   document.write('<a href="javascript:window.print()">[click to print]</a>');
                   }
                  //--></script>
                  


                  I found this code from here:
                  http://willmaster.com/blog/javascript/Four_Ways_To_Print_a_Web_Page.html

                  How to pass the formatted list to the "document" in the code above? And how do I use this javascript fragment in my .xhtml file? (I mean, how to bind this script to a button? ) Many thanks!


                  Regards,
                  Ellen

                  • 6. Re: How to let user print off a list?
                    jcg3

                    You can also use css to determine how the printing appears on paper... google "printing css" for examples.

                    You can go as far as to have only one div show up for printing, or show divs in print that aren't visible on screen.

                    • 7. Re: How to let user print off a list?

                      Why not just create a separate page optimized for printing (ie "printer-friendly" version). Open it in a new window.

                      • 8. Re: How to let user print off a list?
                        ellenzhao

                        @smithbstl

                        because on the page there are several fragments should be printed off. One is a shopping list, which should be like a shopping list (slim, small, layout simple....), others might be suitable for A5 or A4 format. Mixing them on one page is easy for me but not good for my users.

                        • 9. Re: How to let user print off a list?
                          wise_guybg

                          Well you can have a link for every fragment if you think they should display in a different way.

                          Anyway, I guess you have solved your issues with printing by now.

                          • 10. Re: How to let user print off a list?
                            davidfed

                            I needed a way to print content without showing it first. And it is a completely different layout than the visible page and required data that was just entered on the page.

                            I have this at the bottom of the visible page (Event.observe() is from Prototype):

                            <iframe id="printbox" name="printbox" src="#{referralController.faxUrl}"
                             width="0" height="0" style="border:none">
                            </iframe>
                            
                            <script type="text/javascript">
                            Event.observe(window, 'load', printFax);
                            </script>
                            


                            Here's the javascript function:
                            function printFax()
                             {
                             if (window.frames['printbox'].document.URL.indexOf("faxcover") != -1)
                             {
                             window.frames['printbox'].focus();
                             window.frames['printbox'].print();
                             }
                             }
                            


                            The function checks to see if the iframe contains any printable content by checking the URL.

                            I leave the URL blank when first loading the page. When the print button is clicked the backing bean method stores the data, sets the printing URL and then reloads the same page. I include the conversation id so the printing page can find the data.

                            faxUrl = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() +
                             "/faxcover.seam?clr=true&cid=" + conversation.getId();
                            


                            This works in both IE and FF. The page doesn't change visually but the print dialog appears for the hidden layout.