1 28 package net.sf.jasperreports.engine; 29 30 import java.io.ByteArrayOutputStream ; 31 import java.io.File ; 32 import java.io.InputStream ; 33 import java.io.OutputStream ; 34 35 import net.sf.jasperreports.engine.export.JRHtmlExporter; 36 import net.sf.jasperreports.engine.export.JRPdfExporter; 37 import net.sf.jasperreports.engine.export.JRXmlExporter; 38 import net.sf.jasperreports.engine.export.JRXmlExporterParameter; 39 import net.sf.jasperreports.engine.util.JRLoader; 40 41 42 59 public class JasperExportManager 60 { 61 62 63 72 public static String exportReportToPdfFile(String sourceFileName) throws JRException 73 { 74 File sourceFile = new File (sourceFileName); 75 76 77 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 78 79 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".pdf"); 80 String destFileName = destFile.toString(); 81 82 exportReportToPdfFile(jasperPrint, destFileName); 83 84 return destFileName; 85 } 86 87 88 96 public static void exportReportToPdfFile( 97 String sourceFileName, 98 String destFileName 99 ) throws JRException 100 { 101 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFileName); 102 103 exportReportToPdfFile(jasperPrint, destFileName); 104 } 105 106 107 115 public static void exportReportToPdfFile( 116 JasperPrint jasperPrint, 117 String destFileName 118 ) throws JRException 119 { 120 121 JRPdfExporter exporter = new JRPdfExporter(); 122 123 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 124 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFileName); 125 126 exporter.exportReport(); 127 } 128 129 130 138 public static void exportReportToPdfStream( 139 InputStream inputStream, 140 OutputStream outputStream 141 ) throws JRException 142 { 143 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(inputStream); 144 145 exportReportToPdfStream(jasperPrint, outputStream); 146 } 147 148 149 157 public static void exportReportToPdfStream( 158 JasperPrint jasperPrint, 159 OutputStream outputStream 160 ) throws JRException 161 { 162 JRPdfExporter exporter = new JRPdfExporter(); 163 164 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 165 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); 166 167 exporter.exportReport(); 168 } 169 170 171 179 public static byte[] exportReportToPdf(JasperPrint jasperPrint) throws JRException 180 { 181 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 182 183 JRPdfExporter exporter = new JRPdfExporter(); 184 185 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 186 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); 187 188 exporter.exportReport(); 189 190 return baos.toByteArray(); 191 } 192 193 194 210 public static String exportReportToXmlFile( 211 String sourceFileName, 212 boolean isEmbeddingImages 213 ) throws JRException 214 { 215 File sourceFile = new File (sourceFileName); 216 217 218 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 219 220 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".jrpxml"); 221 String destFileName = destFile.toString(); 222 223 exportReportToXmlFile( 224 jasperPrint, 225 destFileName, 226 isEmbeddingImages 227 ); 228 229 return destFileName; 230 } 231 232 233 247 public static void exportReportToXmlFile( 248 String sourceFileName, 249 String destFileName, 250 boolean isEmbeddingImages 251 ) throws JRException 252 { 253 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFileName); 254 255 exportReportToXmlFile( 256 jasperPrint, 257 destFileName, 258 isEmbeddingImages 259 ); 260 } 261 262 263 278 public static void exportReportToXmlFile( 279 JasperPrint jasperPrint, 280 String destFileName, 281 boolean isEmbeddingImages 282 ) throws JRException 283 { 284 JRXmlExporter exporter = new JRXmlExporter(); 285 286 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 287 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFileName); 288 exporter.setParameter(JRXmlExporterParameter.IS_EMBEDDING_IMAGES, 289 isEmbeddingImages ? Boolean.TRUE : Boolean.FALSE); 290 291 exporter.exportReport(); 292 } 293 294 295 304 public static void exportReportToXmlStream( 305 InputStream inputStream, 306 OutputStream outputStream 307 ) throws JRException 308 { 309 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(inputStream); 310 311 exportReportToXmlStream(jasperPrint, outputStream); 312 } 313 314 315 324 public static void exportReportToXmlStream( 325 JasperPrint jasperPrint, 326 OutputStream outputStream 327 ) throws JRException 328 { 329 JRXmlExporter exporter = new JRXmlExporter(); 330 331 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 332 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); 333 334 exporter.exportReport(); 335 } 336 337 338 347 public static String exportReportToXml(JasperPrint jasperPrint) throws JRException 348 { 349 StringBuffer sbuffer = new StringBuffer (); 350 351 JRXmlExporter exporter = new JRXmlExporter(); 352 353 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 354 exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sbuffer); 355 356 exporter.exportReport(); 357 358 return sbuffer.toString(); 359 } 360 361 362 373 public static String exportReportToHtmlFile( 374 String sourceFileName 375 ) throws JRException 376 { 377 File sourceFile = new File (sourceFileName); 378 379 380 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 381 382 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".html"); 383 String destFileName = destFile.toString(); 384 385 exportReportToHtmlFile( 386 jasperPrint, 387 destFileName 388 ); 389 390 return destFileName; 391 } 392 393 394 405 public static void exportReportToHtmlFile( 406 String sourceFileName, 407 String destFileName 408 ) throws JRException 409 { 410 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFileName); 411 412 exportReportToHtmlFile( 413 jasperPrint, 414 destFileName 415 ); 416 } 417 418 419 430 public static void exportReportToHtmlFile( 431 JasperPrint jasperPrint, 432 String destFileName 433 ) throws JRException 434 { 435 JRHtmlExporter exporter = new JRHtmlExporter(); 436 437 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 438 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFileName); 439 440 exporter.exportReport(); 441 } 442 443 444 } 445 | Popular Tags |