Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: need to save PDF to file on server Post Reply Post New Topic
Author Message
mikeb280
Newbie
Newbie


Joined: 29 Jan 2008
Online Status: Offline
Posts: 2
Quote mikeb280 Replybullet Topic: need to save PDF to file on server
     Posted: 29 Jan 2008 at 7:40am
using Rational Software Architect 7,
Crystal Reports for RAD 11.5.3 (component SDKs),
WebSphere v6.1

I need to convert a report to PDF and save it on the server.  I have the jsp below working but it returns the PDF to the web browser.

Is there a way to get the PDF out of the response object?
Or is there another way to convert the report to a PDF?

Thanks in advance


<%@ page import="com.crystaldecisions.report.web.viewer.ReportExportControl" %>
<%@ page import="com.crystaldecisions.reports.sdk.ReportClientDocument" %>
<%@ page import="com.crystaldecisions.sdk.occa.report.exportoptions.ExportOptions" %>
<%@ page import="com.crystaldecisions.sdk.occa.report.exportoptions.ReportExportFormat" %>
<%@ page import="com.crystaldecisions.sdk.occa.report.exportoptions.PDFExportFormatOptions" %>
<%
    Object reportSource = session.getAttribute("reportSource");
    if (reportSource == null) {
       String report = "/CrystalReport1.rpt";
       ReportClientDocument reportClientDoc = new ReportClientDocument();
       reportClientDoc.open(report, 0);
       reportSource = reportClientDoc.getReportSource();
       session.setAttribute("reportSource", reportSource);
    }
    ReportExportControl exportControl = new ReportExportControl();
    ExportOptions exportOptions = new ExportOptions();
    exportOptions.setExportFormatType(ReportExportFormat.PDF);
    PDFExportFormatOptions PDFExpOpts = new PDFExportFormatOptions();
    exportOptions.setFormatOptions(PDFExpOpts);   
    exportControl.setReportSource(reportSource);
    exportControl.setExportOptions(exportOptions);
    exportControl.processHttpRequest(request, response, getServletConfig().getServletContext(), null); 
%>
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 29 Jan 2008 at 8:05am
From the Developer Library website:
To initialize the export control
  1. To set the report source of the control, call its setReportSource method.

    For information about how to get a report source, see How to get a report source from the Java Reporting Component.

        exportControl.setReportSource(reportSource);

  2. To call the setExportOptions method of the control, pass it an ExportOptions object.

    exportControl.setExportOptions(exportOptions);

  3. Set the ExportAsAttachment property to true, if you want give users the option to open the exported file or save the file to disk.

    When ExportAsAttachment is False, the file is automatically displayed in the application that is associated with the type of the exported file. Users then need to save the file from inside the application.

    exportControl.setExportAsAttachment(true);

Note:    If setExportAsAttachment is set to false, the report is opened in the user's browser window, if the browser supports that type of file.

If you look at item 3, you'll see that setting the setExportAsAttachment flag will allow the user to either open or save the attachment to a file.  This is probably not exactly what you're looking for, but it may be a step in the right direction.

There is sample code for saving an exported CSV file that you might be able to adapt for your purposes.
 
-Dell
IP IP Logged
mikeb280
Newbie
Newbie


Joined: 29 Jan 2008
Online Status: Offline
Posts: 2
Quote mikeb280 Replybullet Posted: 29 Jan 2008 at 12:02pm
Thanks hilfy,

Your response sent me in the right direction.  I found this thread on java.sun.com

http://forum.java.sun.com/thread.jspa?threadID=5123092&messageID=9434894

and here is my updated jsp that works

<%@ page import="com.crystaldecisions.reports.sdk.ReportClientDocument" %>
<%@ page import="com.crystaldecisions.reports.sdk.PrintOutputController" %>
<%@ page import="com.crystaldecisions.sdk.occa.report.exportoptions.ReportExportFormat" %>
<%@ page import="java.io.*" %>
<%
    Object reportSource = session.getAttribute("reportSource");
    ReportClientDocument reportClientDoc = null;
    if (reportSource == null) {
       String report = "/CrystalReport1.rpt";
       reportClientDoc = new ReportClientDocument();
       reportClientDoc.open(report, 0);
       reportSource = reportClientDoc.getReportSource();
       session.setAttribute("reportSource", reportSource);
    }
    PrintOutputController printOutputController = reportClientDoc.getPrintOutputController();
    ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream) printOutputController.export(ReportExportFormat.PDF);
   
    byte byteArray[] = new byte[byteArrayInputStream.available()];
   
    File file = new File("C:\\mikemikemike.pdf");
    FileOutputStream fileOutputStream = new FileOutputStream(file);
   
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(byteArrayInputStream.available());
    int x = byteArrayInputStream.read(byteArray, 0, byteArrayInputStream.available());
   
    byteArrayOutputStream.write(byteArray, 0, x);
    byteArrayOutputStream.writeTo(fileOutputStream);
   
    byteArrayInputStream.close();
    byteArrayOutputStream.close();
    fileOutputStream.close();
%>
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum