Click to See Complete Forum and Search --> : sending an HTML file to the printer


pelegk1
02-07-2005, 04:06 AM
i want to send a ready made HTML file to the printer
does any 1 know how it can be done in hjava?
thnaks in advance
peleg

Nadeem
02-07-2005, 08:26 AM
hey dude


what does this hjava means iam not aware of that
anyways using advanced java that is j2ee
u need to write a servlet/applet for that depending upon ur requirement .
if that is case revert back i'll try an help

ray326
02-07-2005, 12:21 PM
Do you want to open an HTML file in a Java application and print its source?

pelegk1
02-07-2005, 05:05 PM
i want to print the HTML itself and not source code!!

hjava is java= h there by mistake :)
how cani write applet for that?
do u hav maybe code for it?thnaks in advance
pele

ray326
02-08-2005, 12:20 AM
Rendering HTML is a non-trivial task. The best way to do what you're asking is to let a web browser handle that job.

pelegk1
02-08-2005, 02:10 AM
i have found a code toprint html pages
in the line : DocumentRenderer1.print(u);
is should recive HTMLDocument (hich i dont understand how execlly to use)
and i tried to give it a u (url)
to print an html file
what to do?how can i give an HTMLDocument so it will work?
thanks in advance
peleg
this is the code=>>>
i have this class PrintHtml:


import javax.swing.text.html.HTMLDocument;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.InputStream;

public class PrintHtml {
public static void main(String[] args) {
System.out.println("printing...");
DocumentRenderer DocumentRenderer1 = new DocumentRenderer();

HTMLDocument x = new HTMLDocument();
URL u;
InputStream is = null;
DataInputStream dis;

try {
u = new URL("http://www.walla.co.il");
DocumentRenderer1.print(u);

} catch (MalformedURLException mue) {

System.out.println("Ouch - a MalformedURLException happened.");
mue.printStackTrace();
System.exit(1);

} catch (IOException ioe) {

System.out.println("Oops- an IOException happened.");
ioe.printStackTrace();
System.exit(1);

} finally {



try {
is.close();
} catch (IOException ioe) {

}

} // end of 'finally' clause

}

}




and the DocumentRenderer.java :

/* Copyright 2002
Kei G. Gauthier
Suite 301
77 Winsor Street
Ludlow, MA 01056
*/



import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.net.URL;
import javax.swing.JEditorPane;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
import javax.swing.text.View;
import javax.swing.text.html.HTMLDocument;

public class DocumentRenderer implements Printable {
protected int currentPage = -1; //Used to keep track of when
//the page to print changes.

protected JEditorPane jeditorPane; //Container to hold the
//Document. This object will
//be used to lay out the
//Document for printing.

protected double pageEndY = 0; //Location of the current page
//end.

protected double pageStartY = 0; //Location of the current page
//start.

protected boolean scaleWidthToFit = true; //boolean to allow control over


protected PageFormat pFormat;
protected PrinterJob pJob;

public DocumentRenderer() {
pFormat = new PageFormat();
pJob = PrinterJob.getPrinterJob();
}

public Document getDocument() {
if (jeditorPane != null)
return jeditorPane.getDocument();
else
return null;
}

public boolean getScaleWidthToFit() {
return scaleWidthToFit;
}

public void pageDialog() {
pFormat = pJob.pageDialog(pFormat);
}

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
double scale = 1.0;
Graphics2D graphics2D;
View rootView;
// I
graphics2D = (Graphics2D) graphics;
// II
jeditorPane.setSize((int) pageFormat.getImageableWidth(), Integer.MAX_VALUE);
jeditorPane.validate();
// III
rootView = jeditorPane.getUI().getRootView(jeditorPane);
// IV
if ((scaleWidthToFit) && (jeditorPane.getMinimumSize().getWidth() >
pageFormat.getImageableWidth())) {
scale = pageFormat.getImageableWidth() /
jeditorPane.getMinimumSize().getWidth();
graphics2D.scale(scale, scale);
}
// V
graphics2D.setClip((int) (pageFormat.getImageableX() / scale),
(int) (pageFormat.getImageableY() / scale),
(int) (pageFormat.getImageableWidth() / scale),
(int) (pageFormat.getImageableHeight() / scale));
// VI
if (pageIndex > currentPage) {
currentPage = pageIndex;
pageStartY += pageEndY;
pageEndY = graphics2D.getClipBounds().getHeight();
}
// VII
graphics2D.translate(graphics2D.getClipBounds().getX(),
graphics2D.getClipBounds().getY());
// VIII
Rectangle allocation = new Rectangle(0,
(int) -pageStartY,
(int) (jeditorPane.getMinimumSize().getWidth()),
(int) (jeditorPane.getPreferredSize().getHeight()));
// X
if (printView(graphics2D, allocation, rootView)) {
return Printable.PAGE_EXISTS;
} else {
pageStartY = 0;
pageEndY = 0;
currentPage = -1;
return Printable.NO_SUCH_PAGE;
}
}

