groovy - Printing multiple poscripts in one job -
i need write java method in order send postscript files printer in 1 job. in other words, need reproduce effect of following unix command:
lp -d printer file1.ps file2.ps file3.ps
first thought concatenate ps files (using classes concatinputstream , printjobwatcher). resulting merged ps file not valid.
if helps, here current code (i have been asked in groovy):
/** * prints {@code files} {@code copycount} times using * {@code printservice}. * <p> * exceptions may thrown. * @param printservice print service * @param files groovy array of {@code file} objects * @param copycount number of copies print */ private static void printjob( printservice printservice, def files, int copycount) { // no multiple copy support ps file, must manually copycount.times { -> inputstream inputstream = null try { log.debug("create stream copy #${i}") inputstream = new concatinputstream() (def file in files) { if (file != null) { log.debug("add '${file.absolutepath}' stream") ((concatinputstream)inputstream).addinputstream( new fileinputstream(file)) } } log.debug("create document") doc doc = new simpledoc( inputstream, docflavor.input_stream.autosense, null) log.debug("create print job") docprintjob docprintjob = printservice.createprintjob() log.debug("create watcher") printjobwatcher watcher = new printjobwatcher(docprintjob) log.debug("print copy #${i}") docprintjob.print(doc, null) log.debug("wait completion") watcher.waitfordone() } { if (inputstream) log.debug("close stream") inputstream?.close() } } }
i’m not allowed convert ps pdf.
i read here i could insert false 0 startjob pop
between ps files. there 1 job?
i may confusing concept of "jobs"...
i didn’t find post on topic (sending multiple ps files printer in 1 job). solution may obvious blinded me, why posted question.
my next attempt execute lp
class, if looks dirty know can make work way... if know simpler way, please tell me.
edit:
executing lp
(as below) works well:
/** * prints {@code files} {@code copycount} times using executable. * <p> * exceptions may thrown. * @param config configobject containing closures building * command line printing executable, , analyze * return code. example of config file: * * print { * commandclosure = { printername, files -> * [ * 'lp', * '-d', printername, * files.collect{ it.absolutepath } * ].flatten() * } * errorclosure = { returncode, stdout, stderr -> returncode != 0 } * warnclosure = { returncode, stdout, stderr -> * !stderr?.isallwhitespace() } * } * * @param printername printer name * @param files groovy array of {@code file} objects * @param copycount number of copies print */ private static void printjob( configobject config, string printername, def files, int copycount) { files.removeall([null]) integer copycount = job.copycountstring.tointeger() copycount.times { -> def command = config.print.commandclosure(printername, files) log.debug("command: `" + command.join(' ') + "`") def proc = command.execute() proc.waitfor() def returncode = proc.exitvalue() def stdout = proc.in.text def stderr = proc.err.text def debugstring = "`" + command.join(' ') + "`\nreturn code: " + returncode + "\nstdout:\n" + stdout + "\nstderr:\n" + stderr if (config.print.errorclosure(returncode, stdout, stderr)) { log.error("error while calling ${debugstring}") throw new printexception("error while calling ${debugstring}") } else if (config.print.warnclosure(returncode, stdout, stderr)) { log.warn("warnings while calling ${debugstring}") } else { log.debug("command successful ${debugstring}") } } }
even if prefer not use external executable... issue not anymore critical me. accept answer if not require call external executable.
actually, can't loop through files inside loop number of copies?
ie:
private static void printjob( printservice printservice, def files, int copycount) { // no multiple copy support ps file, must manually copycount.times { -> log.debug( "printing copy $i" ) files.each { file -> log.debug( "printing $file" ) file.withinputstream { fis -> doc doc = new simpledoc( fis, docflavor.input_stream.autosense, null ) docprintjob docprintjob = printservice.createprintjob() printjobwatcher watcher = new printjobwatcher( docprintjob ) docprintjob.print( doc, null ) watcher.waitfordone() } } } }
(untested)
edit
as update workaround above, rather than:
def proc = command.execute() proc.waitfor() def returncode = proc.exitvalue() def stdout = proc.in.text def stderr = proc.err.text
you're better with:
def proc = command.execute() def out = new stringwriter() def err = new stringwriter() ps.consumeprocessoutput( out, err ) ps.waitfor() def returncode = proc.exitvalue() def stdout = out.tostring() def stderr = err.tostring()
as won't block if process writes lot of information :-)
Comments
Post a Comment