Skip to Content
1

Crystal reports enterprise export pdf

Dec 21, 2017 at 08:21 PM

320

avatar image

Hi All, I am newbie to crystal. I have a easier task like to get the report from Crystal server(managed) and show in PDF or excel formats. I have used all the approaches below but i am not getting reports. I got so many examples from blogs but its making me very confused and unsuccesful in getting report as pdf/excel. We use SAP BusinessObjects BI Platform 4.2 Support Pack 4

Version: 14.2.4.2410 (crystal enterprise or CR4e)

approach 1)

<%@ page import="com.crystaldecisions.sdk.occa.report.application.*"%> <%@ page import="com.crystaldecisions.sdk.occa.report.definition.*"%> <%@ page import="com.crystaldecisions.sdk.occa.report.data.*"%> <%@ page import="com.crystaldecisions.sdk.occa.report.lib.*"%> <%@ page import="com.crystaldecisions.sdk.framework.*"%> <%@ page import="com.crystaldecisions.sdk.occa.managedreports.*"%> <%@ page import="com.crystaldecisions.sdk.occa.infostore.*"%> <%@ page import="com.crystaldecisions.sdk.occa.report.exportoptions.*"%> <%@ page import="java.util.*"%> <%@ page import="java.io.*"%> <% /* String username = "Administrator"; String password = ""; String cmsname = "localhost:6400"; String Authen = "secEnterprise"; */ String cmsname = "dddd"; // name of crystal server String username = "dd"; String password = "ddd"; String Authen = "dd"; String sReportName = "test.rpt"; //connecting to Enterprise IEnterpriseSession es = CrystalEnterprise.getSessionMgr().logon(username, password, cmsname, Authen); //get the report App Factory form the Crystal Enterprise IReportAppFactory appFactory = (IReportAppFactory) es.getService("", "RASReportService"); //IReportSourceFactory appFactory = (IReportSourceFactory) es.getService("PSReportFactory"); //get the infostore service form the Crystal Enterprise IInfoStore istore = (IInfoStore) es.getService("", "InfoStore"); //get the report by name from crystal Enterprise IInfoObjects iobjects = istore .query("Select * From CI_INFOOBJECTS Where SI_NAME = 'test.rpt' and SI_INSTANCE = 0 "); //open the report in the report doc object. ReportClientDocument Doc = appFactory.openDocument((IInfoObject) iobjects.get(0), 0, Locale.ENGLISH); // WORKING WITH THE PRINT OUTPUT CONTROLLER //Use the report documents PrintOutputController to export the report to a ByteArrayInputStream ByteArrayInputStream byteIS = (ByteArrayInputStream) Doc.getPrintOutputController() .export(ReportExportFormat.recordToMSExcel); // EXPORTING THE REPORT //Create a byte[] (same size as the exported ByteArrayInputStream) byte[] buf = new byte[2000 * 1024]; int nRead = 0; //Set response headers to indicate pdf MIME type and inline file response.reset(); response.setHeader("Content-disposition", "inline;filename=ramz"); response.setContentType("application/xls"); //Send the Byte Array to the Client while ((nRead = byteIS.read(buf)) != -1) { response.getOutputStream().write(buf, 0, nRead); } //Flush the output stream response.getOutputStream().flush(); //Close the output stream response.getOutputStream().close(); %>

Error: pageserver is not accessible, contact administrator. i download eclipse integrated with crystal and tried with those jars too.

Approach 2:

