2 Replies Latest reply on Sep 23, 2009 3:17 PM by Ansury

    Generate and open a dynamic PDF

    berferjo

      Hi experts,

       

      I've to create a application that invokes a webservices, one webservice returns a string with PDF content (if I take this string and I copy it in notepad and save the file with pdf extension I can see the pdf directly), now I need to do this process automatically. I need open a new pdf screen with the webservice result.

       

      Any idea to do it?

       

      Thanks in advance,

       

      Jose Manuel

        • 1. Re: Generate and open a dynamic PDF
          ScottCanada

          Why not generate PDF at server side, and launch a new window to show the file after receiving response of web service call?

          • 2. Re: Generate and open a dynamic PDF
            Ansury Level 3

            I think he's just asking how to do that?  Not really Flex related so much as web-service, but since this is a Flex forum I'll steal code from the very Flex-related AlivePDF:

             

            Sorry these crap forums trashed the formatting.  Now, I've never done what you're talking about via a web service (not a big fan) so this may not help at all, but hopefully - do you have access to an HttpServletResponse object to stream back your data?  If you do this should get you started I think.  If not you may need to do some web-service specific research.

             

            import javax.servlet.*;
            import javax.servlet.http.*;
            import java.io.*;
            
            
            public class CreatePDFServlet extends HttpServlet {
            
            public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
            {
            doGet(req, resp);
            }
            
            public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
            {
            int i = 0;
            int k = 0;
            int maxLength = req.getContentLength();
            byte[] bytes = new byte[maxLength];
            String method = req.getParameter("method");
            String name = req.getParameter("name");
            ServletInputStream si = req.getInputStream();
            while (true)
            {
            k = si.read(bytes,i,maxLength);
            i += k;
            if (k <= 0)
            break;
            }
            if (bytes != null)
            { //Check out this part here
            ServletOutputStream stream = resp.getOutputStream();
            resp.setContentType("application/pdf");
            resp.setContentLength(bytes.length);
            resp.setHeader("Content-Disposition",method + ";filename=" + name);
            stream.write(bytes);
            stream.flush();
            stream.close();
            }
            else
            {
            resp.setContentType("text");
            resp.getWriter().write("bytes is null");
            }
            }
            }