Separate PDFs for each report group element

0

Hello there!

Ok, so I've got a tricky one. I've got a report that has multiple subreports. Each one of those subreports contains a report group that puts each data element onto a new page, then the master report compiles all of those into a single PDF. I need a way to instead generate a separate PDF file for each record, while still only making 1 rest call. I also need to be able to dynamically change the PDF names to match the data element value. Any ideas?

matthew.kellar's picture
Joined: Jan 18 2016 - 8:26am
Last seen: 12 months 1 day ago

@hozawa, thank you for the response, but it looks like that link is unrelated, do you have another link?

matthew.kellar - 1 year 7 months ago

@Matthew... remove the Correct Answer Tag from Hozawa's post if you feel you did not get the answer,

so that Other users can comment on this post. Otherwise it not allowing other users to answer.

Below is the code that i wrote in java for the same purpose... try to salvage some code and write your own scriptlet in jasper reports.

/*Imports :
Commons-beanutils
Commons-digester
Commons-logging
Jasperreports
Ojdbc
Org.apache.commons.collections
 */ 

import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JFrame;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.export.ExporterInput;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimplePdfExporterConfiguration;
import net.sf.jasperreports.swing.JRViewer;

public class JasperReportFillWithSplitPDF {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws SQLException, IOException, DocumentException {
        try {
            Connection con = DriverManager.getConnection("jdbcConnectionStrings); // opens a jdbc connection


            String reportName =  "reportJrxml";

            JasperReport report = (JasperReport)
                    JRLoader.loadObjectFromFile("C:/" + reportName + ".jasper");

            Map  parameters = new HashMap();     
            
            JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, con);

            JFrame frame = new JFrame("Report");
            frame.getContentPane().add(new JRViewer(jasperPrint));
            frame.pack();
            frame.setVisible(true);


            JRPdfExporter exporter = new JRPdfExporter();
            ExporterInput inp = new SimpleExporterInput(jasperPrint);
            exporter.setExporterInput(inp);
            exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(reportName + ".pdf"));
            SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
            configuration.setPermissions(PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY);
            exporter.setConfiguration(configuration);
            exporter.exportReport();
            exporter.setExporterOutput(null);
            
            String inFile = new String(reportName + ".pdf");
            PdfReader reader = new PdfReader(inFile);
            int n = reader.getNumberOfPages();
            int i = 0;
             while ( i < n ) {
                    String outFile = inFile.substring(0, inFile.indexOf(".pdf"))
                        + "-" + String.format("%03d", i + 1) + ".pdf";
                    System.out.println ("Writing " + outFile);
                    Document document = new Document(reader.getPageSizeWithRotation(1));
                    PdfCopy writer = new PdfCopy(document, new FileOutputStream(outFile));
                    document.open();
                    PdfImportedPage page = writer.getImportedPage(reader, ++i);
                    writer.addPage(page);
                    document.close();
                    writer.close();
                }

        }
        catch (JRException e) {
            e.printStackTrace();
        }
    }
}

 

 

 

reportdev - 1 year 7 months ago

1 Answer:

1

https://stackoverflow.com/questions/12688778/increase-tomcat-memory-sett... only output 1 pdf per report. You'll have to create your own program that uses JasperReports library to do that.

I've done what you've asked before several times so I'm definitely sure that it's possible if you build your own program.

hozawa's picture
56562
Joined: Apr 24 2010 - 4:31pm
Last seen: 7 months 2 weeks ago
Feedback
randomness