Click to See Complete Forum and Search --> : printing in java from a servlet(not from an applet or application)


kheiremans
03-27-2007, 05:34 AM
i've been trying alot to print html text from a servlet but with not much succes.

i have a database and a servlet. the servlet gets the info from the Db and creates a html apge based on that info. Now i'd like to be able to print this out. i've added a button to the bottom of the page. Now the thing is: it needs to be printed without headers/footers or any other info and also the buttons at the bottom shouldn't be included.

my latest attempt was to create a dynamic html page from a servlet. i also put all the html text in a string.

i then create a JEditorPane and fill it with the html text. Then i print the Pane out. Problem is: it gives a blank apge because i don't actually display the pane(which i dont want to do)

PrintableEditorPane htmlPane = new PrintableEditorPane();
htmlPane.setContentType("text/html");
htmlPane.setText(tekst);
htmlPane.setEditable(false);

// Get a PrinterJob
PrinterJob job = PrinterJob.getPrinterJob();
// Create a landscape page format
PageFormat landscape = job.defaultPage();
landscape.setOrientation(PageFormat.LANDSCAPE);
// Set up a book
Book bk = new Book();
bk.append(htmlPane, job.defaultPage());
// Pass the book to the PrinterJob
job.setPageable(bk);
// Put up the dialog box
if (job.printDialog()) {
// Print the job if the user didn't cancel printing
try { job.print(); }






class PrintableEditorPane extends javax.swing.JEditorPane implements java.awt.print.Printable
{
/**
*
*/
private static final long serialVersionUID = 1L;

public int print(java.awt.Graphics g, java.awt.print.PageFormat pageFormat, int pageIndex)
{
int x = (int)pageFormat.getImageableX();
int y = (int)pageFormat.getImageableY();
g.translate(x, y);
paint(g);
return PAGE_EXISTS;
}
}


so is there a different way to send a html string to a printer for printing out.
or is there an easy way to generate a pdf from a html string(which would also work)


thx in advance