2

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?

Dmytro Plekhotkin
  • 1,965
  • 2
  • 23
  • 47
Stone
  • 2,912
  • 1
  • 21
  • 18
  • I know it's old but nothing's accepted yet: I had much more success with [PJL commands using a java socket](https://stackoverflow.com/a/55780603/1062992). – egerardus May 11 '19 at 20:08

3 Answers3

1

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

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • In the first method printing `HTML` with `CSS` is possible. But the printout is cut of. I have already tried converting to `PDF` but it is looking terrible. – Stone Sep 08 '14 at 08:32
  • For the first approach, try to put the `panel` inside a `JScrollPane` and then try to print it. Try `JScrollPane scroll = new JScrollPane(panel);` and then print the scrollPane. You will have to code for printing the full content, but you have more control with scrollpane – ItachiUchiha Sep 08 '14 at 08:43
1

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

}
Stone
  • 2,912
  • 1
  • 21
  • 18
0

Using Jasper Reports might solve the problem.

Kzhunter
  • 597
  • 1
  • 6
  • 19