package testcrystal; import java.io.IOException; import java.util.Locale; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.crystaldecisions.sdk.exception.SDKException; import com.crystaldecisions.sdk.framework.CrystalEnterprise; import com.crystaldecisions.sdk.framework.IEnterpriseSession; import com.crystaldecisions.sdk.occa.infostore.IInfoObject; import com.crystaldecisions.sdk.occa.infostore.IInfoObjects; import com.crystaldecisions.sdk.occa.infostore.IInfoStore; import com.crystaldecisions.sdk.occa.managedreports.IReportSourceFactory; import com.crystaldecisions.sdk.occa.report.data.Fields; import com.crystaldecisions.sdk.occa.report.data.ParameterField; import com.crystaldecisions.sdk.occa.report.data.ParameterFieldDiscreteValue; import com.crystaldecisions.report.web.viewer.*; import javax.servlet.http.*; import java.util.*; import com.crystaldecisions.sdk.occa.report.data.*; import com.crystaldecisions.sdk.occa.report.reportsource.IReportSource; public class TestServlet extends HttpServlet { private static final String CONTENT_TYPE = "text/html"; // Initialize global variables public void init() throws ServletException { } // Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); HttpSession session = request.getSession(); IInfoStore iStore = null; IInfoObjects oInfoObjects = null; IInfoObject oInfoObject = null; IEnterpriseSession es = null; IReportSource reportSource = null; // Mandatory settings /*String cmsReport = request.getParameter("cmsReport"); if (cmsReport == null) return; String cmsUser = request.getParameter("cmsUser"); if (cmsUser == null) return; // Settings with defaults String cmsPass = request.getParameter("cmsPassword"); if (cmsPass == null) cmsPass = ""; String cmsAuth = request.getParameter("cmsAuth"); if (cmsAuth == null) cmsAuth = "secEnterprise"; String cmsHost = request.getParameter("cmsServer"); if (cmsHost == null) cmsHost = "crystal";*/ String sCMS = "ddd"; // name of crystal server String sID = "dd"; String sPw = "ddd"; String sAuthenticationType = "secEnterprise"; // Parameters Enumeration names = request.getParameterNames(); Map parameters = new HashMap(); while (names.hasMoreElements()) { String key = (String) names.nextElement(); if (key.startsWith("parm")) { String ident = key.substring("parm".length(), key.length()); String name = request.getParameter(key); String value = request.getParameter("value" + ident); String type = request.getParameter("type" + ident); // Only support discrete values at the moment Values cvalue = new Values(); ParameterFieldDiscreteValue pfdv = new ParameterFieldDiscreteValue(); pfdv.setValue(value); cvalue.add(pfdv); parameters.put(name, cvalue); } } // Logon and obtain an Enterprise Session try { es = CrystalEnterprise.getSessionMgr().logon(sID, sPw, sCMS, sAuthenticationType); session.setAttribute("enterpriseSession", es); } catch (SDKException e) { e.printStackTrace(); return; } String sReportName = "ddd.rpt"; try { iStore = (IInfoStore) es.getService("", "InfoStore"); oInfoObjects = (IInfoObjects) iStore .query("Select * from CI_INFOOBJECTS Where SI_PROGID = 'CrystalEnterprise.Report' And SI_INSTANCE=0 And SI_NAME='" + sReportName + "'"); } catch (Exception e) { e.printStackTrace(); return; } // retrieve the latest instance of the report oInfoObject = (IInfoObject) oInfoObjects.get(0); try { System.out.println("before connecting to page server"); IReportSourceFactory factoryPS = (IReportSourceFactory) es.getService( "PSReportFactory"); System.out.println("after connecting to page server"); reportSource = factoryPS.openReportSource((oInfoObject), Locale.ENGLISH); System.out.println(reportSource); } catch (Exception e) { e.printStackTrace(); } // Create a viewer, set the source to the Enterprise report CrystalReportViewer viewer = new CrystalReportViewer(); try { viewer.setReportSource(reportSource); } catch (Exception e) { e.printStackTrace(); } // Get the parameter fields Fields parmFields = null; try { parmFields = viewer.getParameterFields(); } catch (Exception e) { e.printStackTrace(); return; } /** * * Loop through the user parameters and find is they * * match the report parameters. If so, add the values * * to the field. Do not use the Crystal documented method * * of viewer.setParameterFields, it will not work. The * * values must be added directly to the existing object. * */ Set keySet = parameters.keySet(); Iterator parmIter = keySet.iterator(); while (parmIter.hasNext()) { String name = (String) parmIter.next(); Iterator reportIter = parmFields.iterator(); while (reportIter.hasNext()) { ParameterField field = (ParameterField) reportIter.next(); if (field.getName().toUpperCase().equals(name.toUpperCase())) field.setCurrentValues((Values) parameters.get(name)); } } viewer.setOwnPage(true); viewer.setOwnForm(true); viewer.setPrintMode(CrPrintMode.PDF); // Log the user //System.out.println("**CRYSTAL: " + request.getRemoteAddr() + " cmsReport: " + cmsReport + " cmsUser: " + cmsUser //+ " ParmCount: " + parameters.size()); // Now redirect to the report try { viewer.processHttpRequest(request, response, getServletConfig().getServletContext(), null); } catch (Exception e) { e.printStackTrace(); } } // Process the HTTP Put request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } // Clean up resources public void destroy() { } }

Problem: so much jars conflict: this method not found, and goes on and on. finally

Can some on provide exact jars information where i can retrieve reports in pdf format.

Jars used for approach 1:

aspectjrt.jar bcm.jar ceaspect.jar cecore.jar celib.jar ceplugins_core.jar ceplugins.jar cereports.jar cesession.jar com.azalea.ufl.barcode.1.0.jar commons-collections-3.1.jar commons-configuration-1.2.jar commons-lang-2.1.jar commons-logging.jar corbaidl.jar cryptojFIPS.jar CrystalCommon2.jar CrystalReportsRuntime.jar cvom.jar DatabaseConnectors.jar derby.jar ebus405.jar icu4j.jar jai_imageio.jar JDBInterface.jar jrcerom.jar keycodeDecoder.jar log4j.jar logging.jar marks.jar pfjgraphics.jar QueryBuilder.jar rasapp.jar rascore.jar sap.com~tc~sec~csi.jar Serialization.jar TraceLog.jar URIUtil.jar webreporting-jsf.jar webreporting.jar xpp3.jar Xtreme.jar

please give one full example with all jars included for CR4e pdf export using managed server(ie. reports on server)

Thanks,

Kris

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

6 Answers

Ashok guduru Jan 03 at 09:12 PM
0

Lets assume restful is only option. The below code throwing error for the call

HttpGet httpget = new HttpGet("http://xxxx:6405/biprws/infostore/105347/rpt/export?mime_type=application/pdf"); httpget.addHeader("Accept", exportFormatHeader); httpget.addHeader("X-SAP-LogonToken", logonToken);

status code is::;404 response body is::::<error> <error_code>404</error_code> <message>Resource not supported for the requested object. (RWS 00010)</message> </error>

Code:

package testcrystal; import java.io.IOException; import java.io.InputStream; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.params.ConnRoutePNames; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import com.crystaldecisions.sdk.occa.report.data.Values; import com.crystaldecisions.sdk.occa.report.exportoptions.ReportExportFormat; import com.crystaldecisions.sdk.occa.report.lib.ReportSDKException; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import com.crystaldecisions.sdk.exception.SDKException; import com.crystaldecisions.sdk.framework.CrystalEnterprise; import com.crystaldecisions.sdk.framework.IEnterpriseSession; import com.crystaldecisions.sdk.occa.infostore.IInfoObject; import com.crystaldecisions.sdk.occa.infostore.IInfoObjects; import com.crystaldecisions.sdk.occa.infostore.IInfoStore; import com.crystaldecisions.sdk.occa.managedreports.IReportAppFactory; import com.crystaldecisions.sdk.occa.managedreports.IReportSourceFactory; import com.crystaldecisions.sdk.occa.report.application.OpenReportOptions; import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument; import com.crystaldecisions.sdk.occa.report.data.FieldValueType; import com.crystaldecisions.sdk.occa.report.data.Fields; import com.crystaldecisions.sdk.occa.report.data.IParameterField; import com.crystaldecisions.sdk.occa.report.data.ParameterField; import com.crystaldecisions.sdk.occa.report.data.ParameterFieldDiscreteValue; import com.crystaldecisions.sdk.occa.report.data.ParameterFieldRangeValue; import com.crystaldecisions.sdk.occa.report.data.RangeValueBoundType; import com.crystaldecisions.sdk.occa.security.ILogonTokenMgr; import com.crystaldecisions.sdk.plugin.desktop.common.IReportParameter; import com.crystaldecisions.sdk.plugin.desktop.report.IReport; import com.sun.org.apache.xerces.internal.parsers.DOMParser; import com.crystaldecisions.report.web.viewer.*; //import com.crystaldecisions.report.web.viewer.*; import com.crystaldecisions.sdk.occa.report.data.*; public class TestServlet5 extends HttpServlet { private static final long serialVersionUID = -4751096228274971485L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // response.getWriter().println("Hello World!"); String sCMS = "dd"; // name of crystal server String sID = "dd"; String sPw = "dd"; String sAuthenticationType = "secEnterprise"; HttpSession session = request.getSession(true); // session.invalidate(); // Enable Fiddler Trace. This causes the requests to go through a proxy on port // 8888 which fiddler listens on boolean enableFiddler = false; // Sample Variables String reportID = "105347"; // Restful URL's final String baseURL = "http://xxxxxx/biprws"; final String logonURL = baseURL + "/logon/long"; // final String logonURL = baseURL + "/logon/token"; final String logoffURL = baseURL + "/logoff"; final String baseWebiURL = baseURL + "/raylight/v1/documents"; String infoStoreQueryURL = baseURL + "/v1/infostore"; String infoStoreQueryURL1 = baseURL + "/infostore"; try { String xmlString = ""; String documentID = ""; String logonToken = ""; // First check to see if we have a logonToken stored in session. If so, use it. if (session.getAttribute("logonToken") != null) { System.out.println("LogonToken found </br>"); logonToken = (String) session.getAttribute("logonToken"); System.out.println("In quotations logon token: "+logonToken); } else { // No logontoken detected - so create one System.out.println("No LogonToken found - Creating one </br>"); xmlString = "<attrs><attr name=\"userName\" type=\"string\" >" + sID + "</attr><attr name=\"password\" type=\"string\" >" + sPw + "</attr><attr name=\"auth\" type=\"string\" possibilities=\"secEnterprise,secLDAP,secWinAD,secSAPR3\">" + sAuthenticationType + "</attr></attrs>"; String logonXML = restPost(enableFiddler, logonURL, xmlString, "", "", "", ""); System.out.println("my name is token ::"+logonXML); // The quotes are added because the webi URL require quotes around the token logonToken = "\"" + getLogonTokenFromXML(logonXML) + "\""; System.out.println("In quotations logon token: "+logonToken); // Now that we have a logonToken - it must be included in the header for a // future RestFul calls session.setAttribute("logonToken", logonToken); } String exportFormatHeader; String fileExt; String exportURL; //System.out.println("infoStoreQueryURL is::" + infoStoreQueryURL); exportURL = infoStoreQueryURL1 + "/" + reportID; //System.out.println("final url is::" + exportURL); // Excel 2003 // exportFormatHeader = "application/vnd.ms-excel"; // fileExt = "xls"; // Excel 2007 // exportFormatHeader = // "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; // fileExt = "xls"; // PDF exportFormatHeader = "application/xml"; fileExt = "pdf"; // XML // exportFormatHeader = "text/xml"; // fileExt = "xml"; // Now do a httpGet to Export HttpClient httpclient = new DefaultHttpClient(); try { if (enableFiddler) { HttpHost proxy = new HttpHost("127.0.0.1", 8888, "http"); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } HttpGet httpget = new HttpGet("http://xxxx:6405/biprws/infostore/105347/rpt/export?mime_type=application/pdf"); httpget.addHeader("Accept", exportFormatHeader); httpget.addHeader("X-SAP-LogonToken", logonToken); HttpResponse myResponse = httpclient.execute(httpget); int statusCode = myResponse.getStatusLine().getStatusCode(); System.out.println("status code is::;" + statusCode); // Note: EntityUtils is really inefficient when exporting large files due to how // it handles data buffering. HttpEntity entity = myResponse.getEntity(); String responseBody = (String) EntityUtils.toString(entity); System.out.println("response body is::::" + responseBody); // If we don't convert to a byteArray - we will end up with a corrupted file. /*byte[] byteAr = (byte[]) EntityUtils.toByteArray(entity); //System.out.println(byteAr.toString()); // Set the file name response.setHeader("Content-disposition", "attachment;filename=tirumala." + fileExt); response.setContentType(exportFormatHeader); response.setContentLength(byteAr.length); response.getOutputStream().write(byteAr); // Flush the output stream response.getOutputStream().flush(); // Close the output stream response.getOutputStream().close();*/ } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); // session.invalidate(); } } catch (IOException eIO) { System.out.println("IO Exception: " + eIO); } catch (Exception ex) { System.out.println("Exception: " + ex); } } public String getLogonTokenFromXML(String xmlString) throws Exception { Document doc = convertStringToDom(xmlString); NodeList nodes = doc.getElementsByTagName("attr"); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); // Is this the correct XML token if (element.getAttribute("name").equals("logonToken")) { return (element.getTextContent()); } } return (""); } public Document convertStringToDom(String domXMLSTring) throws Exception { DOMParser parser = new DOMParser(); parser.parse(new InputSource(new java.io.StringReader(domXMLSTring))); return (parser.getDocument()); } public static String restPost(Boolean enableFiddler, String urlStr, String XMLString, String param1Name, String param1Value, String param2Name, String param2Value) throws Exception { HttpClient httpclient = new DefaultHttpClient(); try { System.out.println("inside restpost : " + urlStr); System.out.println("inside restpost : " + XMLString); /* * if (enableFiddler) { HttpHost proxy = new HttpHost("127.0.0.1", 8888, * "http"); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, * proxy); } */ HttpPost httpPost = new HttpPost(urlStr); httpPost.addHeader("Accept", "application/xml"); httpPost.addHeader("Content-Type", "application/xml"); if (!param1Name.equals("")) { httpPost.addHeader(param1Name, param1Value); } if (!param2Name.equals("")) { httpPost.addHeader(param2Name, param2Value); } httpPost.setEntity(new StringEntity(XMLString)); HttpResponse response = httpclient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); String responseBody = (String) EntityUtils.toString(entity); return (responseBody); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } } @Override public void init() throws ServletException { System.out.println("Servlet " + this.getServletName() + " has started"); } @Override public void destroy() { System.out.println("Servlet " + this.getServletName() + " has stopped"); } }

Share
10 |10000 characters needed characters left characters exceeded
Ashok guduru Jan 02 at 08:15 PM
0

Hi Don, What is the common approach to open crystal reports(managed ie. reports on crystal server). Is it open document URL or Restful or SDK. Restful is very poor option while working with very large reports (streaming). Please suggest. we use the below version.

SAP BusinessObjects BI Platform 4.2 Support Pack 4
Version: 14.2.4.2410

Share
10 |10000 characters needed characters left characters exceeded
Don Williams
Jan 02 at 02:35 PM
0

Hi Kris,

CR for Enterprise Reports cannot be opened using .NET or Java SDK's. This is by design, only option is to use the Windows version of Crystal Reports or possibly the RESTful SDK can open those reports. But RESTful cannot do anything else with them.

You can find RESTful samples here:

https://wiki.scn.sap.com/wiki/display/BOBJ/RESTful+APIs

Don

Share
10 |10000 characters needed characters left characters exceeded
Dell Stinnett-Christy Jan 09 at 04:54 PM
0

I recommend looking at OpenDocument. It becomes a fairly easy thing to get the IEnterpriseSession using the Java SDK and then build the OpenDocument URL. You can use the "sOutputFormat" in the URL for opening the document as either PDF or Excel. Documentation can be found at http://help.sap.com/bobip.

-Dell

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Hi Dell, Open document url redirects the url to BOE server url from my application. Is it safe to give the open document url to external users (ie. out side of the app and not in the network)?

0

My clients have handled this a couple of ways:

1. Put the OpenDocument URL in an IFrame, which will hide the URL.

2. Open the OpenDocument URL in a window that does not include an address bar, which will also hide the URL.

Either of these will prevent the vast majority of users from seeing the URL. Also, if you manage the session token well, people who see the URL will not be able to use it to log in because it won't be valid. At that point, it will open with a log-in screen for BOBJ instead of showing the report.

-Dell

0
Ashok guduru Jan 11 at 08:18 PM
0

Hi Ted/Don,

I am getting the error below at the line IPDF pdfPlugin = (IPDF) infoObject; Can you please let me know the resolution for this? we use BI 4.2 crystal enterprise reports(managed)

java.lang.ClassCastException: com.crystaldecisions.sdk.occa.infostore.internal.SchedulableCategoryContentInfoObject cannot be cast to com.crystaldecisions.sdk.plugin.desktop.pdf.IPDF

IInfoObject infoObject = (IInfoObject) oInfoObjects.get(0);

IPDF pdfPlugin = (IPDF) infoObject; //Stream content back to Web browser. response.reset(); response.setContentType(pdfPlugin.getMimeType());

response.setHeader("Content-disposition", "attachment;filename=test.pdf");

byte[] bytes = pdfPlugin.getContent(); ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(bytes); outputStream.close();

Share
10 |10000 characters needed characters left characters exceeded
Don Williams
Jan 12 at 02:29 PM
0

Hi Ashok,

Typically when redeploying/build your Java redist packages or even updating they tend to get mixed versions. Make sure all JAR's being used are from the \Java\Lib folder.

For samples on how to do most stuff with BOE Java SDK go here:

https://wiki.scn.sap.com/wiki/display/BOBJ

Then under this title you'll find all sorts of samples for all of the SDK's:

Business Intelligence Platform SDKs

Thanks again

Don

Share
10 |10000 characters needed characters left characters exceeded