Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » BIRT » How to print a report with PrintDialog
How to print a report with PrintDialog [message #652588] Fri, 04 February 2011 11:03 Go to next message
Vincenzo Caselli is currently offline Vincenzo Caselli
Messages: 51
Registered: July 2009
Member
Hi,
in our RCP application we would like to print the report to a user-selected printer with a PrintDialog (source below).
The problem is that the printer is receiving data (spooler shows it, with the right size in bytes), but does not print in the end. Just with a PDF virtual printer we can obtain the task right.
What are we doing wrong?
Is it right to use a PDF OutputFormat if our goal is to send the report to a printer?
Thank you very much.

Vincenzo Caselli
RCP Vision

Our source:

		EngineConfig config = new EngineConfig();
		IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
		IReportEngine engine = factory.createReportEngine(config);
		IReportRunnable design = null;
		//Output Report
		FileInputStream fs = new FileInputStream(reportDesignPath);
		design = engine.openReportDesign(fs);
		IRunAndRenderTask task = engine.createRunAndRenderTask(design);
		PDFRenderContext renderContext = new PDFRenderContext();
		HashMap<String, PDFRenderContext> contextMap = new HashMap<String, PDFRenderContext>();
		contextMap.put(EngineConstants.APPCONTEXT_PDF_RENDER_CONTEXT, renderContext);
		task.setAppContext(contextMap);
		IRenderOption options = new PDFRenderOption();
		final ByteArrayOutputStream bos = new ByteArrayOutputStream();
		options.setOutputStream(bos);
		options.setOutputFormat("pdf");
		task.setRenderOption(options);
		task.run();
		byte[] bytes = bos.toByteArray();
		task.close();
		engine.destroy();
		
		//Print Report
		PrintDialog pd = new PrintDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
		PrinterData pdata = pd.open();
		PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
		PrintService service = null;
		for (int i = 0; i < services.length; i++) {
			if (services[i].getName().equals(pdata.name)) {
				service  = services[i];
				break;
			}
		}
		DocPrintJob job = service.createPrintJob();

		InputStream fin = new ByteArrayInputStream(bytes);
		Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
		pras.add(new Copies(1));
		job.print(doc, pras);
		doc.getStreamForBytes().close();
		fin.close();

Re: How to print a report with PrintDialog [message #652643 is a reply to message #652588] Fri, 04 February 2011 14:53 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason Weathersby
Messages: 9167
Registered: July 2009
Senior Member

Vincenzo,

Checkout the birt viewer source code and look at the

org.eclipse.birt.report.utility.PrintUtility
org.eclipse.birt.report.utility.Printer
org.eclipse.birt.report.utility.BirtUtility (doPrintAction method)
classes. I believe the PrintUtility class shows printing in Postscript.

Jason



On 2/4/2011 11:03 AM, Vincenzo Caselli wrote:
> Hi,
> in our RCP application we would like to print the report to a
> user-selected printer with a PrintDialog (source below).
> The problem is that the printer is receiving data (spooler shows it,
> with the right size in bytes), but does not print in the end. Just with
> a PDF virtual printer we can obtain the task right.
> What are we doing wrong?
> Is it right to use a PDF OutputFormat if our goal is to send the report
> to a printer? Thank you very much.
>
> Vincenzo Caselli
> RCP Vision
>
> Our source:
>
> EngineConfig config = new EngineConfig();
> IReportEngineFactory factory = (IReportEngineFactory)
> Platform.createFactoryObject(IReportEngineFactory.EXTENSION_ REPORT_ENGINE_FACTORY);
>
> IReportEngine engine = factory.createReportEngine(config);
> IReportRunnable design = null;
> //Output Report
> FileInputStream fs = new FileInputStream(reportDesignPath);
> design = engine.openReportDesign(fs);
> IRunAndRenderTask task = engine.createRunAndRenderTask(design);
> PDFRenderContext renderContext = new PDFRenderContext();
> HashMap<String, PDFRenderContext> contextMap = new HashMap<String,
> PDFRenderContext>();
> contextMap.put(EngineConstants.APPCONTEXT_PDF_RENDER_CONTEXT ,
> renderContext);
> task.setAppContext(contextMap);
> IRenderOption options = new PDFRenderOption();
> final ByteArrayOutputStream bos = new ByteArrayOutputStream();
> options.setOutputStream(bos);
> options.setOutputFormat("pdf");
> task.setRenderOption(options);
> task.run();
> byte[] bytes = bos.toByteArray();
> task.close();
> engine.destroy();
>
> //Print Report
> PrintDialog pd = new
> PrintDialog(PlatformUI.getWorkbench().getActiveWorkbenchWind ow().getShell());
>
> PrinterData pdata = pd.open();
> PrintService[] services = PrintServiceLookup.lookupPrintServices(null,
> null);
> PrintService service = null;
> for (int i = 0; i < services.length; i++) {
> if (services[i].getName().equals(pdata.name)) {
> service = services[i];
> break;
> }
> }
> DocPrintJob job = service.createPrintJob();
>
> InputStream fin = new ByteArrayInputStream(bytes);
> Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
> PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
> pras.add(new Copies(1));
> job.print(doc, pras);
> doc.getStreamForBytes().close();
> fin.close();
>
>
Re: How to print a report with PrintDialog [message #652656 is a reply to message #652643] Fri, 04 February 2011 16:25 Go to previous messageGo to next message
Vincenzo Caselli is currently offline Vincenzo Caselli
Messages: 51
Registered: July 2009
Member
Jason,
thank you for the response.
I searched for the project "org.eclipse.birt.report.viewer" in the "dev.eclipse.org:/cvsroot/birt" CVS, but did not found the classes you are referring to.
Where is exactly the cvs for those classes?
Thank you

Vincenzo
Re: How to print a report with PrintDialog [message #652982 is a reply to message #652656] Mon, 07 February 2011 14:02 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason Weathersby
Messages: 9167
Registered: July 2009
Senior Member

That should have found it. You can also get it in the sdk download from
here:
http://download.eclipse.org/birt/downloads/build.php?build=R -R1-2_6_1-201009171723

Extract this jar

org.eclipse.birt.report.viewer.source_2.6.1.v20100913.jar

Jason

On 2/4/2011 4:25 PM, Vincenzo Caselli wrote:
> Jason,
> thank you for the response.
> I searched for the project "org.eclipse.birt.report.viewer" in the
> "dev.eclipse.org:/cvsroot/birt" CVS, but did not found the classes you
> are referring to.
> Where is exactly the cvs for those classes?
> Thank you
>
> Vincenzo
Re: How to print a report with PrintDialog [message #653057 is a reply to message #652982] Tue, 08 February 2011 05:06 Go to previous messageGo to next message
Vincenzo Caselli is currently offline Vincenzo Caselli
Messages: 51
Registered: July 2009
Member
Thanks Jason,
I saw the BirtUtility and PrintUtility classes.
They works with an inputStream and with HttpServletRequest/HttpServletResponse concepts.
Unfortunately we have to handle a large variety of printers, not necessarily supporting Postscript) from an RCP client application.
Is there a way to export a report to a generic printer (something like the Exporter of JasperReports, where you just set the printerService or let the user choose the printer) ?

Thank you
Regards
Vincenzo
Re: How to print a report with PrintDialog [message #653201 is a reply to message #653057] Tue, 08 February 2011 15:33 Go to previous message
Jason Weathersby is currently offline Jason Weathersby
Messages: 9167
Registered: July 2009
Senior Member

Vincenzo,

The print code I referenced earlier was for printing on the server. If
you want the print dialog to open automatically when the user opens the
pdf you could use iText which BIRT already uses. For example add the
com.lowagie.text plugin to your dependencies and put in code like this:

IRunAndRenderTask task=null;
IReportEngine engine=null;
EngineConfig config = null;

try{
//System.setProperty("java.io.tmpdir", "c:/temp/test/testsampledb");
config = new EngineConfig( );
//config.getAppContext().put(EngineConstants.WEBAPP_CLASSPAT H_KEY,
"C:/work/workspaces/2.5.1workspaces/BIRT -DE RE API
Samples/APIs/Reports/myimage.jar");

config.setBIRTHome(" C:\\birt\\birt-runtime-2_6_1\\birt-runtime-2_6_1\\ReportEngi ne ");
Platform.startup( config );
IReportEngineFactory factory = (IReportEngineFactory) Platform
.createFactoryObject(
IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
engine = factory.createReportEngine( config );

IReportRunnable design = null;
//Open the report design
design = engine.openReportDesign("Reports/Master.rptdesign");
//Create task to run and render the report,
task = engine.createRunAndRenderTask(design);
//task.setProgressMonitor(new MyProgressMonitor());
task.setLocale(new Locale("en", "US"));
ByteArrayOutputStream baos= new ByteArrayOutputStream();
PDFRenderOption options = new PDFRenderOption();
options.setOutputStream(baos);
options.setOutputFormat("pdf");
task.setRenderOption(options);
task.run();
task.close();
PdfReader js1 = new PdfReader(baos.toByteArray());
PdfCopyFields cpy = new PdfCopyFields( new FileOutputStream(
"output/resample/printdialog.pdf" ));
cpy.addDocument(js1);
cpy.addJavaScript( "this.print({bUI: true,bSilent: false,bShrinkToFit:
true});" );
cpy.close();
engine.destroy();
}catch( Exception ex){
ex.printStackTrace();
}
finally
{

Platform.shutdown( );
System.out.println("Finished");
}

This will create a pdf that when opened should launch the print dialog
box. BTW I destroy the engine and shutdown the platform in this
example, but generally if the app is going to continue to run this is a
bad idea. If your app is RCP platform is already started so you do not
have to startup or shutdown. Get the engine once with synchronized
method and do not destroy it until your app shuts down. The engine can
be used to create many tasks.

Jason



On 2/8/2011 5:06 AM, Vincenzo Caselli wrote:
> Thanks Jason,
> I saw the BirtUtility and PrintUtility classes.
> They works with an inputStream and with
> HttpServletRequest/HttpServletResponse concepts.
> Unfortunately we have to handle a large variety of printers, not
> necessarily supporting Postscript) from an RCP client application.
> Is there a way to export a report to a generic printer (something like
> the Exporter of JasperReports, where you just set the printerService or
> let the user choose the printer) ?
>
> Thank you
> Regards
> Vincenzo
Previous Topic:BIRT Report Viewer Problem
Next Topic:Can't seem to reference a form item
Goto Forum:
  


Current Time: Thu Jul 31 05:45:28 EDT 2014

Powered by FUDForum. Page generated in 0.01604 seconds