用Java打印HTML文件
我需要从我的Java应用程序打印一个HTML文件。 我试过几种方法。 其中两个正在工作,但并不像预期的那样 方法1: 问题:印刷品在纸张四分之三后被切断用Java打印HTML文件,java,html,printing,Java,Html,Printing,我需要从我的Java应用程序打印一个HTML文件。 我试过几种方法。 其中两个正在工作,但并不像预期的那样 方法1: 问题:印刷品在纸张四分之三后被切断 try { PrinterJob printJob = PrinterJob.getPrinterJob(); PageFormat pageFormat = new PageFormat(); pageFormat.setOrientation(PageFormat.LANDSCAPE);
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();
}
方法2:
问题:打印输出没有在HTML文件中设置样式表
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();
}
有人知道如何用这些方法中的一种来解决这个问题吗?
或者有人有更好的方法从Java打印文件吗?您不能用
CSS
打印HTML
。你最好的办法就是使用PDF,这就是它们的用途。使用Java从HTML创建一个PDF,并打印它。使用可能会解决问题。现在我正在使用,它几乎就是我想要的
我的代码:
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();
}
}
在第一种方法中,可以使用
CSS
打印HTML
。但是打印出来的是剪掉的。我已经尝试过转换成PDF
,但它看起来很糟糕。第一种方法是,尝试将面板
放在JScrollPane
中,然后尝试打印它。TryJScrollPane scroll=newjscrollpane(面板)代码>然后打印滚动窗格。您必须编写代码来打印全部内容,但您对scrollpaneThanks有更多的控制,我将尝试使用它。printJob.setPrintable(新的PDFPPrintable(doc),pageFormat);我知道它很老了,但还没有被接受:我在这方面取得了更大的成功。