1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.ctv.util;
35
36 import java.io.ByteArrayOutputStream;
37 import java.io.IOException;
38 import java.sql.Connection;
39 import java.util.Map;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44 import net.sf.jasperreports.engine.JRException;
45 import net.sf.jasperreports.engine.JasperCompileManager;
46 import net.sf.jasperreports.engine.JasperFillManager;
47 import net.sf.jasperreports.engine.JasperPrint;
48 import net.sf.jasperreports.engine.JasperReport;
49 import net.sf.jasperreports.engine.export.JRPdfExporter;
50 import net.sf.jasperreports.export.SimpleExporterInput;
51 import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
52
53 public class EditionUtils
54 {
55
56 private final static Log LOGGER = LogFactory.getLog( EditionUtils.class );
57
58 public static byte [ ] getEdition( Connection connection, String editionPath, Map<String, Object> parameters )
59 {
60 ByteArrayOutputStream output = new ByteArrayOutputStream( );
61 try
62 {
63 JasperReport report = JasperCompileManager.compileReport( editionPath );
64 JasperPrint print = JasperFillManager.fillReport( report, parameters, connection );
65 JRPdfExporter exporter = new JRPdfExporter( );
66 SimpleOutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput( output );
67 exporter.setExporterInput( new SimpleExporterInput( print ) );
68 exporter.setExporterOutput( exporterOutput );
69 exporter.exportReport( );
70
71 exporterOutput.close( );
72 }
73 catch( JRException e )
74 {
75 LOGGER.error( "[EditionUtils.getEdition]Erreur lors de la génération du pdf", e );
76 }
77
78 byte [ ] result = output.toByteArray( );
79
80 try
81 {
82 output.flush( );
83 output.close( );
84 }
85 catch( IOException e )
86 {
87 LOGGER.error( "[EditionUtils.getEdition]Erreur lors de la fermeture du stream", e );
88 }
89
90 return result;
91 }
92 }