[jcifs] Re: Some printer questions

Eric Glass eric.glass at gmail.com
Mon Jan 15 11:34:47 GMT 2007

As followup, attached is an example test class I'm using to print a
simple HTML file through JEditorPane.

On 1/14/07, Eric Glass <eric.glass at gmail.com> wrote:
> Mike,
> I had a couple of questions around printing; I've hacked together what
> I need to work, but looking for the "best" approach.
> Basically, I'm generating PostScript via Java2D and dumping that to a
> printer; I've gotten this to work by setting
> jcifs.smb.client.useNTSmbs = false and then adding the following in
> SmbTree.java (around line 75, where it is checking the "allowable"
> commands):
>     case SMB_COM_WRITE:
> (apparently only SMB_COM_WRITE_ANDX is currently supported, causing
> jCIFS to spit out "Invalid operation for LPT1: service" with NT SMBs
> switched off).
> I guess my question is, is there any "cleaner" way to do this?  Seems
> funky to disable NT SMBs globally, and recompiling for a one-line
> change is also a pain.  Any thoughts?
> Eric
-------------- next part --------------
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import java.awt.print.PageFormat;
import java.awt.print.Printable;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.SimpleDoc;
import javax.print.StreamPrintServiceFactory;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.RepaintManager;

import javax.swing.text.html.HTMLEditorKit;

import jcifs.Config;

import jcifs.smb.SmbFileOutputStream;

public class PrintHtml {

    public static void main(String[] args) throws Exception {
        Config.setProperty("jcifs.smb.client.useNTSmbs", "false");
        String htmlFile = args[0];
        String printerUrl = args[1];
        StringWriter collector = new StringWriter();
        PrintWriter writer = new PrintWriter(collector);
        BufferedReader reader = new BufferedReader(new FileReader(htmlFile));
        String line;
        while ((line = reader.readLine()) != null) writer.println(line);
        String content = collector.toString();
        final JEditorPane pane = new JEditorPane();
        pane.setEditorKit(new HTMLEditorKit());
        JFrame frame = new JFrame();
        Printable printable = new Printable() {
            public int print(Graphics g, PageFormat format, int pageIndex) {
                RepaintManager currentManager =
                boolean doubleBuffering =
                if (doubleBuffering) {
                try {
                    Dimension size = pane.getSize();
                    double panelWidth = size.width;
                    double panelHeight = size.height;
                    double pageHeight = format.getImageableHeight();
                    double pageWidth = format.getImageableWidth();
                    double scale = pageWidth / panelWidth;
                    int totalNumPages = (int)
                            Math.ceil(scale * panelHeight / pageHeight);
                    if (pageIndex >= totalNumPages) {
                        return Printable.NO_SUCH_PAGE;
                    Graphics2D g2 = (Graphics2D) g;
                    g2.translate(0f, -pageIndex * pageHeight);
                    g2.scale(scale, scale);
                    return Printable.PAGE_EXISTS;
                } finally {
                    if (doubleBuffering) {
        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
        Doc doc = new SimpleDoc(printable, flavor, null);
        StreamPrintServiceFactory[] factories =
                        null, "application/postscript");
        OutputStream output = new SmbFileOutputStream(printerUrl);
        PrintService service = factories[0].getPrintService(output);
        DocPrintJob job = service.createPrintJob();
        job.print(doc, null);


More information about the jcifs mailing list