Jump to content
We've recently updated our Privacy Statement, available here ×

Separate PDFs for each report group element


matthew.kellar
Go to solution Solved by hozawa,

Recommended Posts

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?

Link to comment
Share on other sites

  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

@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();
        }
    }
}

 

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...