ACPrintManager

ACPrintManager is browser printing manager for flash content powered by AnyChart.Com team. It is small JavaScript and ActionScript library.

ACPrintManager is distributed under LGPL license version 3.
See http://www.gnu.org/licenses/lgpl.html

Current version: 0.1

Contents

Why do I need it?

ACPrintManager allows you:

  • to prevent any problems with printing from Firefox (usually Firefox doesn’t display flash content during html page printing).
  • to print properly flash content with sizes in percents in Internet Explorer (usually Internet Explorer scales flash content and ignores settings of stage.scale and stage.align).
  • to set view of flash content when you print from browser (like html css media=”print”).

How does it work?

The problem with printing from Firefox we have solved using feature of this browser - adding image directly to page instead of link to it. See http://en.wikipedia.org/wiki/Data:_URI_scheme#HTML

The issue with percentage sizes in Internet Explorer we have solved using unique events of Internet Explorer: onbeforeprint and onafterprint. When onbeforeprint appears we change percent sizes to pixels by getting it from flash using ExternalInterface.

The main idea of setting view for printing is change the flash content to image in Firefox and redraw the flash content in Internet Explorer when onbeforeprint appears.

Download

http://anychart.com/tips_and_tricks/acprintmanager/ACPrintManager.zip

How can I use it?

We need to go through two simple step to use it:.

  • Add the javascript library into the page
  • Set the content to be printed in a browser

JavaScript library is located in src/js/ folder of the install package. To add it just write the following code in the top of the page
<script src="src/ACPrintManager.js"
type="text/javascript"
language="javascript"></script>

There are some more difficulties with ActionSctript. Library is located in the folder src/as/.
To use it you can copy content of that folder or simply add paths to the folder to classpath:
See Adding classpath for Flash 9 file
Click advanced and set path to the folder with sources for Flex/FDT project.

After adding ActionScript library to your project just do the following things:

Step 1: import and initialize the library

import com.anychart.utils.PrintManager;
...
PrintManager.init(stage);

Warning: You should initialize the library only once!

Step 2: set content to be printed in a browser

For example:
//content for printing
var printableShape:Shape = new Shape();
printableShape.graphics.beginFill(0xAAAAAA, 1);
printableShape.graphics.drawRect(10, 10, 500, 500);
//save printable content
var data:BitmapData = new BitmapData(510, 510, false, 0xFFFFFFFF);
data.draw(printableShape);
PrintManager.setPrintableContent(data);

API Reference

ACPrintManager ActionScript API Reference is located in the api folder of the install package.

Samples

1. Flash 9 sample
Flash 9 sample is located in the samples/flash9 folder of the install package.
2. Flex 3 sample
Flex 3 sample is located in the samples/flex folder of the install package.

Integragion

How to use ACPrintManager with SWFObject

Just edit code of your swf files and add javascript import as it was described above.

FAQ

Q: When should I run setPrintableContent?
A: Every time when you need to refresh the content to be displayed on printed page.

Q: What version of Flash player does this solution work with?
A: 9 and higher

Q: How to set current content of the whole scene for printing?
A: You can add the following code in the main class (root)

var data:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xFFFFFFFF);
data.draw(this);
PrintManager.setPrintableContent(data);

it means you set content of the root sprite for printing and set sizes equals to scene sizes.

Q: Why does ‘t printing work in Firefox using flash embed natively generated by Flash 9.
A: Flash 9 generates file AC_RunActiveContent.js without id attribute for embed tag. Corrected AC_RunActiveContent.js is located on the folder fixes/flash9/AC_RunActiveContent.js