Flash AS3: PDF attachment with email

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.

/***********************************************************
* Classes need to be imported for saving PDF as attachment.
*/
	import com.flashscriptMVC.AbstractClass;
	import com.flashscriptMVC.IModel;
	import com.flashscriptMVC.Model;
	import com.flashscriptMVC.Controller;
	import com.flashscriptMVC.Mediator;
	
/*********************************************************/

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.

/****************************************
* The save function is altered for attaching a pdf to an email. 
* The function has one more parameter.
*****************************************/
public function save ( method:String, url:String='', 
downloadMethod:String='inline', fileName:String='output.pdf', 
frame:String="_blank", myEmail:String = null ):*
{
	dispatcher.dispatchEvent( new ProcessingEvent 
	( ProcessingEvent.STARTED ) );
	var started:Number = getTimer();
	finish();
	dispatcher.dispatchEvent ( new ProcessingEvent 
	( ProcessingEvent.COMPLETE, 
	getTimer() - started ) );
	buffer.position = 0;
	var output:* = null;
			
	switch (method)
	{
		case Method.LOCAL : 
		output = buffer;
		break;	
				
		case Method.BASE_64 : 
		output = Base64.encode64 ( buffer );
					
/*************************************************************************
* To send the PDF file as attachment we use the BASE_64 method and send the
* already converted data to the server. We use the Mediator class as a 
* Pseudo viewer class, whic communicates then with the controller and model 
* class and in the end with the SavePDFView object. Like a regular viewer
* we add eventlisteners.
*/

var model:IModel = new Model();
var controller:IControlHandler = new Controller(model);
if(downloadMethod == "attachment")
{
	if(myEmail == null)
	{
		trace("Don't forget to unnullify the myEmail parameter!")
		return;
	}
	var c:Mediator = new Mediator (model,controller);
	c.pdfHandler (url, output, fileName, myEmail);
	model.addEventListener ("serverResponse", c.update);
	model.addEventListener ("erMSG", c.update);
						
/******************************************************************************/
}
break;

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.

<?php

$buffer = $_POST['buffer'];
$name = $_POST['name'];
$email = $_POST['email'];
$tempname = tempnam("/tmp", "PDF");

if ( isset ( $buffer )) 
{
	$files = fopen( $tempname, 'w' );
	$data = fwrite( $files, $buffer );
	fclose( $files );
}  
else 
{
	fail("An error occured encoding.");
}

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.

Leave a Reply

You must be logged in to post a comment.