
I’m having enormous hassle printing a dynamic movieClip from Flash. Basically I’m trying to develop my Practice Paper application further (more details later). This tool allows a student to be given a question and they type their answer. When complete, they press a button and are shown a model answer. For the whole thing to be more useful, the student should also be able to press a button and print out their work.
The whole thing works well apart from the printing. OK, actually the whole thing has been a right pig (apologies to pigs) to setup and put together due to immense difficulties getting Flash to process all the rich-text fields. Anyway, that is behind me now. What I’m currently stuck with is how to get the PrintJob class to print more than one page effectively.
If I want to print just a single page - i.e. if the student has a short answer, meaning the total text doesn’t go over a full page - then it works. However, I’ve been stumped for hours trying to figure out a workable solution.
Through literally hours of internet searching and experimenting I’ve found many nearly solutions, but not one that really works. What I hope to do with this post is actually start out not knowing the solution and end up knowing it.
Currently, the best solution I’ve found so far is at WebdotDev.com - here they lead you through the standard PrintJob guidance (which I’ve read about far too much of recently) but then - crucially - move onto more helpful ground. This final page produces a solution that does, quite literally extend a text-field across multiple pages. So why aren’t I delighted? Unfortunately, so far all this does is maximise the space available and prints text across the entire page and can also cut text off the bottom. However, this is nearly there - so I hope a little more experimentation might find a solution.
I’ll update this further later on - although if you are reading this and have any ideas, please let me have them.
12 responses so far ↓
1 Paulius Uza // Dec 30, 2006 at 4:58 pm
Hi,
What I would do is:
- Make a bitmap object
- Draw a textfield into the bitmap object
- Print it as an image
Hope that helps
2 Andrew Field // Dec 30, 2006 at 7:26 pm
Thanks - I’ll give that a try, although does it help solve the issue of printing it over many pages? I’ll see what happens…
3 Andrew Field // Dec 30, 2006 at 10:45 pm
I’ve been able to make some more progress now. I experimented with Paulius’ suggestion and it is certainly something I could do, although I found an idea on the Macromedia Livedocs which also seemed to fit the bill. In the end I’ve cobbled together a solution that is based around the code there - when I’ve got it all working I’ll post the code here.
Proof is shown in the image below how a dynamic variable was sent to the dynamic textbox and then successfully printed on more than one page. OK, hardly the most awe inspiring thing ever - but it shows that what I’m trying to do is possible.
The fiddling around now is to make sure a more complex ‘rich-text html’ variable is successfully printed - and also making sure that the page actually prints out clearly enough.
4 Andrew Field // Dec 30, 2006 at 11:53 pm
Or maybe not
Unfortunately when I’ve tried to make the code work in my actual application it fails to display text correctly. It repositions the margins correctly for the first page, but then scrolls the page down too much when displaying the second page - consequently it triggers further problems on later pages.
Damn - back to the drawing board.
5 Andrew Field // Dec 31, 2006 at 9:29 am
Unfortunately, I’ve now found a rather telling paragraph in the excellent Community MX tutorial on printing from Flash:
Oh dear
6 Paulius Uza // Jan 4, 2007 at 3:25 pm
Sorry to hear that. You might try it another way:
- Lets presume that your text inside Flash is formated as a HTML
- Instead of using integrated print functions in Flash you send the text to your browser using Javascript. (Flash and Javascript integration kit - http://osflash.org/doku.php?id=flashjs)
- In javascript - you populate a hidden div with the text you received from Flash
- You launch a print function from javascript - window.print();
To make this method work you will need to have 2 separate css files for “screen” and “print”.
In CSS for screen you must hide the div that will be populated with text from flash.
In CSS for print you must show the print div, and hide the flash content.
7 Paulius Uza // Jan 4, 2007 at 3:41 pm
Oh and the best thing in this method is that you can modify the looks of your print output using CSS, which is easy and flexible.
If you have hard time formating the text in flash, send paragraph data to javascript as an Array, and format it there, right before printing.
8 Paulius Uza // Jan 4, 2007 at 4:34 pm
And if you don’t need fancy formatting options - check out this component that I found on FlashDen, it seems to produce pretty output from inside Flash.
http://www.flashden.net/item/dynamic_print-component/486
9 Andrew Field // Jan 4, 2007 at 10:54 pm
Thanks very much for these excellent suggestions. For the finished application I did actually use a Javascript solution. You can experiment with it here: http://www.reviseict.co.uk/as/revision/practice_ict1.shtml
After some long and extensive messing about I decided to ditch directly printing from Flash and move to getting Flash to send the data directly to the browser and then using JavaScript to construct an entirely new pop-up window to act as the printout.
I managed to get it working but to my immense frustration (yep, even more!) I’ve found it doesn’t work in Internet Explorer. I’ll post up the code I’ve been using here when I have a little more time, but it was essentially what you’ve suggested in post 6 above Paulius.
However, once I used my rich-formatted HTML style text outside of Flash I discovered that it isn’t true HTML. Thus after all the hard work to transfer the data, I then had to introduce a process that would strip some badly formed parts of the html so it would actually work. For instance, font sizes created in Flash ‘html’ didn’t transfer correctly to standard html.
The last link you provided is incredibly interesting because that component seems to offer the ability to correctly parse text across multiple pages - which was the initial issue that I was facing.
I’ll purchase that component anyway, but I suspect that this badly formed html that Flash produces might not be compatible. Unfortunately it is quite important that I retain the formatting that my application produces as it is a tool to allow students and teachers to make notes and ideas by formatting the text in the way they feel most comfortable with. I think I’ve learned the hard way that Flash simply doesn’t allow me to print multiple pages of rich-text in a reliable way. I thus did find a solution based on using Javascript, but as you’ll see when I upload the code it is very much a hack than an elegant solution. It is a combination of problems really - the poorly formatted html that the rich-text entry interface produces (this is an early version of the Flashloaded component) and then Flash’s currently inherent issues in handling printed text.
If you view my example version via a decent browser such as Firefox you’ll see that it does work.
10 Paulius Uza // Jan 5, 2007 at 12:02 am
I’ve checked your application in depth and codewise - it is nice and seems really useful, but the technical implementation is far from the model I suggested.
Let me explain:
As you said - Flash Player formats HTML differently than our internet browsers do.
What you are doing in your App - is sending a pre rendered text output from flash to javascript. You build an output string by adding flash-formated HTML to the following 3 variables:
_root.question
_root.myAnswer
_root.answerCorrect
Then you send the text via simple URL call - thats why it is not working in IE.
I could see from the Javascript code that it is delivered in unescape(transferText) then decoded, re-encoded and printed out. Environment like that is perfect for code-bugs.
How it should be done:
- Questions inside the application should be stored as strings (without formating)
- Formating and COLOURS should be added “on-demand” not only when printing, but also when displaying information inside the App. For Ex you could build a formated question by taking a string from a simple question array like this:
QuestionTextField.htmlText = 'QUESTION:' + _root.Question[4] + '';
- Flash Javascript communication should be handled by Flash-Javascript that can pass Objects and Arrays - not just HTML encoded strings (doublecheck this link http://www.osflash.org/flashjs)
- Then in Javascript you could add appropriate HTML and CSS formatting in a standards compliant way and call window.print()
11 Andrew Field // Jan 8, 2007 at 9:13 pm
Thanks for these additional suggestions (and apologies for taking so long to approve them). I do appreciate that my methods were far apart from your suggestions. I’d come up with my method even before reading yours! I was just commenting above on how I had moved towards a Javascript solution, which was along the lines that you’d proposed.
Your ideas would be perfect if my application worked differently. Unfortunately as I’m using a pre-built component (as mentioned above, the Rich Text interface from Flashloaded.com) I cannot control or record the formatting. I can either output non-formatted or fully formatted. Thus I cannot store questions / answers in a non formatted way, nor can I add specific formatting when required.
In fact, my difficulties with this are compounded for further reasons too. My application is actually produced via Flash-based desktop program. The program is where the questions and model answers are created, this data is then passed to generate a new custom .swf file which is the application shown as the example. Thus if I was programming the question data myself, it would be possible to edit the html, but as it is being ‘generated’ it gets more complex.
Thus I am restricted as the generator program I’ve put together creates a custom .swf together with the custom .html file ‘on the fly’. This is why I’m passing an html encoded string.
Finally, I unescape the text so foreign accented characters are correctly shown. If I don’t foreign accents (as well as things like +) don’t appear in the finished application. Due to the huge amount of code Flash-html produces I found the only way to process everything was to use an html string which I could pass to Flash via javascript.
I hope this goes a little way to explaining what my issues were.
Currently, my solution works in Firefox, but fails to in Internet Explorer 7 due to the inherent pop-up blocking. I think my best course of action is to pursue an alternative pop-up that will allow me to generate the javascript window on the fly.
This has all become a bit too complex, which - to borrow a phrase from Paulius, is an environment perfect for code-bugs!
12 Paulius Uza // Jan 8, 2007 at 9:47 pm
Thank you for explanations.
I hope that our discussion will save some time for people looking into printing via Flash.