Archive for the ‘Media’ Category

For Flash

I recently had to get into email attachments using Flash. So I found the nice alivePDF library, with which one can create PDFs in Flash and Flex. I searched for tutorials and found this. As you can see in this example one can download a PDF file by opening a new browser page. I wanted to convert this to attach the PDF to an email. That was not difficult, although I had to struggle a bit with the PHP file. However, again the problem was that a new page was opened, which I did not like. My goal was to have everything done within the Flex or Flash file as shown below. No email will be sent in this movie to avoid misuse. However, enter an email in the text input and press the save button. You will see that no new window is opened but a message appears.
To create this movie I followed the example for the Model View Controller for Flash.

What I have done is I have slightly modifed the PDF.as file and created files in the Model-View-Controller style. You need to download the alivePDF library. Open the PDF.as file in the PDF folder and add the following lines where packages are imported.

Now we need to modify the save function. I added another parameter for the email and made it null. This does not affect any functionality of other projects, if you have those. Then I initiated a new class, the Mediator class, which serves as a Viewer class, since we cannot use the actual viewer object, which you see in the movie, for this purpose. The modification is shown below. Also we use the Base64 encoded bytearray, since we use that for the attachment later.

I am not showing all the classes. You need to be familiar with the Model-View-Controller pattern. However, if you are not, then the only class you need to take care of and modify for your purpose is the SavePDFView.as clas, which is the viewer and the SavePDFMain.as class, which is the Document class of the movie. All other classes don’t need to be modified. Some of the labels and urls used in the movie are written in the xml file savepdf.xml and can be changed there.

For Flex

For Flex we can use the same classes except we need to create a MXML file of course and we need to change the Flash viewer class. The MXML file is shown here. It communicates with the former Flash viewer file, which we also need to modify as shown here. All other classes need not be modified.

We need to look at the php file. I found the script part for the attachment from various sources from the internet and modified them for this script. The first part of the script is the important part. There are the parameters from the Flash movie. As you may remember, the buffer, the actual pdf content, is already Base64 encoded. We create a temporary file and write the contents of the buffer variable into the file. At a later point we delete the file using the unlink() function.

You can download all files here. You can freely modify these files and use or distribute them as long as it is complient with the rules for the alivePDF library. Don’t forget to add the alivepdf library with the modified PDF.as file where an empty org folder is located.