BF 2973169 - Jasper documents always prints on system default printer.

Now documents are printed on the printer selected in Adempiere or the printer
specified on the print format (as it works with the old reports).

https://sourceforge.net/tracker/index.php?func=detail&aid=2973169&group_id=176962&atid=879332
Link to SF Tracker: http://sourceforge.net/support/tracker.php?aid=2973169
This commit is contained in:
usrdno 2010-03-22 09:24:29 +00:00
parent f479ca2beb
commit a50e471830
2 changed files with 138 additions and 19 deletions

View File

@ -13,6 +13,7 @@
*****************************************************************************/ *****************************************************************************/
package org.compiere.report; package org.compiere.report;
import java.awt.print.PrinterJob;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -45,8 +46,16 @@ import java.util.logging.Level;
import javax.naming.InitialContext; import javax.naming.InitialContext;
import javax.naming.NamingException; import javax.naming.NamingException;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaSizeName;
import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRParameter; import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperExportManager;
@ -54,6 +63,8 @@ import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager; import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPrintServiceExporter;
import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader; import net.sf.jasperreports.engine.util.JRLoader;
import org.adempiere.exceptions.DBException; import org.adempiere.exceptions.DBException;
@ -63,7 +74,13 @@ import org.compiere.model.MAttachment;
import org.compiere.model.MAttachmentEntry; import org.compiere.model.MAttachmentEntry;
import org.compiere.model.MBPartner; import org.compiere.model.MBPartner;
import org.compiere.model.MProcess; import org.compiere.model.MProcess;
import org.compiere.model.PrintInfo;
import org.compiere.model.X_AD_PInstance_Para; import org.compiere.model.X_AD_PInstance_Para;
import org.compiere.print.MPrintFormat;
import org.compiere.print.MPrintPaper;
import org.compiere.print.PrintUtil;
import org.compiere.print.ReportCtl;
import org.compiere.print.layout.LayoutEngine;
import org.compiere.process.ClientProcess; import org.compiere.process.ClientProcess;
import org.compiere.process.ProcessCall; import org.compiere.process.ProcessCall;
import org.compiere.process.ProcessInfo; import org.compiere.process.ProcessInfo;
@ -475,13 +492,34 @@ public class ReportStarter implements ProcessCall, ClientProcess
params.put("AD_PINSTANCE_ID", new Integer( AD_PInstance_ID)); params.put("AD_PINSTANCE_ID", new Integer( AD_PInstance_ID));
Language currLang = Env.getLanguage(Env.getCtx()); Language currLang = Env.getLanguage(Env.getCtx());
String printerName = null;
MPrintFormat printFormat = null;
PrintInfo printInfo = null;
ProcessInfoParameter[] pip = pi.getParameter(); ProcessInfoParameter[] pip = pi.getParameter();
// Check for language parameter // Get print format and print info parameters
if (pip!=null) { if (pip!=null) {
for (int i=0; i<pip.length; i++) { for (int i=0; i<pip.length; i++) {
if ("CURRENT_LANG".equalsIgnoreCase(pip[i].getParameterName())) { if (ReportCtl.PARAM_PRINT_FORMAT.equalsIgnoreCase(pip[i].getParameterName())) {
currLang = (Language)pip[i].getParameter(); printFormat = (MPrintFormat)pip[i].getParameter();
} }
if (ReportCtl.PARAM_PRINT_INFO.equalsIgnoreCase(pip[i].getParameterName())) {
printInfo = (PrintInfo)pip[i].getParameter();
}
if (ReportCtl.PARAM_PRINTER_NAME.equalsIgnoreCase(pip[i].getParameterName())) {
printerName = (String)pip[i].getParameter();
}
}
}
if (printFormat!=null) {
if (printInfo!=null) {
// Set the language of the print format if we're printing a document
if (printInfo.isDocument()) {
currLang = printFormat.getLanguage();
}
}
// Set printer name unless already set.
if (printerName==null) {
printerName = printFormat.getPrinterName();
} }
} }
@ -525,7 +563,35 @@ public class ReportStarter implements ProcessCall, ClientProcess
log.info( "ReportStarter.startProcess print report -" + jasperPrint.getName()); log.info( "ReportStarter.startProcess print report -" + jasperPrint.getName());
//RF 1906632 //RF 1906632
if (!processInfo.isBatch()) { if (!processInfo.isBatch()) {
JasperPrintManager.printReport( jasperPrint, false);
// Get printer job
PrinterJob printerJob = org.compiere.print.CPrinter.getPrinterJob(printerName);
// Set print request attributes
// Paper Attributes:
PrintRequestAttributeSet prats = new HashPrintRequestAttributeSet();
// add: copies, job-name, priority
if (printInfo.isDocumentCopy() || printInfo.getCopies() < 1)
prats.add (new Copies(1));
else
prats.add (new Copies(printInfo.getCopies()));
Locale locale = Language.getLoginLanguage().getLocale();
prats.add(new JobName(printFormat.getName() + "_" + pi.getRecord_ID(), locale));
prats.add(PrintUtil.getJobPriority(jasperPrint.getPages().size() , printInfo.getCopies(), true));
// Create print service exporter
JRPrintServiceExporter exporter = new JRPrintServiceExporter();;
// Set parameters
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, printerJob.getPrintService());
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printerJob.getPrintService().getAttributes());
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, prats);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
// Print report / document
exporter.exportReport();
} }
else else
{ {

View File

@ -18,6 +18,7 @@ package org.compiere.print;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Properties; import java.util.Properties;
import java.util.Vector;
import java.util.logging.Level; import java.util.logging.Level;
import org.adempiere.exceptions.AdempiereException; import org.adempiere.exceptions.AdempiereException;
@ -54,6 +55,13 @@ public class ReportCtl
{ {
} // ReportCtrl } // ReportCtrl
/**
* Constants used to pass process parameters to Jasper Process
*/
public static final String PARAM_PRINTER_NAME = "PRINTER_NAME";
public static final String PARAM_PRINT_FORMAT = "PRINT_FORMAT";
public static final String PARAM_PRINT_INFO = "PRINT_INFO";
/** Static Logger */ /** Static Logger */
private static CLogger s_log = CLogger.getCLogger (ReportCtl.class); private static CLogger s_log = CLogger.getCLogger (ReportCtl.class);
@ -183,7 +191,7 @@ public class ReportCtl
MQuery query = MQuery.get (ctx, pi.getAD_PInstance_ID(), TableName); MQuery query = MQuery.get (ctx, pi.getAD_PInstance_ID(), TableName);
PrintInfo info = new PrintInfo(pi); PrintInfo info = new PrintInfo(pi);
re = new ReportEngine(ctx, format, query, info); re = new ReportEngine(ctx, format, query, info);
createOutput(re, pi.isPrintPreview()); createOutput(re, pi.isPrintPreview(), null);
return true; return true;
} }
// //
@ -197,7 +205,7 @@ public class ReportCtl
} }
} }
createOutput(re, pi.isPrintPreview()); createOutput(re, pi.isPrintPreview(), null);
return true; return true;
} // startStandardReport } // startStandardReport
@ -226,7 +234,7 @@ public class ReportCtl
PrintInfo info = new PrintInfo(pi); PrintInfo info = new PrintInfo(pi);
ReportEngine re = new ReportEngine(Env.getCtx(), format, query, info); ReportEngine re = new ReportEngine(Env.getCtx(), format, query, info);
createOutput(re, pi.isPrintPreview()); createOutput(re, pi.isPrintPreview(), null);
return true; return true;
} // startFinReport } // startFinReport
@ -243,6 +251,21 @@ public class ReportCtl
return startDocumentPrint(type, Record_ID, null, -1, IsDirectPrint); return startDocumentPrint(type, Record_ID, null, -1, IsDirectPrint);
} }
/**
* Start Document Print for Type with specified printer. Always direct print.
* @param type document type in ReportEngine
* @param customPrintFormat Custom print format. Can be null.
* @param Record_ID id
* @param parent The window which invoked the printing
* @param WindowNo The windows number which invoked the printing
* @param printerName Specified printer name
* @return true if success
*/
public static boolean startDocumentPrint(int type, MPrintFormat customPrintFormat, int Record_ID, ASyncProcess parent, int WindowNo, String printerName)
{
return(startDocumentPrint(type, customPrintFormat, Record_ID, parent, WindowNo, true, printerName));
}
/** /**
* Start Document Print for Type. * Start Document Print for Type.
* Called also directly from ProcessDialog, VInOutGen, VInvoiceGen, VPayPrint * Called also directly from ProcessDialog, VInOutGen, VInvoiceGen, VPayPrint
@ -253,8 +276,23 @@ public class ReportCtl
* @param IsDirectPrint if true, prints directly - otherwise View * @param IsDirectPrint if true, prints directly - otherwise View
* @return true if success * @return true if success
*/ */
public static boolean startDocumentPrint (int type, int Record_ID, ASyncProcess parent, int WindowNo, public static boolean startDocumentPrint(int type, int Record_ID, ASyncProcess parent, int WindowNo,
boolean IsDirectPrint) boolean IsDirectPrint)
{
return(startDocumentPrint(type, null, Record_ID, parent, WindowNo, IsDirectPrint, null ));
}
/**
* Start Document Print for Type with specified printer.
* @param type document type in ReportEngine
* @param Record_ID id
* @param parent The window which invoked the printing
* @param WindowNo The windows number which invoked the printing
* @param printerName Specified printer name
* @return true if success
*/
public static boolean startDocumentPrint (int type, MPrintFormat customPrintFormat, int Record_ID, ASyncProcess parent, int WindowNo,
boolean IsDirectPrint, String printerName)
{ {
ReportEngine re = ReportEngine.get (Env.getCtx(), type, Record_ID); ReportEngine re = ReportEngine.get (Env.getCtx(), type, Record_ID);
if (re == null) if (re == null)
@ -280,20 +318,31 @@ public class ReportCtl
} }
return false; return false;
} }
if (customPrintFormat!=null) {
// Use custom print format if available
re.setPrintFormat(customPrintFormat);
}
if(re.getPrintFormat() != null) if(re.getPrintFormat()!=null)
{ {
MPrintFormat format = re.getPrintFormat(); MPrintFormat format = re.getPrintFormat();
if(format.getJasperProcess_ID() > 0) if(format.getJasperProcess_ID() > 0)
{ {
PrintInfo info = re.getPrintInfo();
ProcessInfo pi = new ProcessInfo ("", format.getJasperProcess_ID()); ProcessInfo pi = new ProcessInfo ("", format.getJasperProcess_ID());
pi.setPrintPreview( !IsDirectPrint ); pi.setPrintPreview( !IsDirectPrint );
pi.setRecord_ID ( Record_ID ); pi.setRecord_ID ( Record_ID );
if (info.isDocument()) { Vector<ProcessInfoParameter> jasperPrintParams = new Vector<ProcessInfoParameter>();
ProcessInfoParameter pip = new ProcessInfoParameter("CURRENT_LANG", format.getLanguage(), null, null, null); ProcessInfoParameter pip;
pi.setParameter(new ProcessInfoParameter[]{pip}); if (printerName!=null && printerName.trim().length()>0) {
// Override printer name
pip = new ProcessInfoParameter(PARAM_PRINTER_NAME, printerName, null, null, null);
} }
pip = new ProcessInfoParameter(PARAM_PRINT_FORMAT, format, null, null, null);
jasperPrintParams.add(pip);
pip = new ProcessInfoParameter(PARAM_PRINT_INFO, re.getPrintInfo(), null, null, null);
jasperPrintParams.add(pip);
pi.setParameter(jasperPrintParams.toArray(new ProcessInfoParameter[]{}));
// Execute Process // Execute Process
if (Ini.isClient()) if (Ini.isClient())
@ -319,7 +368,7 @@ public class ReportCtl
} }
else else
{ {
createOutput(re, !IsDirectPrint); createOutput(re, !IsDirectPrint, printerName);
if (IsDirectPrint) if (IsDirectPrint)
{ {
ReportEngine.printConfirm (type, Record_ID); ReportEngine.printConfirm (type, Record_ID);
@ -357,13 +406,17 @@ public class ReportCtl
return startDocumentPrint (ReportEngine.CHECK, C_PaySelectionCheck_ID, null, -1, IsDirectPrint); return startDocumentPrint (ReportEngine.CHECK, C_PaySelectionCheck_ID, null, -1, IsDirectPrint);
} // startCheckPrint } // startCheckPrint
private static void createOutput(ReportEngine re, boolean printPreview) private static void createOutput(ReportEngine re, boolean printPreview, String printerName)
{ {
if (printPreview) if (printPreview)
preview(re); preview(re);
else else {
if (printerName!=null) {
re.getPrintInfo().setPrinterName(printerName);
}
re.print(); re.print();
} }
}
/** /**
* Launch viewer for report * Launch viewer for report