/* print(HTMLDocument) is called to set an HTMLDocument for printing.
*/
public void print(HTMLDocument htmlDocument) {
setDocument(htmlDocument);
printDialog();
}

public void print(JEditorPane jedPane) {
setDocument(jedPane);
printDialog();
}

public void print(PlainDocument plainDocument) {
setDocument(plainDocument);
printDialog();
}

protected void printDialog() {
if (pJob.printDialog()) {
pJob.setPrintable(this, pFormat);
try {
pJob.print();
} catch (PrinterException printerException) {
pageStartY = 0;
pageEndY = 0;
currentPage = -1;
System.out.println("Error Printing Document");
}
}
}

protected boolean printView(Graphics2D graphics2D, Shape allocation,
View view) {
boolean pageExists = false;
Rectangle clipRectangle = graphics2D.getClipBounds();
Shape childAllocation;
View childView;

if (view.getViewCount() > 0) {
for (int i = 0; i < view.getViewCount(); i++) {
childAllocation = view.getChildAllocation(i, allocation);
if (childAllocation != null) {
childView = view.getView(i);
if (printView(graphics2D, childAllocation, childView)) {
pageExists = true;
}
}
}
} else {
// I
if (allocation.getBounds().getMaxY() >= clipRectangle.getY()) {
pageExists = true;
// II
if ((allocation.getBounds().getHeight() > clipRectangle.getHeight()) &&
(allocation.intersects(clipRectangle))) {
view.paint(graphics2D, allocation);
} else {
// III
if (allocation.getBounds().getY() >= clipRectangle.getY()) {
if (allocation.getBounds().getMaxY() <= clipRectangle.getMaxY()) {
view.paint(graphics2D, allocation);
} else {
// IV
if (allocation.getBounds().getY() < pageEndY) {
pageEndY = allocation.getBounds().getY();
}
}
}
}
}
}
return pageExists;
}

/* Method to set the content type the JEditorPane.
*/
protected void setContentType(String type) {
jeditorPane.setContentType(type);
}

/* Method to set an HTMLDocument as the Document to print.
*/
public void setDocument(HTMLDocument htmlDocument) {
jeditorPane = new JEditorPane();
setDocument("text/html", htmlDocument);
}

public void setDocument(JEditorPane jedPane) {
jeditorPane = new JEditorPane();
setDocument(jedPane.getContentType(), jedPane.getDocument());
}

public void setDocument(PlainDocument plainDocument) {
jeditorPane = new JEditorPane();
setDocument("text/plain", plainDocument);
}

/* Method to set the content type and document of the JEditorPane.
*/
protected void setDocument(String type, Document document) {
setContentType(type);
jeditorPane.setDocument(document);
}

/* Method to set the current choice of the width scaling option.
*/
public void setScaleWidthToFit(boolean scaleWidth) {
scaleWidthToFit = scaleWidth;
}


}
__________________


Israel - the best place to live in after heaven (but no one wan't to go there so fast)

http://www.networked-toys.com

ray326
02-08-2005, 12:34 PM
First off
DocumentRenderer DocumentRenderer1 = new DocumentRenderer();
Should be something like
DocumentRenderer dr = new DocumentRenderer();
And
HTMLDocument x = new HTMLDocument();
should be a more meaningful
HTMLDocument doc = new HTMLDocument();
Then you need to learn how to load up the doc from a URL, after which I guess you'd
dr.print(doc);
I've never seen any of this stuff before so I'm just guessing here.

pelegk1
02-08-2005, 12:48 PM
i agree about the way variablles should be named
but the actuall problem on :
dr.print(doc);
you didnt help me!
i can't understand how execlly the doc object recives a url
do it can be printed
any idea?

ray326
02-08-2005, 12:56 PM
Not without reading throught the API doc on HTMLDocument. That's what I'd recommend you do next.

pelegk1
02-08-2005, 03:13 PM
but didnt understand execlly how to do it