用Java打印HTML文件

时间:2014-09-08 08:16:03

标签: java html printing

我需要从Java应用程序中打印HTML文件。 我尝试了几种方法。 其中两个正在运作,但并不像预期的那样。

方法1:

问题:打印在四分之三的纸张后被切割。

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打印文件?

3 个答案:

答案 0 :(得分:1)

您无法使用HTML打印CSS。你得到的最佳镜头是使用PDFs,这是他们的用途。使用Java从HTML创建PDF并打印

答案 1 :(得分:1)

现在我正在使用Apache PDFBox - A Java PDF Library,这几乎就是我想要的。

我的代码:

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();
    }

}

答案 2 :(得分:0)

使用Jasper Reports可能会解决问题。