1

I am stuck at this now. I have checked almost every popular question on SO site regarding Java Print API to print HTML files (with third-party libraries such as Flying Saucer, iText, CSSBox, etc). But still couldn't get it worked at my end yet.

Here are the links of my previous questions:

https://stackoverflow.com/questions/28106757/java-print-api-prints-html-with-huge-size

How to print HTML and not the code using Java Print API?

Basically I am trying to print the HTML file that contains some CSS with <style> tag. This CSS has classes applied for <table> and <p> tags for example. I cannot change CSS code inside HTML as it should be viewed exactly with this style in browser.

Below is my program

import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintServiceAttributeSet;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.PrinterName;
import javax.swing.JEditorPane;

public class Print {
    public static void main(String[] args) throws PrintException {
        String printerName = "\\\\network-path\\myPrinter";
        String fileName = "C:\\log\\myLog.html";

        URL url = null;
        try {
            url = (new File(fileName)).toURI().toURL();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        JEditorPane editorPane = new JEditorPane();
        editorPane.setEditable(false);
        if (url != null) {
            try {
                editorPane.setPage(url);
            } catch (IOException e) {
                System.err.println("Attempted to read a bad URL: " + url);
            }
        } else {
            System.err.println("Couldn't find file: " + fileName);
        }

        PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
        printServiceAttributeSet.add(new PrinterName(printerName, null));
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, printServiceAttributeSet); // list of printers
        PrintService printService = printServices[0];

    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        Copies copies = new Copies(1);
        pras.add(copies);
        pras.add(OrientationRequested.PORTRAIT);
        pras.add(MediaSizeName.ISO_A4);

        try {
            editorPane.print(null, null, false, printService, pras, false);
        } catch (PrinterException e) {
            throw new PrintException("Print error occurred:" + e.getMessage());
        }
    }
}

The problem is above code works and I get good print of the above HTML with proper CSS styling. But it just scales up. When the said HTML is opened in IE it looks different and when it is printed by the code what I get is different. I would prefer the print to be same as it is viewed in IE.

I also tried to get it done by passing SimpleDoc object to the printer. My printService supports below formats:

image/gif   [B
image/gif   java.io.InputStream
image/gif   java.net.URL
image/jpeg  [B
image/jpeg  java.io.InputStream
image/jpeg  java.net.URL
image/png   [B
image/png   java.io.InputStream
image/png   java.net.URL
application/x-java-jvm-local-objectref  java.awt.print.Pageable
application/x-java-jvm-local-objectref  java.awt.print.Printable
application/octet-stream    [B
application/octet-stream    java.net.URL
application/octet-stream    java.io.InputStream

But nothing works with SimpleDoc. I then tried converting HTML to .png using CSSBox. It works but for multipage HTML, generated image is shrunk and is not viewable for printing. With Flying Saucer and iText version 2.0.8 I get NoSuchMethodError. Also even if I get it worked (by compiling the source against the said iText version) the output is broken.

Can someone please help? I would prefer to stick to Java Print API than using any third-party. Am I missing something when using SimpleDoc object approach? What settings need to be set to print above HTML using SimpleDoc object and available printService formats.

Community
  • 1
  • 1
ParagJ
  • 1,566
  • 10
  • 38
  • 56

0 Answers0