<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>James Ward - RIA Cowboy &#187; PDF</title>
	<atom:link href="http://www.jamesward.com/category/pdf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jamesward.com</link>
	<description>Rich Internet Applications &#124; Flex &#124; Adobe AIR &#124; Java &#124; Open Source &#124; Linux &#124; Enterprise Software &#124; Cloud</description>
	<lastBuildDate>Mon, 26 Jul 2010 19:15:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>RIAs on the Web, on the Desktop, and in a PDF</title>
		<link>http://www.jamesward.com/2009/09/13/rias-on-the-web-on-the-desktop-and-in-a-pdf/</link>
		<comments>http://www.jamesward.com/2009/09/13/rias-on-the-web-on-the-desktop-and-in-a-pdf/#comments</comments>
		<pubDate>Sun, 13 Sep 2009 21:30:51 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[Adobe AIR]]></category>
		<category><![CDATA[Flash Player]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[PDF]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/blog/?p=1200</guid>
		<description><![CDATA[Some believe that the &#8220;Internet&#8221; in &#8220;rich Internet application&#8221; (RIA) means that RIAs must only run in the browser. However my definition of RIA is not constrained to only web-based applications. RIAs can run anywhere: web, desktop, mobile devices, TVs, or even inside PDFs. Ideally we should have some level of code and library reusability [...]]]></description>
			<content:encoded><![CDATA[<p>Some believe that the &#8220;Internet&#8221; in &#8220;rich Internet application&#8221; (RIA) means that RIAs must only run in the browser.  However my <a href="http://www.jamesward.com/blog/2007/10/17/what-is-a-rich-internet-application/">definition of RIA</a> is not constrained to only web-based applications.  RIAs can run anywhere: web, desktop, mobile devices, TVs, or even <a href="http://www.jamesward.com/blog/2008/11/05/portable-rias-flex-apps-in-pdfs/">inside PDFs</a>.  Ideally we should have some level of code and library reusability between these environments.  However to think that we can reuse the entire application is a pipe dream.  Client capabilities and end user needs vary too greatly between these mediums.</p>
<p>I wanted to build an application in Flex that shows how applications can have a high degree of reuse between the web, the desktop, and in a PDF.  I decided to build a Mortgage Calculator to illustrate this.  Here is the web widget:<br />
<iframe src="/mortgageCalc/mortgageCalcWeb.html" width="100%" height="540"></iframe></p>
<p>From within the web widget you can install the desktop widget or email yourself a PDF containing the widget.  Since this application is a small, self contained application (i.e. a widget) the functionality between the different mediums is very similar.  In this case I was able to reuse about 99% of the code between the different versions.  However, sometimes achieving that level of reuse is not possible due to the differences in client capabilities and the end user needs.  This is the case with the Flex and Adobe AIR based <a href="http://www.oracle.com/applications/crm/siebel/crm-gadgets.html">Oracle CRM Gadgets</a>, which are for different use cases than the primary Siebel UI.</p>
<p>There are beginning to be more instances where RIA widgets are being reused across different mediums.  But this is only one piece of software development.  In other instances the capabilities and functionality of web, desktop, and mobile applications vary so greatly that there is little reuse.  Either way it&#8217;s important to architect our back ends such that they are agnostic to the front end.  This is one of the ways RIA and mobile app development have changed the way we build software.  It&#8217;s a good thing and we should embrace it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2009/09/13/rias-on-the-web-on-the-desktop-and-in-a-pdf/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Portable RIAs Gaining Traction</title>
		<link>http://www.jamesward.com/2009/04/03/portable-rias-gaining-traction/</link>
		<comments>http://www.jamesward.com/2009/04/03/portable-rias-gaining-traction/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 19:44:48 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[PDF]]></category>
		<category><![CDATA[RIA]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/blog/?p=868</guid>
		<description><![CDATA[It&#8217;s great to see the concept of Portable RIAs beginning to crop up in real world applications. The latest one I&#8217;ve seen is on ESPN &#8211; allowing the user to check out the latest March Madness updates from within a PDF. Check it out:]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s great to see the concept of <a href="http://www.jamesward.com/blog/portable-rias-tutorial/">Portable RIAs</a> beginning to crop up in real world applications.  The latest one I&#8217;ve seen is on ESPN &#8211; allowing the user to check out the latest March Madness updates from within a PDF.  Check it out:</p>
<p><a href="http://promo.espn.go.com/espn/specialsection/adobe/"><img src="http://www.jamesward.com/blog/wp-content/uploads/2009/04/portable_ria_espn.jpg" alt="Portable RIA - ESPN" title="Portable RIA - ESPN" width="611" height="874" class="aligncenter size-full wp-image-869" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2009/04/03/portable-rias-gaining-traction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adobe Reader 9.1 for Linux Released!</title>
		<link>http://www.jamesward.com/2009/03/25/adobe-reader-91-for-linux-released/</link>
		<comments>http://www.jamesward.com/2009/03/25/adobe-reader-91-for-linux-released/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 16:17:51 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[PDF]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/blog/?p=846</guid>
		<description><![CDATA[Adobe has released Adobe Reader 9.1 for Linux! This is the first 9.x release on Linux and has a number of new features. My personal favorite is the inclusion of Flash Player so that Portable RIAs will work on Linux! Portable RIAs are beginning to catch on more and so it&#8217;s great to have true [...]]]></description>
			<content:encoded><![CDATA[<p>Adobe has released <a href="http://get.adobe.com/reader/">Adobe Reader 9.1 for Linux</a>!  This is the first 9.x release on Linux and has a number of new features.  My personal favorite is the inclusion of Flash Player so that <a href="http://www.jamesward.com/blog/portable-rias-tutorial/">Portable RIAs</a> will work on Linux!  Portable RIAs are beginning to catch on more and so it&#8217;s great to have true cross-platform support for them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2009/03/25/adobe-reader-91-for-linux-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Portable RIAs &#8211; Flex Apps in PDFs</title>
		<link>http://www.jamesward.com/2008/11/05/portable-rias-flex-apps-in-pdfs/</link>
		<comments>http://www.jamesward.com/2008/11/05/portable-rias-flex-apps-in-pdfs/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 19:54:00 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[PDF]]></category>
		<category><![CDATA[RIA]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/wordpress/?p=491</guid>
		<description><![CDATA[Update: I&#8217;ve posted another Portable RIA demo. The web began as a platform for browsing, finding, and exchanging documents. Over the past ten years the web has moved beyond this document-centric role, and is now a platform for exchanging data. We typically refer to web sites used for data exchange as web applications. The next [...]]]></description>
			<content:encoded><![CDATA[<p><font color="red">Update: I&#8217;ve posted another <a href="http://www.jamesward.com/2009/09/13/rias-on-the-web-on-the-desktop-and-in-a-pdf/">Portable RIA demo</a>.</font></p>
<p>The web began as a platform for browsing, finding, and exchanging documents. Over the past ten years the web has moved beyond this document-centric role, and is now a platform for exchanging data. We typically refer to web sites used for data exchange as web applications. The next major evolution of the web is underway as web applications become more interactive and useful. The industry now refers to these next generation web applications as rich Internet applications or RIAs.</p>
<p>Another popular means of document exchange is the Portable Document Format (PDF). Like the web, PDFs are also evolving into more than just a document exchange technology. When RIAs are inserted into PDFs, this familiar format for documents becomes a method for exchanging and interacting with data. The primary benefits of using PDFs for data exchange are that PDFs can easily be secured, emailed around, and accessed when offline.</p>
<p>I’ve put together a demo illustrating this concept. You will need Reader 9 in order for this to work. First load <a href="/portablerias/PortableRIAs.html">the Flex Dashboard application</a> application in your browser. Then in the application click the &#8220;Create PDF&#8221; button. Now you should be looking at the same data inside of a PDF! You can even save the PDF locally and view it when disconnected. You can also refresh the data from the server from the PDF or from the browser.</p>
<p><a href="/portablerias/PortableRIAs.html"><img src="/wordpress/wp-content/uploads/2008/10/portable_rias-reader.jpg" title="Portable RIAs" width="800"/></a></p>
<p>This works by using a template PDF containing the Flex application without any data. When the user asks for a PDF the data is inserted into the PDF template and delivered to the user. Optionally the PDF could be secured with PDF policy protection before being sent to the user. You can also create these PDFs in a nightly batch process.</p>
<p><strong>Getting Started</strong></p>
<p>You need several pieces to build a portable RIA. First, you need a Flex application that the user will interact with in their browser and inside a PDF. Second, you need some place for the Flex application to get its data. Third, you need a PDF template and a server that can insert the application data into it when the user requests a PDF. I used the following steps to build these pieces:</p>
<ol>
<li>Create the back-end data source for the application
<li>Create the basic Flex front-end application
<li>Create a back-end PDF generation service
<li>Create a PDF with form fields that will be used to store the data
<li>Modify the Flex application to read data from the PDF
<li>Add a way for the user to generate a PDF from the Flex application
<li>Merge the final Flex application into the PDF template
</ol>
<p>To complete this tutorial you’ll need a Java web app server (Tomcat, JBoss, WebLogic, WebSphere, or similar), LiveCycle Data Services 2.6, Flex Builder 3 (installed as a plugin for Eclipse), Acrobat 9 to create the PDF, and optionally Adobe LiveCycle Designer 8.2. </p>
<p>Let’s walk through each of these seven steps in detail.</p>
<p>Download the <a href="/downloads/portable_rias.zip">source code</a> for tutorial.</p>
<p><strong>Step 1: Creating the back-end data source</strong></p>
<p>The application needs some data so I created a JSP that generates some fake and random sales data. This data is returned from the server in XML format. One challenge in building this was that Reader needs a &#8220;*&#8221; crossdomain.xml policy file on the server that offers the data. Having a &#8220;*&#8221; policy can be a security risk, especially when the data is on an internal server or on a server which uses cookies for authentication. In my production demo I didn’t want to put a &#8220;*&#8221; policy file on my demo server so I serve the data from a different server. Because of this I ended up using PHP for my public demo. However, for the purposes of this tutorial I will just use a simple JSP file. Here is the file, data.jsp:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span> response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;utf-8&quot;</span><span style="color: #339933;">?&gt;</span>
<span style="color: #339933;">&lt;</span>list<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;%</span> 
<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> months <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;Jan&quot;</span>, <span style="color: #0000ff;">&quot;Feb&quot;</span>, <span style="color: #0000ff;">&quot;Mar&quot;</span>, <span style="color: #0000ff;">&quot;Apr&quot;</span>, <span style="color: #0000ff;">&quot;May&quot;</span>, <span style="color: #0000ff;">&quot;Jun&quot;</span>, <span style="color: #0000ff;">&quot;Jul&quot;</span>, <span style="color: #0000ff;">&quot;Aug&quot;</span>, <span style="color: #0000ff;">&quot;Sep&quot;</span>, <span style="color: #0000ff;">&quot;Oct&quot;</span>, <span style="color: #0000ff;">&quot;Nov&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span> 
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> months.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> 
  <span style="color: #009900;">&#123;</span> 
    <span style="color: #003399;">String</span> month <span style="color: #339933;">=</span> months<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;-0&quot;</span> <span style="color: #339933;">+</span> i<span style="color: #339933;">;</span> 
    <span style="color: #000066; font-weight: bold;">int</span> apacRev <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">60000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #000066; font-weight: bold;">int</span> europeRev <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">80000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #000066; font-weight: bold;">int</span> japanRev <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">50000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #000066; font-weight: bold;">int</span> latinAmRev <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">30000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #000066; font-weight: bold;">int</span> northAmRev <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">100000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #000066; font-weight: bold;">int</span> totalRev <span style="color: #339933;">=</span> apacRev <span style="color: #339933;">+</span> europeRev <span style="color: #339933;">+</span> japanRev <span style="color: #339933;">+</span> latinAmRev <span style="color: #339933;">+</span> northAmRev<span style="color: #339933;">;</span> 
    <span style="color: #000066; font-weight: bold;">int</span> averageRev <span style="color: #339933;">=</span> totalRev <span style="color: #339933;">/</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">;</span> 
<span style="color: #339933;">%&gt;</span> 
    <span style="color: #339933;">&lt;</span>month name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;%=month%&gt;&quot;</span> revenue<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;%=totalRev%&gt;&quot;</span> average<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;%=averageRev%&gt;&quot;</span><span style="color: #339933;">&gt;</span> 
        <span style="color: #339933;">&lt;</span>region name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;APAC&quot;</span> revenue<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;%=apacRev%&gt;&quot;</span><span style="color: #339933;">/&gt;</span> 
        <span style="color: #339933;">&lt;</span>region name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Europe&quot;</span> revenue<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;%=europeRev%&gt;&quot;</span><span style="color: #339933;">/&gt;</span> 
        <span style="color: #339933;">&lt;</span>region name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Japan&quot;</span> revenue<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;%=japanRev%&gt;&quot;</span><span style="color: #339933;">/&gt;</span> 
        <span style="color: #339933;">&lt;</span>region name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Latin America&quot;</span> revenue<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;%=latinAmRev%&gt;&quot;</span><span style="color: #339933;">/&gt;</span> 
        <span style="color: #339933;">&lt;</span>region name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;North America&quot;</span> revenue<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;%=northAmRev%&gt;&quot;</span><span style="color: #339933;">/&gt;</span> 
    <span style="color: #339933;">&lt;/</span>month<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;%</span> 
  <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span> 
<span style="color: #339933;">%&gt;</span> 
<span style="color: #339933;">&lt;/</span>list<span style="color: #339933;">&gt;</span></pre></div></div>

<p>You will also need to add a crossdomain.xml policy file to your root web containing the following:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cross-domain-policy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;site-control</span> <span style="color: #000066;">permitted-cross-domain-policies</span>=<span style="color: #ff0000;">&quot;master-only&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;allow-access-from</span> <span style="color: #000066;">domain</span>=<span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cross-domain-policy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Make sure that you do not use this policy file in production, as you should never have a &#8220;*&#8221; policy on an internal server or on a domain which uses cookies for authentication.  For more information on secure cross-domain communication in Flash Player see:<br />
<a href="http://www.adobe.com/devnet/flashplayer/articles/secure_swf_apps.html">http://www.adobe.com/devnet/flashplayer/articles/secure_swf_apps.html</a></p>
<p><strong>Step 2: Creating the Flex front-end application</strong></p>
<p>Before I could use the typical sales dashboard in the demo, I first had to create that dashboard in Flex. Luckily this was easy since one of the out-of-the-box LiveCycle Data Services samples has a dashboard. I made a few modifications to the dashboard and then added it to my Flex application. You can test the dashboard by creating a HTTPService and attaching the data to the Dashboard. Here is my application (you will need the Dashboard source code from the LiveCycle Data Services sample application for this to compile):</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Application</span> <span style="color: #000066;">xmlns:mx</span>=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> <span style="color: #000066;">xmlns:local</span>=<span style="color: #ff0000;">&quot;*&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">applicationComplete</span>=<span style="color: #ff0000;">&quot;srv.send()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:HTTPService</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;srv&quot;</span> <span style="color: #000066;">url</span>=<span style="color: #ff0000;">&quot;http://localhost:8080/data.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:Dashboard</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">dataProvider</span>=<span style="color: #ff0000;">&quot;{srv.lastResult.list.month}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><strong>Step 3: Creating a back-end PDF generation service</strong><br />
I used LiveCycle Data Services to handle generating the PDF for the user. To set up this service I copied the WEB-INF/flex and WEB-INF/lib directories from the flex.war file included with LiveCycle Data Services to my root web application. I then added some necessary configuration code to my web.xml file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span> 
<span style="color: #00bbdd;">&lt;!DOCTYPE web-app PUBLIC &quot;-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quot; &quot;http://java.sun.com/dtd/web-app_2_3.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-app<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Portable RIAs Demo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Portable RIAs Demo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>flex.messaging.HttpFlexSession<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MessageBrokerServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MessageBrokerServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>flex.messaging.MessageBrokerServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;init-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>services.configuration.file<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/WEB-INF/flex/services-config.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/init-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>PDFResourceServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Helper for retrieving dynamically generated PDF documents.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.jamesward.portablerias.PDFResourceServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MessageBrokerServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/messagebroker/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>PDFResourceServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/dynamic-pdf/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/web-app<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The PDFResourceServlet is a custom class that returns the generated PDF to the user. I also created a class named PDFService that actually generates the PDF. Both of these classes are derived from the PDF samples in LiveCycle Data Services. To create these classes I first set up a new Java project in Eclipse. I named the project &#8220;portablerias_server&#8221; and pointed its root directory to the WEB-INF directory, its src directory to the WEB-INF/src directory (which you need to create), and its output directory to the WEB-INF/classes directory. I then created a new Java class called &#8220;com.jamesward.portablerias.PDFService&#8221; containing the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.jamesward.portablerias</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.w3c.dom.Document</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">flex.acrobat.pdf.XFAHelper</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">flex.messaging.FlexContext</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">flex.messaging.FlexSession</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">flex.messaging.util.UUIDUtils</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PDFService 
<span style="color: #009900;">&#123;</span> 
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> generatePDF<span style="color: #009900;">&#40;</span><span style="color: #003399;">Document</span> dataset<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> 
    <span style="color: #009900;">&#123;</span> 
        <span style="color: #003399;">String</span> source <span style="color: #339933;">=</span>
            FlexContext.<span style="color: #006633;">getServletContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getRealPath</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/WEB-INF/pdfgen/dashboard.pdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        XFAHelper helper <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XFAHelper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        helper.<span style="color: #006633;">open</span><span style="color: #009900;">&#40;</span>source<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
        helper.<span style="color: #006633;">importDataset</span><span style="color: #009900;">&#40;</span>dataset<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
        <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> bytes <span style="color: #339933;">=</span> helper.<span style="color: #006633;">saveToByteArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #003399;">String</span> uuid <span style="color: #339933;">=</span> UUIDUtils.<span style="color: #006633;">createUUID</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        FlexSession session <span style="color: #339933;">=</span> FlexContext.<span style="color: #006633;">getFlexSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        session.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span>uuid, bytes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
        helper.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
        HttpServletRequest req <span style="color: #339933;">=</span> FlexContext.<span style="color: #006633;">getHttpRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #003399;">String</span> contextRoot <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">;</span> 
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>req <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> 
            contextRoot <span style="color: #339933;">=</span> req.<span style="color: #006633;">getContextPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
        <span style="color: #003399;">String</span> r <span style="color: #339933;">=</span> contextRoot <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/dynamic-pdf?id=&quot;</span> <span style="color: #339933;">+</span> uuid <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&amp;;jsessionid=&quot;</span> <span style="color: #339933;">+</span> session.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #000000; font-weight: bold;">return</span> r<span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span> 
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice that this class references a PDF file, dashboard.pdf, that I have not yet created. This is the PDF template that will be created in Step 7.</p>
<p>I then created a new class called &#8220;com.jamesward.portablerias.PDFResourceServlet&#8221; containing the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.jamesward.portablerias</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServlet</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpSession</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PDFResourceServlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet 
<span style="color: #009900;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 8178787853519803189L<span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse res<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> 
    <span style="color: #009900;">&#123;</span> 
        doPost<span style="color: #009900;">&#40;</span>req, res<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span> 
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doPost<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse res<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> 
    <span style="color: #009900;">&#123;</span> 
        <span style="color: #003399;">String</span> id <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span>req.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>id <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> 
        <span style="color: #009900;">&#123;</span> 
            HttpSession session <span style="color: #339933;">=</span> req.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
            <span style="color: #000000; font-weight: bold;">try</span> 
            <span style="color: #009900;">&#123;</span> 
                <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> bytes <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>session.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>bytes <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> 
                <span style="color: #009900;">&#123;</span> 
                    res.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/pdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                    res.<span style="color: #006633;">setContentLength</span><span style="color: #009900;">&#40;</span>bytes.<span style="color: #006633;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                    res.<span style="color: #006633;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>bytes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                <span style="color: #009900;">&#125;</span> 
                <span style="color: #000000; font-weight: bold;">else</span> 
                <span style="color: #009900;">&#123;</span> 
                    res.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">404</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
                <span style="color: #009900;">&#125;</span> 
            <span style="color: #009900;">&#125;</span> 
            <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> t<span style="color: #009900;">&#41;</span> 
            <span style="color: #009900;">&#123;</span> 
                <span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>t.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
            <span style="color: #009900;">&#125;</span> 
        <span style="color: #009900;">&#125;</span> 
    <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This class reads the PDF byte array from the session and returns it to the user as a file. Now that Flash Player 10 is available a better approach would be to just have the PDFService return the byte array and then use the new FileReference API to initiate a file download of those bytes. Storing the PDF in the user session as this demo does is certainly not a scalable approach, and therefore it should not be used in production. Alternatively you could write the PDF files to disk in the PDFService class and then read them from disk in the PDFResourceServlet class.</p>
<p>You need one last configuration step to allow Flex to make requests to the PDFService class. Add the following lines to the WEB-INF/flex/remoting-config.xml file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;destination</span> <span style="color: #000066;">channels</span>=<span style="color: #ff0000;">&quot;my-amf&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;PDFService&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.jamesward.portablerias.PDFService<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/destination<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now your back end is set up! Start (or restart) your app server and verify that there are no errors on the console.</p>
<p><strong>Step 4: Creating a PDF with form fields to store the data</strong></p>
<p>Now you need to create a PDF template that will hold the form fields in which LiveCycle Data Services will store the data. For this step you can either just use my portable_rias_template.pdf file or you can create your own using LiveCycle Designer ES 8.2. If you choose to use mine then you can skip this step.</p>
<p>First create a new blank PDF in Acrobat and save it.  Then open that PDF in LiveCycle Designer and add a form named &#8220;PortableRIAs&#8221;. Add a page named &#8220;Page1&#8243; and then add a TextField named &#8220;Data&#8221; to the page. Then add a script called &#8220;dataScript&#8221; containing:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> xfa.<span style="color: #660066;">form</span>.<span style="color: #660066;">PortableRIAs</span>.<span style="color: #660066;">Page1</span>.<span style="color: #660066;">Data</span>.<span style="color: #660066;">rawValue</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> data<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> setData<span style="color: #009900;">&#40;</span>dataString<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    xfa.<span style="color: #660066;">form</span>.<span style="color: #660066;">PortableRIAs</span>.<span style="color: #660066;">Page1</span>.<span style="color: #660066;">Data</span>.<span style="color: #660066;">rawValue</span> <span style="color: #339933;">=</span> dataString<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The final PDF in Designer should look like:</p>
<p><img src="/wordpress/wp-content/uploads/2008/10/portable_rias-designer.jpg" title="Portable RIAs - Designer" width="800"/></p>
<p>In step 7 you will merge the Flex application into this file using Acrobat.</p>
<p><strong>Step 5: Modifying the Flex application to read data from the PDF</strong></p>
<p>When your Flex application runs in the browser you want it to get the live data from the web. When it runs in a PDF you want it to use the data inside the PDF. This allows the application to work when the user is disconnected. For bonus points I also added the ability for users to refresh the data in the PDF when they want to. To manage this I created a class called &#8220;com.jamesward.portablerias.PDFCacheManager&#8221; containing the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">jamesward</span>.<span style="color: #006600;">portablerias</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">external</span>.<span style="color: #006600;">ExternalInterface</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">xml</span>.<span style="color: #006600;">XMLDocument</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">xml</span>.<span style="color: #0066CC;">XMLNode</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;
&nbsp;
<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">core</span>.<span style="color: #006600;">Application</span>;
<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">AsyncToken</span>;
<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">FaultEvent</span>;
<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">ResultEvent</span>;
<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">http</span>.<span style="color: #006600;">HTTPService</span>;
<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #0066CC;">xml</span>.<span style="color: #006600;">SimpleXMLDecoder</span>;
<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">utils</span>.<span style="color: #006600;">ObjectUtil</span>;
&nbsp;
<span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;fault&quot;</span>, <span style="color: #0066CC;">type</span>=<span style="color: #ff0000;">&quot;mx.rpc.events.FaultEvent&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PDFCacheManager <span style="color: #0066CC;">extends</span> EventDispatcher
<span style="color: #66cc66;">&#123;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> srv:HTTPService;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> readerVersion:<span style="color: #0066CC;">Number</span>;
&nbsp;
  <span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> inPDF:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
  <span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> lastResult:<span style="color: #0066CC;">Object</span>;
&nbsp;
  <span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> lastXMLResult:<span style="color: #0066CC;">String</span>;
&nbsp;
  <span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> inBrowser:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> PDFCacheManager<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    srv = <span style="color: #000000; font-weight: bold;">new</span> HTTPService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    srv.<span style="color: #006600;">useProxy</span> = <span style="color: #000000; font-weight: bold;">false</span>;
    <span style="color: #808080; font-style: italic;">// force the result to be text</span>
    srv.<span style="color: #006600;">resultFormat</span> = HTTPService.<span style="color: #006600;">RESULT_FORMAT_TEXT</span>;
    srv.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ResultEvent.<span style="color: #006600;">RESULT</span>, handleResult<span style="color: #66cc66;">&#41;</span>;
    srv.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>FaultEvent.<span style="color: #006600;">FAULT</span>, handleFault<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #0066CC;">try</span>
    <span style="color: #66cc66;">&#123;</span>
      ExternalInterface.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;eval&quot;</span>, <span style="color: #ff0000;">&quot;function getViewerVersion() { return app.viewerVersion }&quot;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #000000; font-weight: bold;">var</span> r:<span style="color: #0066CC;">Object</span> = ExternalInterface.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;getViewerVersion&quot;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>r <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#123;</span>
        readerVersion = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#40;</span>r<span style="color: #66cc66;">&#41;</span>;
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>readerVersion <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#123;</span>
        inPDF = <span style="color: #000000; font-weight: bold;">true</span>;
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>readerVersion <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// Alert.show(&quot;You must use Adobe Acrobat 9 or Adobe Reader 9 to view this PDF&quot;);</span>
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #0066CC;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #808080; font-style: italic;">// not in a PDF</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>readerVersion <span style="color: #66cc66;">&gt;</span>= <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #808080; font-style: italic;">// setup function to get the data from the PDF</span>
      ExternalInterface.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;eval&quot;</span>, <span style="color: #ff0000;">&quot;function getData() { return xfa.form.PortableRIAs.dataScript.getData(); }&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
      <span style="color: #808080; font-style: italic;">// setup function to set the data in the PDF</span>
      ExternalInterface.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;eval&quot;</span>, <span style="color: #ff0000;">&quot;function setData(dataString) { xfa.form.PortableRIAs.dataScript.setData(dataString); }&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
      <span style="color: #808080; font-style: italic;">// see if the PDF has been opened in the browser or in Reader</span>
      ExternalInterface.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;eval&quot;</span>, <span style="color: #ff0000;">&quot;function getExternal() { return this.external; }&quot;</span><span style="color: #66cc66;">&#41;</span>;
      inBrowser = ExternalInterface.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;getExternal&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> <span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
    srv.<span style="color: #0066CC;">url</span> = <span style="color: #0066CC;">_url</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleResult<span style="color: #66cc66;">&#40;</span>event:ResultEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
    parseData<span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">result</span> as <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>inPDF<span style="color: #66cc66;">&#41;</span>
	  <span style="color: #66cc66;">&#123;</span>
	    updateCache<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> parseData<span style="color: #66cc66;">&#40;</span>dataString:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
    lastXMLResult = dataString;
&nbsp;
	  <span style="color: #000000; font-weight: bold;">var</span> xn:<span style="color: #0066CC;">XMLNode</span> = <span style="color: #0066CC;">XMLNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> XMLDocument<span style="color: #66cc66;">&#40;</span>lastXMLResult<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	  lastResult = <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SimpleXMLDecoder<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">decodeXML</span><span style="color: #66cc66;">&#40;</span>xn<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleFault<span style="color: #66cc66;">&#40;</span>event:FaultEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
    dispatchEvent<span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDataFromServer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
    srv.<span style="color: #0066CC;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDataFromCache<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> xmlDataString:<span style="color: #0066CC;">String</span> = ExternalInterface.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;getData&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	  parseData<span style="color: #66cc66;">&#40;</span>xmlDataString<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> updateCache<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
  <span style="color: #66cc66;">&#123;</span>
    ExternalInterface.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;setData&quot;</span>, lastXMLResult<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>When this class is instantiated it checks to see if the application is running inside a PDF Reader. It also checks to make sure it’s at least Reader 9. If it is running in Reader >= 9 then it sets up a few function calls that will be used to read and write data to the hidden form field. The class stores a copy of the XML text it retrieves from the server so that when the PDF is created in the browser, it doesn’t have to retrieve the data again. Also if the user updates the data while in the PDF then the local form field is updated but the user would need to save the PDF for the data to be persisted for the next time the PDF is opened.</p>
<p>To use the PDFCacheManager class simply create an instance of it and specify the id, url, and optionally a fault handler:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;portablerias:PDFCacheManager</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;pdfCacheManager&quot;</span> <span style="color: #000066;">url</span>=<span style="color: #ff0000;">&quot;http://ws.jamesward.com/data.php&quot;</span> <span style="color: #000066;">fault</span>=<span style="color: #ff0000;">&quot;mx.controls.Alert.show(event.fault.message)&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>When the Flex application has fully initialized I ask the PDFCacheManager to get the data either from within the PDF if the application is running inside Reader or from the network if the application is running in the browser:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:applicationComplete<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  if (pdfCacheManager.inPDF)
  {
    pdfCacheManager.getDataFromCache();
  }
  else
  {
    pdfCacheManager.getDataFromServer();
  }
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:applicationComplete<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>In the Dashboard you can now bind to the data in the pdfCacheManager:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:Dashboard</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">dataProvider</span>=<span style="color: #ff0000;">&quot;{pdfCacheManager.lastResult.list.month as mx.collections.ArrayCollection}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>Now your Flex application will display data whether it is running on the web or inside a PDF!</p>
<p><strong>Step 6: Adding a way for the user to generate a PDF from the Flex application</strong></p>
<p>Next you’ll want to modify the Flex application so that a user can initiate the PDF generation. To do this I created a simple ActionScript class called &#8220;com.jamesward.portablerias.CreatePDFService&#8221; which will pass the data in the Flex application to the back-end PDFService. Here is the code for that class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">jamesward</span>.<span style="color: #006600;">portablerias</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
  <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">navigateToURL</span>;
&nbsp;
  <span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">FaultEvent</span>;
  <span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">ResultEvent</span>;
  <span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">remoting</span>.<span style="color: #006600;">RemoteObject</span>;
&nbsp;
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CreatePDFService
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ro:RemoteObject;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> CreatePDFService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      ro = <span style="color: #000000; font-weight: bold;">new</span> RemoteObject<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;PDFService&quot;</span><span style="color: #66cc66;">&#41;</span>;
      ro.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ResultEvent.<span style="color: #006600;">RESULT</span>, handleResult<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> generatePDF<span style="color: #66cc66;">&#40;</span>xmlString:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">var</span> xmlData:<span style="color: #0066CC;">XML</span> = <span style="color: #66cc66;">&lt;</span>PortableRIAs<span style="color: #66cc66;">&gt;&lt;</span>Data<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#123;</span>xmlString<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&lt;/</span>Data<span style="color: #66cc66;">&gt;&lt;/</span>PortableRIAs<span style="color: #66cc66;">&gt;</span>;
&nbsp;
      ro.<span style="color: #006600;">generatePDF</span><span style="color: #66cc66;">&#40;</span>xmlData<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleResult<span style="color: #66cc66;">&#40;</span>event:ResultEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span> = event.<span style="color: #006600;">result</span> as <span style="color: #0066CC;">String</span>;
      navigateToURL<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;_blank&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>When the PDFService is called it is passed an XML block containing the XML data that the Flex application loaded from the server. Although you could use alternative serialization methods instead of XML, for simplicity this demo uses XML. Notice that the data is wrapped in the &lt;PortableRIAs&gt; and &lt;Data&gt; XML tags. These must match the PDF form name and form field name used in the PDF template.</p>
<p>When the result is returned by the server the client opens a new browser window pointing to the download servlet that initiates the PDF download.</p>
<p>To use this class in the main Flex application simply create an instance of the CreatePDFService:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;portablerias:CreatePDFService</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;createPDFService&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>Then add a button for the user to click to initiate the PDF generation:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Button</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;Create PDF&quot;</span> <span style="color: #000066;">visible</span>=<span style="color: #ff0000;">&quot;{!pdfCacheManager.inPDF}&quot;</span> <span style="color: #000066;">includeInLayout</span>=<span style="color: #ff0000;">&quot;{!pdfCacheManager.inPDF}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:click<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      createPDFService.generatePDF(pdfCacheManager.lastXMLResult);
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:click<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Notice that the data passed to the generatePDF method is pulled out of the pdfCacheManager that was created in the previous step. Also the button is only made visible when the Flex application is not running inside a PDF.</p>
<p><strong>Step 7: Merging the final Flex application into the PDF template</strong></p>
<p>Hopefully your Flex application compiles and is working in the browser. The final step is to update the PDF template and include the Flex application into it. It would be nice to automate this step somehow but at this time I’m not aware of a method for doing this. Also one thing to note is that you are using the same application for the browser and for the PDF. You can create different applications if you want to and still use much of the same code. But for this demo using the same Flex application for both mediums works fine.</p>
<p>First open the PDF template in Acrobat, then choose Tools > Multimedia > Flash Tool. Drag a box that will contain the Flex application (you’ll resize it later). Browse and select the SWF file for your Flex application. Select &#8220;Show Advanced Options&#8221; and set the &#8220;Enabled When&#8221; option to &#8220;When the page containing the content is opened&#8221;. Click OK. To resize the Flex application choose Tools > Advanced Editing > Select Object Tool. Then select the image of the Flex application and resize it. The PDF should look like the following:</p>
<p><img src="/wordpress/wp-content/uploads/2008/10/portable_rias-acrobat.jpg" title="Portable RIAs - Acrobat" width="800"/></p>
<p>Save the PDF to the file location specified in the PDFService Java class. In my case this is &#8220;WEB-INF/pdfgen/dashboard.pdf&#8221;.</p>
<p>Everything should now be ready to go! Pull up your Flex application in the browser, click the &#8220;Create PDF&#8221; button, and then you should be looking at the Flex application with its data inside of a PDF! If the PDF opened in your browser you can save it to your local file system and load it in the standalone Reader. Also make sure that clicking the &#8220;Update Data&#8221; button works to refresh the data from the server.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2008/11/05/portable-rias-flex-apps-in-pdfs/feed/</wfw:commentRss>
		<slash:comments>56</slash:comments>
		</item>
	</channel>
</rss>
