Question

I'm trying to get a program to print labels to a print attached (USB) to the server. I've been able to print to it from a batch file but for some reason the print command fails inside of coldfusion execute command. My current solution is to access the printer through Java. My problem with this solution is that I can't call any of them java methods.

The solution is based off of this answer How to print strings with line breaks in java

The following section of code appears to work fine.

var printService = createObject("java","javax.print.PrintService");
var printServiceLookup = createObject("java","javax.print.PrintServiceLookup");
var docFlavor = createObject("java","javax.print.DocFlavor");   
writeDump( printService.init() );
writeOutput('<hr>');
writeDump( printServiceLookup );
writeOutput('<hr>');
writeDump( docFlavor );

The problem comes when I try to do a execute any of the methods that exist in the objects that were just created.

When I try to execute

writeDump( DocFlavor.STRING.TEXT_PLAIN );

I get:

Application Execution Exception
Error Type: java.lang.NoSuchMethodException : 0
Error Messages: No matching Constructor for javax.print.DocFlavor() found

Remember I'm trying to send a set of ZPL print commands to a printer attached to the computer. If been able to successfully do this through sockets but also need to be able to do it through a locally connected printer. So alternate solutions are acceptable provided they work with Railo & Windows.

Updated: 4/3/2013

var printer = javacast("null","");
var printers = printServiceLookup.lookupPrintServices( javacast("null",""), javacast("null","") );
for( var i=1; i le ArrayLen( printers ); i++ ) {
    if( printers[i].getName() eq printername ) {
        printer = printers[i];
        break;
    }
}

if( i gt ArrayLen( printer ) ) {
    throw('Printer #printername# was not found');
}

var Job = printer.createPrintJob();
var LabelCmdbytes = javaCast( "byte[]", javaCast( "String", LabelCmd ).getBytes() );
var Labeldoc = Doc.init( LabelCmdbytes, printer.getSupportedDocFlavors()[1].AUTOSENSE, javacast("null","" ) );
Job.print( Labeldoc, javacast("null","") 

This solution works but I don't think that printer.getSupportedDocFlavors()[1].AUTOSENSE is the best solution to the DocFlavor value. The environment is Windows 7 with Railo 4.1.x

Was it helpful?

Solution

Actually DocFlavor.STRING is a nested class. So you need to use $ to grab a reference to it:

StringFlavor = createObject("java","javax.print.DocFlavor$STRING");   

Then you can access the public field:

writeDump( StringFlavor.TEXT_PLAIN );

Side note, printService.init() should not work. PrintService is an interface and cannot be instantiated. So calling init() should throw an error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top