1

I have now managed to create an upload in jsf that allows the user to upload a txt file, I can also display what has been uploaded, what I need to know now is how can I print this text to a printer when a user presses a command button

Thanks

I have added create a new css file called print.css,

@media print {
    #header, #footer, #menu, #title, #h1, #2, #main, #logo, #logo_text, #logo_colour, #site_content, #content{ 
        display: none;
    }
}

As these are the things I wish removed

My css for the whole document is:

    <link rel="stylesheet" type="text/css" href="style.css" title="style" />

I have added:

<h:outputStylesheet type="text/css" name="print.css" media="print" />

above my button :

                <button onclick="window.print()">Print</button>

but when I press this button I can still see all the css

Lain
  • 2,166
  • 4
  • 23
  • 47
user1924104
  • 891
  • 2
  • 16
  • 38

1 Answers1

2

You can at most show up the browser's print dialog by JS window.print().

<button onclick="window.print()">Print</button>

This will still require confirmation of the enduser. It's for websites (fortunately) simply not possible to unaskingly print something. That would otherwise produce a lot of paper waste and annoyed clients.

You might perhaps want to supply a print media CSS along so that only the desired content is been printed and that the remnant is been hidden by display:none. You can find some hints in this related question: Conditionally render JSF components for printing.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • so if i was wanting to print only what is in the inputTextarea, would the css you linked still be the best way to do this – user1924104 Dec 24 '12 at 01:13
  • Im sorry, i don't understand how upon the press of the print button the css way works, could it explain it for me , sorry i am very new to all this still – user1924104 Dec 24 '12 at 01:20
  • Just supply the CSS file with desired `@media` rules by `` or `` with `media="print"` in the very same webpage as the button is placed. Exactly as explained (and linked) in the related question. – BalusC Dec 24 '12 at 01:22
  • I have update the original question with some issues i am currently having – user1924104 Dec 24 '12 at 01:31
  • Where exactly do you "still see all that CSS"? On the print preview or on the printed page? By the way, the `@media print {}` block is not necessary when the CSS file itself is already served via `media="print"`. It's only necessary when including in the default CSS. – BalusC Dec 24 '12 at 01:34
  • Did the browser successfully retrieve the `print.css` file? – BalusC Dec 24 '12 at 01:39
  • Silly question - how can i find that out ? – user1924104 Dec 24 '12 at 01:40
  • WARNING: JSF1064: Unable to find or serve resource, print.css. – user1924104 Dec 24 '12 at 01:41
  • Just look at the HTTP traffic monitor (press F12 in Chrome/IE9/Firebug), or the server log in case you're using `` instead. Well, it turns out that its URL is wrong, or that you placed it at the wrong location, or that it's not properly deployed. – BalusC Dec 24 '12 at 01:43
  • it is in the same location as my main .css file and have added the these into the original question, will double check – user1924104 Dec 24 '12 at 01:46
  • Ok i have successfully managed to remove all the css, but i still have text and buttons around the inputTextarea, can these be removed too? – user1924104 Dec 24 '12 at 01:49
  • Yes, by using CSS the right way. Add a selector on those elements with `display:none`. Please ask a new question if you still stucks. Please note that the concrete CSS problem is **unrelated** to JSF. – BalusC Dec 24 '12 at 01:55