Search code examples
cssjspprintingbilling

How to print a Bill on JSP


I am trying to print a bill for my client.

I have used window.print() at first, but the problem is I can't set the tear off of the printer, and sometimes the alignments are changed.

So as an alternative I need a solution for printing the bill. Can anybody suggest some?

Can I use applet for this with interfacing the printer. I have gone through most of them & it tells me I should use report, if that is the case which one will be better for me. I use a dot matrix printer for printing job.


Solution

  • I suppose you need to make your pages printer-friendly with print-specific CSS by using css-media-types.

    You can include this in your JSP:

    <link rel="stylesheet" type="text/css" href="only_4_print.css" media="print" />
    

    notice the media="print" attribute, the CSS won't be applied to the normal page which the User is looking at, but when he tries to print the page the output will have the CSS from this file.

    If you want to directly include CSS for print in your JSP it might look something like this:

    @media print {
         body {font: 12pt georgia,serif;}
         h1 {font-size: 18pt;}
         h2 {font-size: 15pt; color: #000;}
    }
    

    So you need to have two CSS files for the same page:

    1. For normal display on the browser
    2. For printing the page (i.e. the CSS to be applied to the page for printing)

    Hope this helps.

    Here are some more links which can help you understand this approach better:

    1. An introduction to media specific CSS
    2. How to create a simple print
    3. CSS Media types
    4. A nice article regarding Print CSS