Posted in Flex, ColdFusion | Posted on 07-13-2009 | 6,215 views
One of the cool new features of ColdFusion 9 is CFaaS, or ColdFusion as a Service. I'll be honest, I hate the abbreviation. It makes me think of a disease. But - I'll get over it since the feature itself is so impressive. During the ColdFusion Meetup (recording here), ColdFusion Evangelist Terry Ryan demonstrated using CFDOCUMENT remotely via Flex. In his demo, he enters some text, clicks a button, and gets the URL to download a PDF. This reminded me that many moons ago, earlier in the ColdFusion 9 beta, I had created a simple demo called DeadBike. (I'll buy you a beer at CFUNITED if you can guess why.) Here is a quick video showing it in action (clicking will take you to a full screen view, so I'd recommend 'open in new tab'):
So the code behind this is very similar to what Terry showed. Note that I've been lazy and hard coded all the security information within Flex.
2<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:cf="coldfusion.service.mxml.*" layout="absolute" viewSourceURL="srcview/index.html">
3
4<cf:Config id="conf" cfServer="127.0.0.1" cfPort="8502" serviceUserName="service" servicePassword="service" />
5
6<cf:Document id="doctestnow" action="generate" format="pdf" result="handleResult(event)" fault="handleError(event)" />
7
8<mx:Script>
9<![CDATA[
10import mx.rpc.events.ResultEvent;
11
12public var res:String = new String();
13
14private function renderSourceAsPDF():void {
15 var source:String = sourceText.text;
16 if(source.length == 0) return
17 doctestnow.content = source;
18 doctestnow.execute();
19}
20
21private function handleResult(event:ResultEvent):void {
22 res=event.result.toString();
23 pdfviewer.location=res;
24}
25
26private function handleError(event:Event):void {
27 mx.controls.Alert.show(event.toString());
28}
29
30]]>
31</mx:Script>
32
33<mx:HDividedBox width="100%" height="100%">
34
35 <!-- source side -->
36 <mx:VBox width="50%" height="100%">
37
38 <mx:Panel title="Source" width="100%" height="100%">
39 <mx:TextArea id="sourceText" width="100%" height="100%" />
40 </mx:Panel>
41
42 <mx:HBox horizontalAlign="right" width="100%">
43 <mx:Button label="Render PDF" click="renderSourceAsPDF()" />
44 </mx:HBox>
45
46 </mx:VBox>
47
48 <!-- render side -->
49 <mx:Panel title="PDF" width="50%" height="100%">
50
51 <mx:HTML id="pdfviewer" width="100%" height="100%" />
52
53 </mx:Panel>
54
55</mx:HDividedBox>
56
57</mx:WindowedApplication>
Let's focus on the interesting bits first. You can find the connection/service information here:
2
3<cf:Document id="doctestnow" action="generate" format="pdf" result="handleResult(event)" fault="handleError(event)" />
CFaaS has two special security requirements. First, you must go to User Management and specifically create a user and then give them access to particular services. In this case I gave the user, service, access to the Document service. Secondly, you have to specify which IP addresses are allowed to use the service. Now - I have to say - this is a bit troubling. I get the idea behind it - but it's going to be a problem for an intranet. I can't imagine you would need to enter each and every single IP address of your corporation by hand. But - that seems to be the case for right now. Hopefully that will change before release.
The code handling rendering is about as simple as it gets:
2 var source:String = sourceText.text;
3 if(source.length == 0) return
4 doctestnow.content = source;
5 doctestnow.execute();
6}
Essentially - if I ttyped something, execute the CF service. My services result handler then simply uses the HTML component to render the PDF.
2 res=event.result.toString();
3 pdfviewer.location=res;
4}
Note that I had gotten a URL back, like Terry's demo, but I went ahead and loaded the result in the component.
I was going to package up the demo, but it's usability by the public is very limited. I'd recommend simply copying my code into a new Flex Builder project. Remember to include the cfservices.swc file with your project!



(lot's of 'to's there, apologies, but that's the way it came out!)
Just thought you might like to know.
[Add Comment] [Subscribe to Comments]