<?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>Kalen Gibbons &#187; series</title>
	<atom:link href="http://www.kalengibbons.com/blog/index.php/tag/series/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kalengibbons.com/blog</link>
	<description>The Dead Tree Blog</description>
	<lastBuildDate>Mon, 24 Oct 2011 20:43:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Learning Parsley, Part 1: Dependency Injection</title>
		<link>http://www.kalengibbons.com/blog/index.php/2009/11/learning-parsley-part-1-dependency-injection/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2009/11/learning-parsley-part-1-dependency-injection/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 04:59:01 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[Parsley]]></category>
		<category><![CDATA[series]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog/?p=373</guid>
		<description><![CDATA[I decided to take a look at SpiceFactory&#8217;s Parsely framework for Flex, and I was pretty impressed. When I started to dig into the framework I discovered that although the framework&#8217;s documentation was very good, there weren&#8217;t many community examples or tutorials for the framework. So I decided to create a short series on getting [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="SpiceFactorys Parsley Framework" src="/assets/images/parsleyCog.gif" title="SpiceFactorys Parsley Framework" width="115" height="100" align="left" />I decided to take a look at <a href="http://www.spicefactory.org/parsley/">SpiceFactory&#8217;s Parsely framework</a> for Flex, and I was pretty impressed.  When I started to dig into the framework I discovered that although the framework&#8217;s documentation was very good, there weren&#8217;t many community examples or tutorials for the framework.  So I decided to create a short series on getting started with Parsley.  The examples in the series will be fairly simple and no advanced topics will be discussed; my aim is to help developers get a jump-start on development, by providing simple examples of working code. </p>
<h3>Defining Object Dependencies</h3>
<p>We&#8217;ll begin by learning how to add dependency injection into an application.  First, we need to recognize our object dependencies.  In the sample application below, the <b>ContactList</b> view requires the <b>ContactManager</b> model object to provide its data.  In Parsley, marking that dependency as an injection point is as simple as adding the <em>[Inject]</em> metadata tag above the property declaration.  </p>
<p>Parsley can also inject properties into constructors and other methods, and you can find more information on that <a href="http://www.spicefactory.org/parsley/docs/2.1/manual/injection.php#intro">here</a>.</p>
<div class="code">
<span class="mxmlSpecialString">&lt;mx:Script&gt;</span></p>
<div class="indent">&lt;![CDATA[</p>
<p>&nbsp;</p>
<div class="indent">
<span class="asReserved">import</span> com.kalengibbons.contactsManager.model.ContactManager;</p>
<p>&nbsp;</p>
<p>[Inject]<br />
[<span class="asReserved">Bindable</span>]<br />
<span class="asReserved">public</span> <span class="asVar">var</span> contactManager:ContactManager; </div>
<p>&nbsp;</p>
<p>]]&gt;</p></div>
<p><span class="mxmlSpecialString">&lt;/mx:Script&gt;</span>
</div>
<h3>Creating the IOC Container</h3>
<p>Next, we need to tell Parsley where to find the <b>ContactManager</b> class before it can inject it into the view.  To do this, we need to add it to the IOC Container, which is responsible for wiring all our dependencies together.  The container can be written as MXML, ActionScript, XML, or a combination of the three.  For more information on using these methods you can view the documentation <a href="http://www.spicefactory.org/parsley/docs/2.1/manual/config.php#intro">here</a>.  For this example, we will look at an MXML configuration.  To do this, simply create an MXML file with mx:Object as the root tag, and add references to the objects that we will need to wire, like so:</p>
<div class="code">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br />
<span class="mxmlComponent">&lt;mx:Object</span> xmlns=&quot;<span class="mxmlString">*</span>&quot; xmlns:mx=&quot;<span class="mxmlString">http://www.adobe.com/2006/mxml</span>&quot;</p>
<div class="indent">
xmlns:model=&quot;<span class="mxmlString">com.kalengibbons.contactsManager.model.*</span>&quot;<span class="mxmlComponent">&gt;</span>
</div>
<p>&nbsp;</p>
<p><span class="mxmlComponent indent">&lt;model:ContactManager /&gt;</span></p>
<p>&nbsp;</p>
<p><span class="mxmlComponent">&lt;/mx:Object&gt;</span>
</div>
<h3>Wiring the View</h3>
<p>The framework won&#8217;t know to look in our <b>ContactList</b> view for injection points unless we tell it to.  To do this, we need to also add the view into the IOC Container.  There are two ways of doing this.  We could explicitly declare it in the container like we did in the previous step for the <b>ContactManager</b>.  But for views, Parsley allows us to dynamically wire them by simply dispatching a single configuration event like so:</p>
<div class="code">
<span class="mxmlComponent">&lt;mx:VBox</span> xmlns:mx=&quot;<span class="mxmlString">http://www.adobe.com/2006/mxml</span>&quot;<br />
<span class="indent">addedToStage=&quot;<span class="asReserved">this</span>.dispatchEvent( <span class="asReserved">new</span> Event(<span class="asString">&#8216;configureView&#8217;</span>, <span class="asReserved">true</span>) );&quot;<span class="mxmlComponent">&gt;</span></span>
</div>
<p>The <em>configureView</em> event tells Parsley that the view should be added to the IOC Container at runtime.</p>
<h3>Initializing the Framework</h3>
<p>Finally, to put it all together, we need to initialize the framework and provide Parsley with the IOC Container we created.  We want to do this as early as possible so doing this on the <em>addedToStage</em> event is common practice.  Simply call the <em>FlexContextBuilder.build()</em> method and provide the IOC Container and the root DisplayObject used for wiring (typically the application root).</p>
<div class="code">
<p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br />
    <span class="mxmlComponent">&lt;mx:Application</span> xmlns:mx=&quot;<span class="mxmlString">http://www.adobe.com/2006/mxml</span>&quot; layout=&quot;<span class="mxmlString">vertical</span>&quot;</p>
<div class="indent">
    xmlns:views=&quot;<span class="mxmlString">com.kalengibbons.contactsManager.views.*</span>&quot;<br />
    addedToStage=&quot;addedToStageHandler()&quot;<span class="mxmlComponent">&gt;</span></p>
<p>&nbsp;</p>
<p> <span class="mxmlSpecialString">&lt;mx:Script&gt;</span></p>
<div class="indent">&lt;![CDATA[</p>
<p>&nbsp;</p>
<div class="indent">
    <span class="asReserved">import</span> com.kalengibbons.contactsManager.config.ContactManagerConfig;<br />
    <span class="asReserved">import</span> org.spicefactory.parsley.flex.FlexContextBuilder;</p>
<p>&nbsp;</p>
<p>    <span class="asReserved">private</span> function addedToStageHandler():<span class="asReserved">void</span>{</p>
<div class="indent">
    <span class="asComment">// configure the IoC container</span><br />
    FlexContextBuilder.build(ContactManagerConfig, <span class="asReserved">this</span>);
    </div>
<p>    }
  </div>
<p>    ]]&gt;
    </p></div>
<p>    <span class="mxmlSpecialString">&lt;/mx:Script&gt;</span></p>
<p>&nbsp;</p>
<p>  <span class="mxmlComponent">&lt;views:ContactList width=&quot;</span><span class="mxmlString">100%</span><span class="mxmlComponent">&quot; height=&quot;</span><span class="mxmlString">100%</span><span class="mxmlComponent">&quot; /&gt;</span></p></div>
<p>&nbsp;</p>
<p>  <span class="mxmlComponent">&lt;/mx:Application&gt;</span></p>
</div>
<p>Here is a working example of the code.  The application is very simple; when you click the button it simply populates the <b>ContactModel</b> object with data, which is bound to the DataGrid.  The key is to notice how the <b>ContactModel</b> has successfully been injected into the view.</p>
<p>You can view the <a href="http://www.kalengibbons.com/assets/pages/srcview/contactsmanager/">source code here</a>.<br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_contactsManager_1550725437"
			class="flashmovie"
			width="750"
			height="500">
	<param name="movie" value="/assets/flash/contactsManager.swf" />
	<param name="bgcolor" value="#000000" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/assets/flash/contactsManager.swf"
			name="fm_contactsManager_1550725437"
			width="750"
			height="500">
		<param name="bgcolor" value="#000000" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<h3>Points to Remember</h3>
<ul>
<li>Dependency injection takes time, and your objects might not be immediately available.  In your views, even after the creationComplete event, your objects may still be null.  So what you need to do is use <em>[Init]</em> metadata to designate a function for Parsley to run after injection is complete.  Using this instead of a Flex event will ensure that your objects are all available and ready for use.
</li>
<li>Don&#8217;t forget to add the application root to the IOC Container, if necessary.  Even though you may initialize the framework in the application root, and use it as the view root of the Context, you will still need to dispatch the <em>configureView</em> event if you need dependency injection to take place in the application root.</li>
</ul>
<h3>Conclusion</h3>
<p>If you haven&#8217;t done so already, please read the <a href="http://www.spicefactory.org/parsley/docs/2.1/manual/">Parsley Documentation</a> for more information about dependency injection and the Parsley framework.  And like I stated in the intro, I&#8217;m new to Parsley myself so if anyone would like make corrections or suggest any best practices, please feel free to do so in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2009/11/learning-parsley-part-1-dependency-injection/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Custom Printing with Flex, Part 3: Printing Data</title>
		<link>http://www.kalengibbons.com/blog/index.php/2009/04/custom-printing-with-flex-part-3-printing-data/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2009/04/custom-printing-with-flex-part-3-printing-data/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 06:19:52 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Coldfusion]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[AlivePDF]]></category>
		<category><![CDATA[Custom Printing with Flex]]></category>
		<category><![CDATA[series]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog/?p=254</guid>
		<description><![CDATA[In the first article of this series, I discussed how to prevent users from printing directly from Flash&#8217;s default context menu. In the second article, I wrote about using the AlivePDF library to create PDFs from your application for printing or saving. Today I will be covering how to print application data from Flex. The [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://www.kalengibbons.com/blog/index.php/2009/02/custom-printing-with-flex-part-1-overriding-flashs-built-in-printing/">first article</a> of this series, I discussed how to prevent users from printing directly from Flash&#8217;s default context menu.  In the <a href="http://www.kalengibbons.com/blog/index.php/2009/03/custom-printing-with-flex-part-2-generating-pdfs-with-alivepdf/">second article</a>, I wrote about using the <a href="http://alivepdf.org/" target="_blank">AlivePDF</a> library to create PDFs from your application for printing or saving. Today I will be covering how to print application data from Flex.</p>
<p>The Flex framework comes with several classes to assist you in printing; however, they don&#8217;t tend to produce the greatest results.  The <a href="http://livedocs.adobe.com/flex/3/langref/mx/printing/PrintDataGrid.html" target="_blank">PrintDataGrid</a> class is probably your best option when it comes to printing data, but whenever I&#8217;ve worked with it in the past I&#8217;ve always ended up disappointed.  Even after formatting, there are always issues; like rows getting cut off and pages breaks in weird places.</p>
<p>There have been talks about AlivePDF offering a Grid class to the library, which may be interesting, but it hasn&#8217;t been released and would probably only handle DataGrid data. So the best option I&#8217;ve found for printing data is to allow a server to generate a PDF, which can then be sent back to Flex and output however you want.</p>
<p>A great benefit of this option is the ability to easily format the print results.  You can use simple HTML and CSS to customize the printout much easier than you could using ActionScript.  You can also add custom styles, letterheads, and footers to fit your needs.</p>
<p>Here is an example, you can <a target="_blank" href="http://www.kalengibbons.com/assets/pages/srcview/PrintDataExample/index.html">view the source here</a>.<br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_PrintDataExample_1698522825"
			class="flashmovie"
			width="750"
			height="500">
	<param name="movie" value="/assets/flash/PrintDataExample.swf" />
	<param name="bgcolor" value="#000000" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/assets/flash/PrintDataExample.swf"
			name="fm_PrintDataExample_1698522825"
			width="750"
			height="500">
		<param name="bgcolor" value="#000000" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>This will work with any server-side script that can generate PDF content.  In this demo I am using a ColdFusion script which simply generates an HTML table and returns it in PDF format.  You can view the ColdFusion code along with the rest of the source code <a target="_blank" href="http://www.kalengibbons.com/assets/pages/srcview/PrintDataExample/index.html">here</a>.</p>
<p>The data from the server is returned to Flex as a ByteArray.  You can take that ByteArray and output it to the user in several different ways:</p>
<h3>Using the Browser</h3>
<p>Similar to the last article in this series, you can send the results to a server-side script to either display the PDF in the browser or prompt the user to open or save the document. This time, however, the AlivePDF library is no longer necessary since the PDF binary has already been created.</p>
<div class="code">
<span class="asReserved">private</span> <span class="asFunction">function</span> generatePDF(pdfBinary:ByteArray, method:String):<span class="asReserved">void</span>{</p>
<div class="indent">
		<span class="asComment">//result comes back as binary, create a new URL request and pass it back to the server</span><br />
        <span class="asVar">var</span> header:URLRequestHeader = <span class="asReserved">new</span> URLRequestHeader(&quot;<span class="asString">Content-type</span>&quot;, &quot;<span class="asString">application/octet-stream</span>&quot;);<br />
        <span class="asVar">var</span> urlString:String = &quot;<span class="asString">http://kalengibbons.com/assets/pages/pdfCreator.cfm</span>&quot;;<br />
        <span class="asReserved">if</span>(method == &quot;<span class="asString">inline</span>&quot;)</p>
<div class="indent">
        urlString += &quot;<span class="asString">?method=inline</span>&quot;;		</div>
<p>        <span class="asReserved">else</span></p>
<div class="indent">
        urlString += &quot;<span class="asString">?method=attachment&amp;name=dataPrintSample.pdf</span>&quot;;		</div>
<p>        <span class="asVar">var</span> sendRequest:URLRequest = <span class="asReserved">new</span> URLRequest(urlString);<br />
        sendRequest.requestHeaders.push(header);<br />
        sendRequest.method = URLRequestMethod.POST;<br />
        sendRequest.data = pdfBinary;<br />
        navigateToURL(sendRequest, &quot;<span class="asString">_blank</span>&quot;);		</div>
<p>        }
</p></div>
<h3>Save locally from Flash</h3>
<p>Thanks to new capabilities in <a href="http://www.adobe.com/products/flashplayer/features/" target="_blank">Flash Player 10</a>, you can also provide users the option to save the PDF directly from Flash.  This eliminates the need for the second server-side script.</p>
<div class="code">
<span class="asReserved">private</span> <span class="asFunction">function</span> savePDF(pdfBinary:ByteArray):<span class="asReserved">void</span>{</p>
<div class="indent">
		<span class="asVar">var</span> fileRef:FileReference = <span class="asReserved">new</span> FileReference();<br />
        fileRef.save(pdfBinary, &quot;<span class="asString">yourPrintout.pdf</span>&quot;);		</div>
<p>        }
</p></div>
<p>If you plan to use the save functionality you&#8217;ll need to set the &#8220;Required Flash Player version&#8221; in your project preferences to 10.0.0 or higher or else Flex Builder will complain.  You have to remember that only users with Flash Player 10 or above will be able to use this functionality, so some type of version validation is a necessity.</p>
<p>Another caveat is that the save method must be triggered by some type of <a href="http://kb.adobe.com/selfservice/viewContent.do?externalId=kb405546" target="_blank">user interaction</a>, that&#8217;s why the sample application prompts for system access before saving.  Although the user has clicked the &#8220;Print Data&#8221; button, that event is lost when the server call is made, so we need another interaction from the user.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2009/04/custom-printing-with-flex-part-3-printing-data/feed/</wfw:commentRss>
		<slash:comments>63</slash:comments>
		</item>
		<item>
		<title>Custom Printing with Flex, Part 2: Generating PDFs with AlivePDF</title>
		<link>http://www.kalengibbons.com/blog/index.php/2009/03/custom-printing-with-flex-part-2-generating-pdfs-with-alivepdf/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2009/03/custom-printing-with-flex-part-2-generating-pdfs-with-alivepdf/#comments</comments>
		<pubDate>Sun, 01 Mar 2009 08:10:27 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Coldfusion]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[AlivePDF]]></category>
		<category><![CDATA[Custom Printing with Flex]]></category>
		<category><![CDATA[series]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog/?p=191</guid>
		<description><![CDATA[In the first article of this series, Overriding Flash’s Built-In Print Option, I discussed how to prevent users from printing directly from Flash, by replacing the default context menu. I demonstrated how poorly Flash handles printing directly, and now we will start discussing what alternatives are available to developers. In this article I will demonstrate [...]]]></description>
			<content:encoded><![CDATA[<p>In the first article of this series, <a href="http://www.kalengibbons.com/blog/index.php/2009/02/custom-printing-with-flex-part-1-overriding-flashs-built-in-printing/">Overriding Flash’s Built-In Print Option</a>, I discussed how to prevent users from printing directly from Flash, by replacing the default context menu.  I demonstrated how poorly Flash handles printing directly, and now we will start discussing what alternatives are available to developers.</p>
<p>In this article I will demonstrate how to print and save any part of your application by utilizing the <a href="http://www.alivepdf.org/" target="_blank">AlivePDF library</a>.  AlivePDF is  an ActionScript 3 open-source PDF library that easily creates PDF documents from your Flex application.  The library is pretty robust and offers much more functionality than I will be able to cover here.  In this article I want to demonstrate how simple it is to dynamically generate PDF documents using ActionScript and AlivePDF.</p>
<p>To give you a better understanding of what can be done with AlivePDF, let&#8217;s revisit one of the printing examples that was used in <a href="http://www.kalengibbons.com/blog/index.php/2009/02/custom-printing-with-flex-part-1-overriding-flashs-built-in-printing/">part 1</a> of the series.  The two images below are printouts of the The <a href="http://examples.adobe.com/flex2/inproduct/sdk/flexstore/flexstore.html" target="_blank">Flex Store</a> &#8211; one printed with Flash&#8217;s default print option and the other with AlivePDF.</p>
<table>
<tr>
<td>
<strong>Flex Store printed using Flash&#8217;s default print option</strong><br />
<img src="/assets/images/flexStore_printout.jpg" alt="Flex Store Printed from Flash" />
</td>
<td>
<strong>Flex Store printed using AlivePDF</strong><br />
<img src="/assets/images/flexStore_alivePDF.jpg" alt="Flex Store PDF created with AlivePDF" />
</td>
</tr>
</table>
<p>In addition, here is a sample application for you to play with some of AlivePDF&#8217;s options.  You can <a href="http://www.kalengibbons.com/assets/pages/srcview/AlivePDFExample/index.html" target="_blank">view the source here</a>.</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_AlivePDFExample_629512893"
			class="flashmovie"
			width="750"
			height="500">
	<param name="movie" value="/assets/flash/AlivePDFExample.swf" />
	<param name="bgcolor" value="#000000" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/assets/flash/AlivePDFExample.swf"
			name="fm_AlivePDFExample_629512893"
			width="750"
			height="500">
		<param name="bgcolor" value="#000000" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>So now that you&#8217;ve had a taste of what AlivePDF can do, lets look at how it works:</p>
<h3>Adding AlivePDF to your project</h3>
<p>First, download the library from <a href="http://www.alivepdf.org/" target="_blank">www.AlivePDF.org</a>, and then add it to your project in one of two ways.  The easiest way is to copy the AlivePDF.swc into your project&#8217;s <em>lib</em> folder.  Flex Builder should automatically detect the swc and add it to the compile settings.  The second approach is to add it manually in Flex Builder, by clicking Project >> Properties >> Flex Build Path >> Library Path >> Add SWC&#8230; and selecting the swc from the file system.</p>
<h3>Displaying PDFs in the Browser</h3>
<p>After the swc has been added to your project you can begin working with the AlivePDF library.  The sample code below could be used to generate a PDF document from any UI component in your application.  We simply turn it into an image (think of it as a screenshot) and publish it to a PDF for the user to save or print.  </p>
<p>The code is fairly simple, we start by creating a new instance of the PDF class and adding a page to it. Then we add our UIComponent to the document as an image.  Finally, we tell AlivePDF how we want to generate the PDF with the save() method.  In this example, the third argument of the method, Download.INLINE, specifies that we want the PDF to be displayed in the browser instead of prompting the user to save (that will be covered in the next section).</p>
<div class="code">
<p><span class="asReserved">import</span> mx.core.UIComponent;</p>
<p>&nbsp;</p>
<p><span class="asReserved">import</span> org.alivepdf.display.Display;<br />
        <span class="asReserved">import</span> org.alivepdf.images.ResizeMode;<br />
        <span class="asReserved">import</span> org.alivepdf.layout.Layout;<br />
        <span class="asReserved">import</span> org.alivepdf.layout.Orientation;<br />
        <span class="asReserved">import</span> org.alivepdf.layout.Size;<br />
        <span class="asReserved">import</span> org.alivepdf.layout.Unit;<br />
        <span class="asReserved">import</span> org.alivepdf.pdf.PDF;<br />
        <span class="asReserved">import</span> org.alivepdf.saving.Download;</p>
<p><span class="asReserved">import</span> org.alivepdf.saving.Method;</p>
<p>&nbsp;</p>
<p>
        <span class="asReserved">private</span> <span class="asFunction">function</span> doPrint(whatToPrint:UIComponent):<span class="asReserved">void</span>{</p>
<div class="indent">
		<span class="asVar">var</span> printPDF:PDF = <span class="asReserved">new</span> PDF( Orientation.LANDSCAPE, Unit.MM, Size.A4 );<br />
        printPDF.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE );<br />
        printPDF.addPage();<br />
        printPDF.addImage( whatToPrint, 0, 0, 0, 0, <span class="asString">&#8216;PNG&#8217;</span>, 100, 1, ResizeMode.FIT_TO_PAGE );<br />
        printPDF.save( Method.REMOTE, <span class="asString">&quot;/includes/pdfCreator.cfm&quot;</span>, Download.INLINE, <span class="asString">&quot;test.pdf&quot;</span> );    </div>
<p>    }</p>
</div>
<h3>Prompting Users to Save</h3>
<p>Now that works great and all&#8230; but what if you want to prompt the user to save or open in Adobe Reader?  Fortunately, AlivePDF makes that easy; all you have have to do is change Download.INLINE to Download.ATTACHMENT.</p>
<div class="code">
printPDF.save( Method.REMOTE, <span class="asString">&quot;/includes/pdfCreator.cfm&quot;</span>, Download.ATTACHMENT, <span class="asString">&quot;test.pdf&quot;</span> );
</div>
<h3>Using AlivePDF with ColdFusion</h3>
<p>A PHP script is provided with the download of the AlivePDF library, but if you don&#8217;t like PHP you can use any other server-side language that can generate PDF content.  For those of you who are ColdFusion fans, like myself, here is a basic example of a ColdFusion script that can be used.</p>
<div class="code">
<p><span class="cfComment">&lt;!&ndash;&ndash;&ndash; establish parameters &ndash;&ndash;&ndash;&gt;</span><br />
        <span class="cfTag">&lt;cfparam name=</span><span class="cfString">&quot;URL.method&quot;</span><span class="cfTag"> default=</span><span class="cfString">&quot;&quot;</span><span class="cfTag"> /&gt;<br />
&lt;cfparam name=</span><span class="cfString">&quot;URL.name&quot;</span><span class="cfTag"> default=</span><span class="cfString">&quot;&quot;</span><span class="cfTag"> /&gt;</span></p>
<p>&nbsp;</p>
<p>        <span class="cfComment">&lt;&ndash;&ndash;&ndash; get the content from the http data &ndash;&ndash;&ndash;&gt;</span><br />
        <span class="cfTag">&lt;cfset</span> httpContent = <span class="cfReserved">GetHttpRequestData()</span><span class="cfTag">&gt;</span><br /><br/>
    </p>
<p><span class="cfComment">&lt;!&ndash;&ndash;&ndash; make sure content was passed in &ndash;&ndash;&ndash;&gt;</span><br />
        <span class="cfTag">&lt;cfif</span> <span class="cfReserved">len(</span>httpContent.content<span class="cfReserved">)</span> <span class="cfReserved">gt</span> <span class="cfNumber">0</span><span class="cfTag">&gt;</span></p>
<div class="indent">
			<span class="cfComment">&lt;!&ndash;&ndash;&ndash; write the content to local pdf &ndash;&ndash;&ndash;&gt;</span><br />
<span class="cfTag">&lt;cfheader name=</span><span class="cfString">&quot;Content-Disposition&quot;</span><span class="cfTag"> value=</span><span class="cfString">&quot;#URL.method#; filename=#URL.name#&quot;</span>&nbsp;<span class="cfTag">/&gt;</span><br />
			<span class="cfTag">&lt;cfcontent type=</span><span class="cfString">&quot;application/pdf&quot;</span> <span class="cfTag">variable=</span><span class="cfString">&quot;#httpContent.content#&quot;</span><span class="cfTag">&gt;</span></div>
<p>	    <span class="cfTag">&lt;cfelse&gt;</span></p>
<div class="indent">
		<span class="cfComment">&lt;!&ndash;&ndash;&ndash; redirect to home page &ndash;&ndash;&ndash;&gt;</span><br />
		<span class="cfTag">&lt;cflocation url=</span><span class="cfString">&quot;/&quot;</span><span class="cfTag"> /&gt;</span></div>
<p>	<span class="cfTag">&lt;/cfif&gt;</span> </p>
</div>
<p>AlivePDF has a wide range of capabilities and we&#8217;ve barely scratched the surface here.  I encourage you to take a good look at the library and utilize it to its full potential.</p>
<p>In the next article in this serious we will discuss how to print data from Flex.  See you soon!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2009/03/custom-printing-with-flex-part-2-generating-pdfs-with-alivepdf/feed/</wfw:commentRss>
		<slash:comments>96</slash:comments>
		</item>
		<item>
		<title>Custom Printing with Flex, Part 1: Overriding Flash&#8217;s Built-In Print Option</title>
		<link>http://www.kalengibbons.com/blog/index.php/2009/02/custom-printing-with-flex-part-1-overriding-flashs-built-in-printing/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2009/02/custom-printing-with-flex-part-1-overriding-flashs-built-in-printing/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 21:27:19 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[contextMenu]]></category>
		<category><![CDATA[contextMenuItem]]></category>
		<category><![CDATA[Custom Printing with Flex]]></category>
		<category><![CDATA[series]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog/?p=145</guid>
		<description><![CDATA[If you&#8217;re like me and you&#8217;ve spent any time working with printing in Flex, you&#8217;ve probably realized that Flash&#8217;s built-in printing capabilities leave a lot to be desired. So I&#8217;ve decided to create a short series of articles on how to do custom printing in Flex. This article will be the first in the series [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re like me and you&#8217;ve spent any time working with printing in Flex, you&#8217;ve probably realized that Flash&#8217;s built-in printing capabilities leave a lot to be desired.  So I&#8217;ve decided to create a short series of articles on how to do custom printing in Flex.  This article will be the first in the series and will discuss how to prevent users from printing directly from the context menu.</p>
<div style="text-align: center">
<img alt="Default Flash Context Menu" src="/assets/images/FlashContextMenu.gif" title="Default Flash Context Menu" width="250" height="124" />
</div>
<p>By default, when users right-click on a SWF file, they receive the default context menu, which provides them several options, and one of which is the ability to print.  However, in my experience, using this option for printing returns horrible results.  Below are two examples of this, the first is the <a href=" http://examples.adobe.com/flex2/inproduct/sdk/flexstore/flexstore.html" target="_blank">The Flex Store</a>, a sample application from Adobe, and the second is the <a href="http://mediastore.verizonwireless.com/" target="_blank">Verizon Media Store</a>.  On the left is a screenshot of each and on the right is a capture of the print results that were produced by using the default print option.</p>
<table>
<tr>
<td>
<strong>The Flex Store application:</strong><br />
<img alt="Flex Store Screenshot" src="/assets/images/flexstore_screenshot.jpg" title="Flex Store Screenshot" width="365" height="216" />
</td>
<td>
<strong>Printing the Flex Store:</strong><br />
<img alt="Flex Store Printout" src="/assets/images/flexstore_printout.jpg" title="Flex Store Printout" width="365" height="216" align="center" />
</td>
</tr>
<tr>
<td>
<strong>The Verizon Media Store:</strong><br />
<img alt="Verizon Media Store Screenshot" src="/assets/images/verizonMediaStore_screenshot.jpg" title="Verizone Media Store Screenshot" width="365" height="216" />
</td>
<td>
<strong>Printing the Verizon Media Store:</strong><br />
<img alt="Verizon Media Store Printout" src="/assets/images/verizonMediaStore_printout.jpg" title="Flex Store Printout" width="365" height="216" />
</td>
</tr>
</table>
<p>As you can see, the results were less than satisfactory.  I would consider this unacceptable for my applications.  So, the question arises: <strong>&#8220;How do you prevent users from printing from the context menu?&#8221;</strong></p>
<p>The answer is simple &#8211; override Flash&#8217;s built-in context menu with your own.  This is how you do it:<br/><br/></p>
<div class="code">
<span class="asReserved">private</span> <span class="asFunction">function</span> setContextMenu():<span class="asReserved">void</span>{</p>
<div class="indent">
<span class="asComment">//create the new contextMenu </span><br />
<span class="asVar">var</span> customContextMenu:ContextMenu = <span class="asReserved">new</span> ContextMenu();<br />
<span class="asComment">//disable the default options</span><br />
customContextMenu.hideBuiltInItems();</p>
<p><span class="asComment">//create your own print option, and add it to the new contextMenu</span><br />
<span class="asVar">var</span> printOption:ContextMenuItem = <span class="asReserved">new</span> ContextMenuItem(<span class="asString">&quot;Print Me&quot;</span>);<br />
printOption.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, doPrint);<br />
customContextMenu.customItems.push(printOption);</p>
<p><span class="asComment">//set the newly created contextMenu for this application</span><br />
<span class="asReserved">this</span>.contextMenu = customContextMenu;</div>
<p>}<br />
<br />
<span class="asReserved">private</span> <span class="asFunction">function</span> doPrint(event:ContextMenuEvent):<span class="asReserved">void</span>{<br />
<span class="asComment indent">//now print this application</span><br />
}
</div>
<p>Okay, so let&#8217;s review the code.  The first thing you need to do is to create a new ContextMenu and then call its <strong>hideBuiltInItems</strong> menthod to disable the built-in options (forwardAndBack, loop, play, print, quality, rewind, save, and zoom).  This hides the print option and prevents the user from clicking it.  Next you add our own menu item to replace the default print option.  If you don&#8217;t want to do this, because you are going to provide your own print button on the application, you can skip ahead to the last line of code.</p>
<p>To add our own menu option, just create a new <strong>ContextMenuItem</strong> object and give it a label. Then add an event listener to catch when the user clicks on this option (in this example we have specified <em>doPrint</em> as our listener function; we will be working with that method in the proceeding articles of this series).  Finally, add our new menu item to the customItems array, and set our new menu as the contextMenu for the application.</p>
<p>You can see the results below. Although nothing spectacular has taken place here, we&#8217;ve disabled Flash&#8217;s built-in print capabilities and will be routing all print requests through our own function.</p>
<div style="text-align: center">
<img alt="Default Flash Context Menu" src="/assets/images/CustomContextMenu.gif" title="Custom Context Menu" width="250" height="132" />
</div>
<p><strong>Gotcha:</strong><br/><br />
When creating CustomMenuItems, the labels you provide must be different than those provided by the default items.  For example, you cannot use &#8220;Print&#8221; as your label or the application will ignore the option.  However, you can use variations, such as &#8220;Print This&#8221; or &#8220;Print Page.&#8221;</li>
</p>
<p><br/>In the next article we will discuss how to generate PDF documents for more functional printing and saving.  For more information of the ContextMenu, check out the <a href="http://livedocs.adobe.com/flex/3/langref/flash/ui/ContextMenu.html" target="_blank">Livedocs</a>.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2009/02/custom-printing-with-flex-part-1-overriding-flashs-built-in-printing/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

