Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

output a list of JasperPrint object to PDF got blank pages before each item #7

Open
richso opened this issue Dec 1, 2022 · 5 comments

Comments

@richso
Copy link

richso commented Dec 1, 2022

I have a report which composed of different Jasperreport templates, e.g. template1 and template2, therefore we use ArrayList to collect the JasperPrint Objects and generate the report into PDF like this:

JasperFillManager fm = new JasperFillManager();
JasperPrint jp1 = fm.fillReport("template1.jasper", ...);
JasperPrint jp2 = fm.fillReport("template1.jasper", ...);
List list = new java.util.ArrayList();
list.add(jp1);
list.add(jp2);
JRPdfExporter pdfExporter = new JRPdfExporter();

///////////////////////////////////////////////////////////////////////////////////
// NOTE: this set of code is required for using itext7 support instead of itext2

JasperReportsContext defJrCtx = net.sf.jasperreports.engine.DefaultJasperReportsContext.getInstance();
defJrCtx.setProperty(pdfExporter.PDF_PRODUCER_FACTORY_PROPERTY, "com.jaspersoft.jasperreports.export.pdf.modern.ModernPdfProducerFactory");

////////////////////////////////////////////////----------------------------------

pdfExporter.setExporterInput(SimpleExporterInput.getInstance(list));
FileOutputStream fos = new FileOutputStream('test.pdf')
pdfExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(fos));
pdfExporter.exportReport();

The above code will generate the PDF file with an extra blank page before each items in the ArrayList, i.e. before contents of template1 and before contents of template2; on commenting out the itext7 support code (use itext2), the output will not have the extra blank pages; is there any way to instruct the ModernPdfProducerFactory / itext7 to NOT outputting the extra blank pages?

@dadza
Copy link
Collaborator

dadza commented Dec 1, 2022

The problem is caused by the superfluous newPage call at https://github.com/TIBCOSoftware/jasperreports/blob/2703d84eb5fd8008f910d7612b74dbba6fc7ffc6/jasperreports/src/net/sf/jasperreports/engine/export/JRPdfExporter.java#L978

It needs to be fixed in the core JasperReports library by removing that call.

@dadza
Copy link
Collaborator

dadza commented Dec 1, 2022

PS: The "classic" PDF producer is not affected because the newPage implementation in iText 2.x is idempotent in a way that subsequent calls do not actually result in a new page being created when no content has been written since the previous call.

@richso
Copy link
Author

richso commented Dec 2, 2022

Hi @dadza

I just downloaded the version 6.20.0 of the Jasperreports library source code and commented the line 978 and later also line 1013 (both are newPage call) and rebuild the jar, both the generated PDF still same as before, i.e. with extra blank page before each items in the ArrayList. Any other thing can be done to fix?

@dadza
Copy link
Collaborator

dadza commented Dec 2, 2022

Can you double check that you are indeed running the code you modified? E.g. put a System.out/err statement and confirm that the line is printed.

If the problem persists after commenting pdfProducer.newPage(); at line 978, we'll need a test case to reproduce it because the test that I made was fixed by that change.

@richso
Copy link
Author

richso commented Dec 5, 2022

@dadza
Yes, you are right, my previous build using Netbeans which opens the pom file only builds the "jasperreports-6.20.0.jar" but not "jasperreports-javaflow-6.20.0.jar"; just found that the modified codes are inside "jasperreports-javaflow-6.20.0.jar" therefore my previous test was not working properly. After I changed to build using "ant alljars", it builds also the "jasperreports-javaflow-6.20.0.jar" and the result is as you said.

Many thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants