How To Create PDF in Flex 3


1. Creating PDF in Flex 3, Here is the Application code

Pre Requeste:# AlivePdf.swc [ include in ur Flex Project By dropping into libs folder ] Download AlivePDF

You can find answer in this post, for the question ” How to create PDF from Flex 3″ or “By Capturing Screen How to create PDF from Flex 3″

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; layout=”absolute”>
<mx:Script>
<![CDATA[

import org.alivepdf.saving.Download;
import org.alivepdf.saving.Method;
import org.alivepdf.layout.Size;
import org.alivepdf.layout.Unit;
import org.alivepdf.layout.Orientation;
import org.alivepdf.pdf.PDF;
import org.alivepdf.pages.Page;
import org.alivepdf.images.ResizeMode;
import org.alivepdf.images.ImageFormat;

private var myPDF:PDF ;

private function GeneratePDF(event:MouseEvent):void{
myPDF = new PDF(Orientation.PORTRAIT, Unit.MM, Size.A4);
var newPage:Page=new Page(Orientation.PORTRAIT, Unit.MM, Size.A4);
myPDF.addPage(newPage);
myPDF.addImage(testPanel, 5 ,5, testPanel.width,testPanel.height, ImageFormat.PNG, 0, 1, ResizeMode.FIT_TO_PAGE);

myPDF.save(Method.REMOTE, “http://localhost:8080/FlexPDF/servlet/ScreenShotPDFServlet&#8221;, Download.INLINE, “myPDF.pdf”);
// You can modify the code to position the captured image in generated PDF

}
]]>
</mx:Script>
<mx:Panel x=”20″ y=”10″ width=”250″ height=”200″ layout=”absolute” id=”testPanel” title=”testPanel”>
<mx:Button x=”84″ y=”128″ label=”Generate PDF” click=”GeneratePDF(event);”/>
</mx:Panel>
</mx:Application>

****************

2. Here is Java Servlet Code, which generates PDF …

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ScreenShotPDFServlet extends HttpServlet {

public ScreenShotPDFServlet() {
super();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType(“application/pdf”);
response.setHeader(“Content-disposition”,”inline; filename=myPDF.pdf” );

int contentLength = request.getContentLength();
byte[] byteArray = new byte[contentLength];
ServletOutputStream sos = response.getOutputStream();
ServletInputStream  sis  = request.getInputStream();
response.setContentLength(contentLength);
sis.read(byteArray,0,contentLength-1);
sos.write(byteArray);
}

}

3. web.xml  entry of the servlet

<servlet>
<servlet-name>ScreenShotPDFServlet</servlet-name>
<servlet-class>com.spm.servlets.ScreenShotPDFServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ScreenShotPDFServlet</servlet-name>
<url-pattern>/servlet/ScreenShotPDFServlet</url-pattern>
</servlet-mapping>

Note..

1. This  servlet generates PDF from flex client with the captured ScreenShot ..

2. GET method called From Flex Client

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s