az0 Posted February 11, 2010 Share Posted February 11, 2010 dear all,In my web based application we have used jasper report successfully to create a pdf and forward to the browser(PdfServlet). now i have a new requirement to create pdfs of the same and store in a file server. so If i tried to export the same compiled jasper using the JasperExportManager.exportReportToPdfFile, after doing necessary works, method to exports, but the pdf generated have different page orientations than the one created in servlets. For example i have 2 jasper to create a pdf. i create two byte[] streams and combine to a single pdf in which one is in portait and another in landscape. The generated pdf was not in the specified orientation. should there be any properties to be set to solve this problem? is the joining pdf using itext creating the problem? I found the joining pdf code from the web, i think thats a common code used. although i tried to export this pdf to two than combining it to one, still the problem persists. Thanks in advane for your help VijayjasperInfo is a bean having configuration of the jasperprivate boolean doGenerate() { ///other works................. while (iterator.hasNext()) { JasperReport jasperReport = null; Entry<String, JasperConfig> entry = (Entry<String, JasperConfig>) iterator .next(); JasperConfig jasperInfo = entry.getValue(); _COMPILE_: { if (jasperInfo.isPreCompile()) { compileJasperFile(jasperInfo); } } _JASPER_LOAD_: { try { jasperReport = (JasperReport) JRLoader.loadObject(jasperInfo .getJasperFilePath()); if (jasperReport == null) return false; } catch (JRException e) { System.err .println("Could not create JasperReport, problem in configuration\n" + e); return false; } } JasperPrint jasperPrint = null; _FILL_JASPER_: { jasperPrint = fillJasper(jasperReport, jasperInfo); if (jasperPrint == null) { System.err .println("Could not create JasperPrint, problem in configuration"); return false; } if(jasperInfo.getPageWidth()>0) jasperPrint.setPageWidth(jasperInfo.getPageWidth()); if(jasperInfo.getPageHeight()>0) jasperPrint.setPageHeight(jasperInfo.getPageHeight()); } try { _EXPORT$_:{ /*JRPdfExporter exporter = new JRPdfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream(fileName)); exporter.exportReport();*/ // i tried this way, but didnt solve the problem JasperExportManager.exportReportToPdfFile(jasperPrint, "build/"+(counter++)+".pdf"); byte[] pdfBytes = JasperExportManager .exportReportToPdf(jasperPrint); pdfData.add(new ByteArrayInputStream(pdfBytes)); //add to a list } } catch (JRException e) { System.err.println("Could not create pdf, problem generation"); e.printStackTrace(); } } if (pdfData.size() > 0) { boolean flag = joinPDFs(pdfData, fileName, false); //join them, the code is below. if (flag) generatedFilePath = fileName; return flag; } return false; } public boolean joinPDFs(List<InputStream> pdfStreams, String fileName, boolean paginate) { Document document = new Document(); FileOutputStream mergedPdfFile = null; try { mergedPdfFile = new FileOutputStream(fileName); } catch (FileNotFoundException e1) { e1.printStackTrace(); } try { List<InputStream> pdfs = pdfStreams; List<PdfReader> readers = new ArrayList<PdfReader>(); int totalPages = 0; Iterator<InputStream> iteratorPDFs = pdfs.iterator(); // Create Readers for the pdfs. while (iteratorPDFs.hasNext()) { InputStream pdf = iteratorPDFs.next(); PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); totalPages += pdfReader.getNumberOfPages(); } // Create a writer for the outputstream PdfWriter writer = PdfWriter.getInstance(document, mergedPdfFile); document.open(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); PdfContentByte cb = writer.getDirectContent(); // Holds the PDF // data PdfImportedPage page; int currentPageNumber = 0; int pageOfCurrentReaderPDF = 0; Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; currentPageNumber++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); // Code for pagination. if (paginate) { cb.beginText(); cb.setFontAndSize(bf, 9); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + " of " + totalPages, 520, 5, 0); cb.endText(); } } pageOfCurrentReaderPDF = 0; } mergedPdfFile.flush(); document.close(); mergedPdfFile.close(); return true; } catch (Exception e) { e.printStackTrace(); } finally { if (document.isOpen()) document.close(); try { if (mergedPdfFile != null) mergedPdfFile.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } return false; } Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now