Reputation: 3592
I need to print a HTML File from my Java Application. I have tried several methods. Two of them are working, but not as expected.
Method 1:
Problem: The Print is cut of after three-quarter of the sheet.
try {
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = new PageFormat();
pageFormat.setOrientation(PageFormat.LANDSCAPE);
Paper paper = new Paper(); // Set to A4 size.
paper.setSize(594.936, 841.536); // Set the margins.
paper.setImageableArea(0, 0, 594.936, 841.536);
pageFormat.setPaper(paper);
XHTMLPanel panel = new XHTMLPanel();
panel.setDocument(new File("./documents/" + "Personalstamm"
+ ".html"));
printJob.setPrintable(new XHTMLPrintable(panel), pageFormat);
if (printJob.printDialog()) {
printJob.print();
}
} catch (Exception x) {
x.printStackTrace();
}
Method 2:
Problem: The printout is without the Stylesheet set in the HTML file.
PageFormat pageFormat = new PageFormat();
Paper a4paper = new Paper();
double paperWidth = 8.26;
double paperHeight = 11.69;
a4paper.setSize(paperWidth * 72.0, paperHeight * 72.0);
/*
* set the margins respectively the imageable area
*/
double leftMargin = 0.78; /* should be about 2cm */
double rightMargin = 0.78;
double topMargin = 0.78;
double bottomMargin = 0.78;
a4paper.setImageableArea(leftMargin * 72.0, topMargin * 72.0,
(paperWidth - leftMargin - rightMargin) * 72.0, (paperHeight
- topMargin - bottomMargin) * 72.0);
pageFormat.setPaper(a4paper);
pageFormat.setOrientation(PageFormat.LANDSCAPE);
DocumentRenderer documentRenderer = new DocumentRenderer(pageFormat,
"Report");
try {
FileInputStream stringReader = new FileInputStream(new File(
"./documents/" + "Personalstamm" + ".html"));
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit
.createDefaultDocument();
htmlKit.read(stringReader, htmlDoc, 0);
documentRenderer.print(htmlDoc);
} catch (Exception x) {
x.printStackTrace();
}
Does anybody have an idea how to solve the problem in one of these methods? Or do anybody have a better way to print a file from Java?
Upvotes: 2
Views: 3984
Reputation: 3592
Now i am using Apache PDFBox - A Java PDF Library and it's nearly what i was looking for.
My Code:
public void printFile(String fileName) {
//Convert to PDF
try {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(new File("./documents/html/"+fileName+".html"));
renderer.layout();
String fileNameWithPath = "./documents/pdf/"+fileName+".pdf";
FileOutputStream fos = new FileOutputStream(fileNameWithPath);
renderer.createPDF(fos);
fos.close();
} catch (Exception x) {
x.printStackTrace();
}
//Print with Dialog
try {
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = new PageFormat();
pageFormat.setOrientation(PageFormat.LANDSCAPE);
Paper paper = new Paper();
paper.setSize(595, 842);
paper.setImageableArea(0, 0, 595, 842);
pageFormat.setPaper(paper);
PDDocument doc = PDDocument.load(new File("./documents/pdf/"+fileName+".pdf"));
printJob.setPrintable(new PDPageable(doc), pageFormat);
if (printJob.printDialog()) {
printJob.print();
}
doc.close();
} catch (Exception x) {
x.printStackTrace();
}
}
Upvotes: 1
Reputation: 36722
You can't print your HTML
with CSS
. The best shot that you got is to use PDFs
, that's what they are for. Create a PDF from the HTML using Java and print it
Upvotes: 1