繁体   English   中英

用Java打印HTML文件

[英]Print HTML File in Java

我需要从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打印文件?

您不能使用CSS打印HTML 最好的选择就是使用PDFs ,这就是它们的用途。 使用Java从HTML创建PDF并打印

现在我正在使用Apache PDFBox-一个Java 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();
    }

}

使用Jasper Reports可以解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM