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?

1

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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

}

1 Comment

printJob.setPrintable(new PDFPrintable(doc), pageFormat);
0

Using Jasper Reports might solve the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.