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

Problems displaying a PDF


Java_Jasper
Go to solution Solved by Java_Jasper,

Recommended Posts

Hello,

 

I am having issues getting a pdf to display. I can bring up a pdf in the JasperViewer but I am trying to just get a pdf to open independent of the viewer.

I have included the code for the viewer, only to show that is is working.

I have attache my code.

Code:
private static final String Report_Def = ******MY JRMXL FILE*********private static final String Report_DataSource = ******MY DB CONNECTION********public String ActionButton(){  try{ //Load jrmxl fileJasperDesign reportDefinition = JRMxlLoader.load(new FileInputStream(new File(Reprot_Def);//CompileJasper Report report = JasperCompileManager.compileReport(reportDefinition);//Get DB DatainitialContext initalContext = new initialContext();DataSourec ds = (DataSource)initialContext.lookup(Report_DataSource);//Fill the jrmxl template with dataJasperPrint print = JasperFillManager.fillReport(report, new HashMap<String, Object>(), ds.getConnection());//View in jasperViewerJasperViewer.viewReport(print, false);//Display in pdfOutputStream output = new FileOutputStream(new File(***MY CREATED PDF FROM ABOVVE***));JasperExportManager.exportReportToPdfFile(print, **LOCATION OF MY FILE***);}catch(Exception e){e.printStackTrace();}}[/code]
Link to comment
Share on other sites

  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

  • Solution

Hello all,

For your enjoyment --- TAG LINE   "Display PDF in Adobe Reader"

 

//DECLARE LOCATION OF YOUR JRXML FILEprivate static final String Report_Def = ******MY JRMXL FILE LOCATION ON C DRIVE*********//Resource REF example://jdbc/<your DB name>ConnectionDS //javax.sql.DataSource//Containerprivate static final String Report_DataSource = ******MY DB CONNECTION********   public String ActionButton(){  try{ //LOAD THE JRMXL REPORT TEMPLATEJasperDesign reportDefinition = JRMxlLoader.load(new FileInputStream(new File(Reprot_Def);//COMPILE THE REPORT TEMPLATEJasper Report report = JasperCompileManager.compileReport(reportDefinition);//GET THE CONNECTION TO THE DATABASEinitialContext initalContext = new initialContext();DataSourec ds = (DataSource)initialContext.lookup(Report_DataSource);//FILL THE COMPILED REPORT TEMPLATEJasperPrint print = JasperFillManager.fillReport(report, new HashMap<String, Object>(), ds.getConnection());//********DISPLAY IN ADOBE READER*********//CREATE AN OUTPUT STREAM WHERE DATA IS WRITTEN TO BYTE ARRAYByteArrayOutputStream output = new ByteArrayOutputStream();//CALL UPON THE EXPORT MANAGER TO LOAD JASPER PRINT AND BYTE ARRAYJasperExportManager.exportReportToPdfStream(print, output);//DECLARE A STRING WHERE YOU WILL SAVE THE REPORTfinal String PDF_FILE = "C;/****LOCATION YOU WANT TO SAVE THE NEW REPORT****);//CREATE A NEW STREAM AND CALL THE CONVERTED, SAVED REPORTOutputStream pdfFile = new FileOutputStream(new File(PDF_FILE));//WRITE THE PDFpdfFile.write(output.toByteArray());//CLEAN THE PDFpdfFile.flush();//CLOSE ITpdfFile.close();//RUN YOUR BRAND NEW PDF REPROTfinal String PDF_RUN = "rundll32 url.dll, FileProtocolHandaler " + PDF_FILE; //Don't forget the space between 32 and URL.//EXECUTE THE COMMANDRuntime.getRuntime().exec(PDF_RUN);}catch(Exception e){e.printStackTrace();}}

 



Post Edited by bhayman1 at 04/25/2012 18:21
Link to comment
Share on other sites

  • 2 years later...

//RUN YOUR BRAND NEW PDF REPROT
final String PDF_RUN = "rundll32 url.dll, FileProtocolHandler " + PDF_FILE; //Don't forget the space between 32 and URL.

//EXECUTE THE COMMAND
Runtime.getRuntime().exec(PDF_RUN);

 

thanks Java_Jasper you saved my life.

JRExporter.exportReport() generate report in to .metadata.pluginsxxxx folder but no pdf report show in browser. ( i don't know what happened, the other project showed safely) so i need to force its to display.

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...