原文:http://flexpearls.blogspot.com/2007/05/flex-components-to-pdf.html
Printing Flex components or converting them to PDF is very easy. Here is a sample code which shows how to print a column chart. If you want to generate a PDF you need to have ADOBE PDF printer(安装adobe pdf printer) driver installed on the machine. In the print dialog instead choosing a printer you can choose ADOBE PDF printer listed and you get a PDF as output. Sunil has more info about printing charts with data tips here. More about Flex Printing can be found here too.
chartToPrint.mxml
关于只能打印出一部分图片,有人说:
The reason why it was just printing a part of the graph is because, it seems the resizing used to take some time. Hence, the chart wasn't resized when it went to the printer. Even though the size of the chart was changed before fpj.addObject(), as in your example. May be because the canvas is the container which is being resized and it has two charts and couple of text boxes. I got around the problem by adding an event hadler for resize on canvas. When this occurs I do a addObject and send. So, printing works fine now.
Printing Flex components or converting them to PDF is very easy. Here is a sample code which shows how to print a column chart. If you want to generate a PDF you need to have ADOBE PDF printer(安装adobe pdf printer) driver installed on the machine. In the print dialog instead choosing a printer you can choose ADOBE PDF printer listed and you get a PDF as output. Sunil has more info about printing charts with data tips here. More about Flex Printing can be found here too.
chartToPrint.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
- <mx:Script>
- <![CDATA[
- import mx.printing.FlexPrintJobScaleType;
- import mx.printing.FlexPrintJob;
- private function printChart():void
- {
- var fpj:FlexPrintJob = new FlexPrintJob();
- if (fpj.start())
- {
- fpj.addObject(ChartContainer,FlexPrintJobScaleType.MATCH_WIDTH);
- fpj.send();
- }
- }
- private var chartData:Array = [
- { bugCount:20, date:"05/20/07" },
- { bugCount:14, date:"05/21/07" },
- { bugCount:17, date:"05/22/07" },
- { bugCount:10, date:"05/23/07" },
- { bugCount:2, date:"05/24/07" }
- ];
- ]]>
- </mx:Script>
- <mx:Canvas id="ChartContainer" backgroundColor="white" width="433" height="431">
- <mx:ColumnChart id="myChart" x="31" y="10" width="353" height="357" dataProvider="{chartData}">
- <mx:series>
- <mx:ColumnSeries yField="bugCount" />
- </mx:series>
- <mx:horizontalAxis>
- <mx:CategoryAxis categoryField="date" />
- </mx:horizontalAxis>
- </mx:ColumnChart>
- </mx:Canvas>
- <mx:Button label="Print" click="printChart()" x="174" y="399"/>
- </mx:Application>
关于只能打印出一部分图片,有人说:
The reason why it was just printing a part of the graph is because, it seems the resizing used to take some time. Hence, the chart wasn't resized when it went to the printer. Even though the size of the chart was changed before fpj.addObject(), as in your example. May be because the canvas is the container which is being resized and it has two charts and couple of text boxes. I got around the problem by adding an event hadler for resize on canvas. When this occurs I do a addObject and send. So, printing works fine now.