Double sided printing of multiple reports? [message #661364] |
Thu, 24 March 2011 07:47  |
Eclipse User |
|
|
|
Hi,
I wonder if there's is a way to guarantee a correct double sided printing of multiple reports?
I use BIRT 2.6.2 to generate several reports, which I concatinate into one big pdf printout. When printing the pdf I would like to have double sided printouts, BUT keep the different reports separate, so that each report starts on an new paper instead of the backside of the previous.
I was thinking about checking the pagenumber on each report and insert a blank page if a report ends on an uneven number.
Anyone know if this works? Or is there another way to do this?
Tnx in advance!
|
|
|
|
|
|
|
|
Re: Double sided printing of multiple reports? [message #668097 is a reply to message #668069] |
Tue, 03 May 2011 13:47   |
Eclipse User |
|
|
|
Are you calling the iText libraries directly? Maybe the
PdfCopyFields.addDocument will work. I have an RE API example that
combines to report outputs. I am attaching it.
Jason
package REAPI;
import java.util.logging.Level;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.PDFRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfCopyFields;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
public class CombineReportsPDF {
public void concat(byte[] ba1, byte[] ba2, String outpath){
try{
//Document document = new Document();
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
//PdfWriter writer = PdfWriter.getInstance(document, baos);
//PdfAction pdfAction = new PdfAction(PdfAction.PRINTDIALOG);
//writer.setOpenAction(pdfAction);
//document.open();
//writer.addJavaScript(
// "this.print({bUI: true,bSilent: false,bShrinkToFit: true});" +
//"\r\n" +
//"this.closeDoc();"
//);
//document.add(new Paragraph("What is this doing"));
//PdfReader js1 = new PdfReader(baos.toByteArray());
PdfReader rd1 = new PdfReader( ba1 );
PdfReader rd2 = new PdfReader( ba2 );
PdfCopyFields cpy = new PdfCopyFields( new FileOutputStream( outpath ));
//cpy.addDocument(js1);
cpy.addDocument(rd1);
cpy.addDocument(rd2);
//cpy.addJavaScript( "this.print({bUI: true,bSilent:
false,bShrinkToFit: true});" +
// "\r\n" +
// "this.closeDoc();"
// );
//document.close();
cpy.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void runReport() throws EngineException
{
IReportEngine engine=null;
EngineConfig config = null;
try{
config = new EngineConfig( );
config.setBIRTHome(" C:\\birt\\birt-runtime-2_6_1\\birt-runtime-2_6_1\\ReportEngi ne ");
config.setLogConfig(null, Level.OFF);
Platform.startup( config );
IReportEngineFactory factory = (IReportEngineFactory) Platform
.createFactoryObject(
IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
engine = factory.createReportEngine( config );
}catch( Exception ex){
ex.printStackTrace();
}
IReportRunnable design, design2 = null;
//Open the report design
design = engine.openReportDesign("Reports/TopNPercent.rptdesign");
design2 =
engine.openReportDesign("Reports/TopSellingProducts.rptdesign ");
//Create task to run and render the report,
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
task.setParameterValue("Top Percentage", (new Integer(3)));
task.setParameterValue("Top Count", (new Integer(5)));
task.validateParameters();
PDFRenderOption options = new PDFRenderOption();
ByteArrayOutputStream fso= new ByteArrayOutputStream();
ByteArrayOutputStream fso2= new ByteArrayOutputStream();
options.setOutputStream(fso);
options.setOutputFormat("pdf");
task.setRenderOption(options);
task.run();
task.close();
//Create task to run and render the report,
task = engine.createRunAndRenderTask(design2);
options.setOutputStream(fso2);
options.setOutputFormat("pdf");
task.setRenderOption(options);
task.run();
task.close();
concat( fso.toByteArray(), fso2.toByteArray(),
"output/resample/Combined.pdf");
engine.destroy();
Platform.shutdown();
System.out.println("Finished");
}
/**
* @param args
*/
public static void main(String[] args) {
try
{
CombineReportsPDF ex = new CombineReportsPDF( );
ex.runReport();
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
On 5/3/2011 11:22 AM, aca wrote:
> Thanks once again for the quick response!
> I have changed to using the runTask and renderTask and now the pageCount
> works perfect.
> One last (?) question:
> I have added an empty last page by creating an empty rptdesign and
> adding that to the bao list. This works just fine, but do you know of
> another smoother way to add an empty page to the reportDocument or the
> rendered pdf?
>
> Aca
|
|
|
Re: Double sided printing of multiple reports? [message #668627 is a reply to message #668097] |
Fri, 06 May 2011 09:31   |
Eclipse User |
|
|
|
Thanks a lot for the API, I've tried it and it works. For other reasons we did however choose to use an empty rpt-design instead, but I will save the API anyway 
I'm having some problem with the pageCount which in tests turned out to be not so perfect. A printout with a dynamic list that is shown on two pages still returns a pagecount of 1. Any clue why?
Here is the create-method in which I have replaced RunAndRenderTask with a RunTask and a RenderTask.
public ByteArrayOutputStream createReport(String reportDesignName, String reportDocumentName, Object reportObject, Boolean printSettingDoubleSided)
throws ReportException {
ServletContext servletContext = WebHelper.getServletContext();
ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
IRunTask runTask = null;
IRenderTask renderTask = null;
HashMap<String, Object> appContext = new HashMap<String, Object>();
appContext.put("reportObject", reportObject);
try {
IReportEngine engine = BirtEngine.getBirtEngine();
// RUN
String designHome = servletContext.getRealPath(ReportingConstants.REPORT_DESIGN_ PATH);
log.debug("RealPath to Birt report: " + designHome);
IReportRunnable report = engine.openReportDesign(designHome + reportDesignName);
runTask = engine.createRunTask(report);
runTask.setAppContext(appContext);
String documentHome = servletContext.getRealPath(ReportingConstants.REPORT_DOCUMEN T_PATH);
runTask.run(documentHome + reportDocumentName);
// RENDER
IReportDocument reportDocument = engine.openReportDocument(documentHome + reportDocumentName);
renderTask = engine.createRenderTask(reportDocument);
renderTask.setAppContext(appContext);
HTMLCompleteImageHandler imageHandler = new HTMLCompleteImageHandler();
HTMLRenderOption options = new HTMLRenderOption();
options.setOutputFormat(IRenderOption.OUTPUT_FORMAT_PDF);
options.setOutputStream(pdfBaos);
options.setImageDirectory("./images");
options.setImageHandler(imageHandler);
renderTask.setRenderOption(options);
renderTask.render();
long pageCount = reportDocument.getPageCount();
if(printSettingDoubleSided && pageCount % 2 > 0) {
//add an empty last page and concatenate into one baos
System.out.println(reportDesignName +" har udda antal sidor");
List<ByteArrayOutputStream> pdfBaosListTemporary = new ArrayList<ByteArrayOutputStream>();
pdfBaosListTemporary.add(pdfBaos);
pdfBaosListTemporary.add(createEmptyReport());
pdfBaos = concatPdfs(pdfBaosListTemporary, false);
}
reportDocument.close();
} catch(EngineException e) {
log.error(e.toString());
throw new ReportException("[createReport] Error when creating report:" + reportDesignName);
} catch(BirtException e) {
log.error(e.toString());
throw new ReportException("[createReport] Error when trying to create reportengine in creating report:"
+ reportDesignName);
} finally {
if(runTask != null) {
runTask.close();
}
if(renderTask != null) {
renderTask.close();
}
}
//return baos;
return pdfBaos;
}
|
|
|
Re: Double sided printing of multiple reports? [message #668667 is a reply to message #668627] |
Fri, 06 May 2011 12:04   |
Eclipse User |
|
|
|
Are you saying the reportDocument.getPageCount() method is only
returning 1? Can you reproduce with the sample db so I can run the
report here and have a look at it?
BTW if you are going to render to PDF you do not need to use
HTMLRenderOption. Use PDFRenderOption like:
PDFRenderOption options = new PDFRenderOption();
options.setOption(IPDFRenderOption.PDF_HYPHENATION, true);
options.setOption(IPDFRenderOption.PDF_TEXT_WRAPPING, true);
options.setOption(IPDFRenderOption.PAGE_OVERFLOW,
IPDFRenderOption.FIT_TO_PAGE_SIZE);
//options.setOption(IPDFRenderOption.DPI, 300);
options.setSupportedImageFormats("PNG;GIF;JPG;BMP;SWF;SVG");
options.setOutputFileName("output/resample/file2.pdf");
options.setOutputFormat("pdf");
Jason
On 5/6/2011 9:31 AM, aca wrote:
> Thanks a lot for the API, I've tried it and it works. For other reasons
> we did however choose to use an empty rpt-design instead, but I will
> save the API anyway :)
>
> I'm having some problem with the pageCount which in tests turned out to
> be not so perfect. A printout with a dynamic list that is shown on two
> pages still returns a pagecount of 1. Any clue why?
>
> Here is the create-method in which I have replaced RunAndRenderTask with
> a RunTask and a RenderTask.
> public ByteArrayOutputStream createReport(String reportDesignName,
> String reportDocumentName, Object reportObject, Boolean
> printSettingDoubleSided)
> throws ReportException {
> ServletContext servletContext = WebHelper.getServletContext();
> ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
>
> IRunTask runTask = null;
> IRenderTask renderTask = null;
>
> HashMap<String, Object> appContext = new HashMap<String, Object>();
> appContext.put("reportObject", reportObject);
>
> try {
> IReportEngine engine = BirtEngine.getBirtEngine();
>
> // RUN
> String designHome =
> servletContext.getRealPath(ReportingConstants.REPORT_DESIGN_ PATH);
> log.debug("RealPath to Birt report: " + designHome);
> IReportRunnable report = engine.openReportDesign(designHome +
> reportDesignName);
>
> runTask = engine.createRunTask(report);
> runTask.setAppContext(appContext);
>
> String documentHome =
> servletContext.getRealPath(ReportingConstants.REPORT_DOCUMEN T_PATH);
> runTask.run(documentHome + reportDocumentName);
>
> // RENDER
> IReportDocument reportDocument = engine.openReportDocument(documentHome
> + reportDocumentName);
> renderTask = engine.createRenderTask(reportDocument);
> renderTask.setAppContext(appContext);
>
> HTMLCompleteImageHandler imageHandler = new HTMLCompleteImageHandler();
>
> HTMLRenderOption options = new HTMLRenderOption();
> options.setOutputFormat(IRenderOption.OUTPUT_FORMAT_PDF);
> options.setOutputStream(pdfBaos);
>
> options.setImageDirectory("./images");
> options.setImageHandler(imageHandler);
>
> renderTask.setRenderOption(options);
> renderTask.render();
>
> long pageCount = reportDocument.getPageCount();
> if(printSettingDoubleSided && pageCount % 2 > 0) {
> //add an empty last page and concatenate into one baos
> System.out.println(reportDesignName +" har udda antal sidor");
> List<ByteArrayOutputStream> pdfBaosListTemporary = new
> ArrayList<ByteArrayOutputStream>();
> pdfBaosListTemporary.add(pdfBaos);
> pdfBaosListTemporary.add(createEmptyReport());
> pdfBaos = concatPdfs(pdfBaosListTemporary, false);
> }
> reportDocument.close();
>
> } catch(EngineException e) {
> log.error(e.toString());
> throw new ReportException("[createReport] Error when creating report:" +
> reportDesignName);
> } catch(BirtException e) {
> log.error(e.toString());
> throw new ReportException("[createReport] Error when trying to create
> reportengine in creating report:"
> + reportDesignName);
> } finally {
> if(runTask != null) {
> runTask.close();
> }
> if(renderTask != null) {
> renderTask.close();
> }
> }
> //return baos;
> return pdfBaos;
> }
>
|
|
|
|
|
|
|
Re: (no subject) [message #669524 is a reply to message #668688] |
Tue, 10 May 2011 11:09  |
Eclipse User |
|
|
|
After changing the Layout preference to Fixed layout, the pageCount now works as expected for the generated pdf.
Thanks again for all the support!
Aca
|
|
|
Powered by
FUDForum. Page generated in 0.05288 seconds