YOUR FEEDBACK
NGASI Releases AppServer Manager 8.1
Dave Jenkins wrote: The remote server management is a welcomed added feature...


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
MXDJ TOP LINKS YOU MUST CLICK ON !


Creating PDF Documents from Flex Applications
A little different approach for PDF creation from Flex

Digg This!

There are three ways of creating PDF documents from Adobe Flex applications:

•    by using the forms/LiveCycle designer - this process is well documented and requires someone to create XDP form (an Acrobat XML Data Package file), the data model and establish a process of synchronizing Flex application with LiveCycle

•    by treating PDF printing as one of the features of your Flex application, seamlessly providing printing functionality (with or without LiveCycle Designer)

•    by using Snapshots available in LCDS, which uses bitmaps to represent Flash content

While the first process provides guaranteed quality and predictable results, it also requires a double effort of developing XDP and Flex forms and introduces the model and communications that can be foreign to the application domain.

The third type of printing is good for screen-only PDFs due to low DPI used by the screen snapshots. Flex 3 should provide control over the DPI resolution.

The second process is more applicable when the document structure is dynamic (like the end-user customizable report or form). It often produces better printing quality with the current version of LiveCycle Data Services ES (formerly Flex Data Services) Flex. The idea is to send the server not the data for merging with the form, but already merged compressed document in XDP format containing both data and formatting.  In this case LCDS layer just needs to process it with XDPXFAHelper class and returns it as a PDF stream to the browser for displaying.
The next code sample is the server side part written as JavaServer Page (createPDF.jsp). Note, that you need to have a commercial license of LCDS as its Express version does not support PDF generation.

<%@ page language="java"
    import="java.io.IOException,
javax.servlet.http.HttpServletRequest,
flex.acrobat.pdf.XDPXFAHelper,
java.util.zip.Inflater,
java.util.zip.InflaterInputStream"
%>
<jsp:directive.page import="java.io.InputStream"/>
<jsp:directive.page import="java.io.ByteArrayInputStream"/>
<jsp:directive.page import="java.io.FileInputStream"/>
<jsp:directive.page import="java.io.FileOutputStream"/>
<%
        //Load compressed XDP stream (received from client)
            //into a byte array
        byte[] result = new byte[5000000];
        int resultLength =0 ;
        try {
            Inflater decompresser = new Inflater(  );
            byte[] ba = new byte[500000];
            ServletInputStream si = request.getInputStream();
            int k = 0;
            int i = 0;
             while (true ) {
                 k = si.read(ba, i, 500000);
                 i +=k;
                 if (k<=0) break;
            }
            try {//Uncompress data into result
                decompresser.setInput(ba);
                resultLength  = decompresser.inflate(result);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } catch (IOException e) {
            throw new ServletException(e);
        }

      //The next line won’t work unless you have a commercial
        // LCDS. It’s not available in Express edition
        XDPXFAHelper helper = new XDPXFAHelper();
        
        try { //load the input into PDF convertor
            helper.openFromByteArray( result );
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Save new PDF as a byte array
        byte[] bytes = helper.saveToByteArray();

        //Send PDF to the client(GZIP compression can be
        // provided by an external filter)
        ServletOutputStream out2 = response.getOutputStream();
        response.setContentType("application/pdf");
        response.setContentLength(bytes.length);
        out2.write(bytes);
        out2.flush();
        out2.close();
%>

On the client (Flex) side, you need a few simple classes. First you would need an ActionScript class that sends generated XDP to the server and opens the browser page for displaying the incoming PDF:

    import flash.net.*;
    import flash.utils.ByteArray;
    
    public class FormRenderer
    {
        public static function openPdf(xdp:String,
                    target:String="_blank"):void
        {
            var req:URLRequest = new URLRequest("/createPDF.jsp");                req.method = URLRequestMethod.POST;
            var ba :ByteArray = new ByteArray();;
            ba.writeMultiByte(xdp, "iso-8859-1");
            ba.compress();
            ba.position = 0;
            req.data = ba;
            navigateToURL(req, target);
        }
    }
}

Now all we need is an XDP file with the data and presentation. While it is feasible to make an XDP file programmatically we usually recommend to start with blank templates made in LiveCycle Designer (comes with Acrobat Professional) matching the printer’s paper size/corporate stationary. XDP documents are XML objects, and EcmaScript for XML (e4x) makes the XDP building process in Flex a breeze. For example:

1.    Declare a variable of type XML, and using e4x syntax read the XDP template that matches the document and the paper size. A fragment of XDP template may look like this:
  <?xml version="1.0" encoding="UTF-8"?>
<?xfa generator="AdobeLiveCycleDesigner_V8.0" APIVersion="2.5.6290.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/" timeStamp="2007-01-25T10:40:38Z" uuid="784f469b-2fd0-4555-a905-6a2d173d0ee1">
<template xmlns="http://www.xfa.org/schema/xfa-template/2.5/">
   <subform name="form1" layout="tb" restoreState="auto" locale="en_US">
      <pageSet>
         <pageArea name="Page1" id="Page1">
            <contentArea x="0.25in" y="0.25in" w="8in" h="10.5in"/>
            <medium stock="letter" short="8.5in" long="11in"/>
            <?templateDesigner expand 1?></pageArea>
         <?templateDesigner expand 1?></pageSet>
      <subform w="8in" h="10.5in" name="YourPageAttachedHere"/>
      <proto/>
      <desc>
         <text name="version">8.0.1291.1.339988.308172</text>
      </desc>
      </subform>
   </template>
    …

2.    Select a Flex container or control that you are going to print, i.e. a Panel, a DataGrid et al.

3.    Query the object from step 2, get its attributes and children and create the XML preparing this object for printing. Attach the XML to the template as a page. If you have your own presentation framework, all components can implement your own interface (the code below shows IXdpContent with the only getter function xmlContent) that allows to traverse the list of elements on the screen for their XDP content. You can also have a processor that would provide the XML for standard classes. Here is implementation of generic DataGridItemRenderer to illustrate how you can prepare for printing just one datagrid cell:
package printing
{
    import com.theriabook.controls.superGridClasses.DataGridItemRenderer;

    public class DataGridItemRendererXdp extends DataGridItemRenderer
                                            implements IXdpElement
    {
        public  function get xmlContent():Object
        {
            var o:XML = <draw  x={convert(x)} w={convert(width)}
                               h={convert(height)}>
                  <ui>
                     <textEdit>
                     </textEdit>
                  </ui>
                     <value><text>{text}</text></value>
                        <font typeface={getStyle("fontFamily")}
                            size={(getStyle("fontSize")*4/5)+"pt"}
                            weight={getStyle("fontWeight")}
                            posture={getStyle("fontStyle")}
                          underline={getStyle("textDecoration")=="normal"?1:0}
                            >
                            <fill>
                                <color value={rgb(getStyle("color"))}/>
                    </fill>
                            </font>
                    <margin topInset="0.5mm" bottomInset="0.5mm" leftInset="0.5mm" rightInset="0.5mm"/>

                  <border>
                     <edge presence="hidden"/>
                     <edge presence="visible"/>
                     <edge presence="hidden"/>
                     <edge presence="hidden"/>
                  </border>
               </draw>;

          return o;
        }
        private function convert(val:Number) : String {
            return val/96 + "in"; //convert 96 dpi to inches
        }
        private function rgb(val:Number) : String {
            
            return (val >> 16 & 255) + "," + (val >> 8 & 255) + "," + (val  & 255);
        }
        
    }
}

4.    Repeat the process of attaching XML to the XDP template using E4X until all print pages are ready.

In my opinion, this method of printing from Flex requires less efforts for reporting. It might also provide better printing quality and searchability within printed document.

About Anatole Tartakovsky
Anatole Tartakovsky is a Managing Principal of Farata Systems. He?s responsible for creation of frameworks and reusable components. Anatole authored number of books and articles on AJAX, XML, Internet and client-server technologies. He holds an MS in mathematics. You can reach him at atartakovsky@faratasystems.com

LATEST FLEX STORIES & POSTS
AJAX World - Sun Talks Up its Late-to-the-Party AIR-Silverlight Rival
At Java One this week Sun has been selling its year -old-but-still-upcoming - and definitely late-to-the-party - Adobe AIR- and Microsoft Silverlight-competitive JavaFX Rich Client environment as a potential revenue-generator capable of putting ads on mobile applications and JavaFX Scri
AJAX World - Xceed Launches Microsoft Silverlight 2 Control
Xceed launched Xceed Upload for Silverlight, the commercial offering in support of Microsoft's promising new Silverlight technology. The product is available now for purchase or as a fully functional 45-day trial on Xceed's website. Xceed Upload for Silverlight lets developers add uplo
Microsoft To Keynote 4th International Virtualization Conference & Expo
Mike Neil is general manager for virtualization strategy in the Windows Server Division at Microsoft. Mike is focused on the delivery of the Windows virtualization technology, including Windows Server 2008 Hyper-V, Microsoft Hyper-V Server and Virtual PC 2007. Mike also directs the tec
AJAX World - Skyway Software Announces RIA Developer Contest
According to Sean Walsh, President and CEO of Skyway Software, 'Our Skyway Community is thriving and our members are very talented. We truly look forward to their RIAs submittals and Skyway Builder extensions and are excited that all of the contributions will benefit the entire Skyway
"Virtualization Journal" Debuts This Week at JavaOne
Founded in 2006, SYS-CON Media's 'Virtualization Journal' is the world's first magazine devoted exclusively to what Gartner has earmarked as the single highest-impact IT trend through 2012: virtualization. And now it will be available on newsstands worldwide, as SYS-CON Media seeks to
3rd International Virtualization Conference & Expo: Themes & Topics
From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE