Saving PDF stream using FDPI/AS3

Hey all,

I’ve been trying to figure this out for hours and wanted to see if the community could help. I’m generating a PDF using FPDF/FPDI/PHP in combination with a Flash-based PDF editor. I’m able to pass in POST values from Flash to FPDI for the dynamic PDF values without issue using code URLRequest/URLRequestLoader, and I get a response back in my onComplete handler. All is well.

The problem is, I want to deliver this PDF stream into an attachment and prompt the user to download this dynamic PDF. I can see the event.target.data stream in my onComplete handler, but can’t figure out how to send this back to the user. Here’s what I’ve tried:

1.) Setting my request.contentType to both “application/octet-stream” and “application/pdf”.

2.) Adding URLRequestHeader instances to the request object, like this one:

var header:URLRequestHeader = new URLRequestHeader("Content-Disposition", "attachment='doc.pdf'");

3.) Using navigateToURL instead and bypassing the URLLoader object altogether. This works, but I’d like to be able to capture things like the HTTP status, codes, and other info. Plus, the user will need to create/download several files in succession and I’d like to control that flow. This ideal way would be to do this without navigateToURL.

Actionscript:

private function onClick(evt:MouseEvent):void {
            //set up our loader.
            var loader:URLLoader = new URLLoader(request);
            var request:URLRequest = new URLRequest("http://myurl.com/createPDF.php");
            var variables:URLVariables = new URLVariables();
            
            //Set up events.
            this.configureListeners(loader);
            
            //Define vars
            variables.myText = "This is dynamic text";
        
            //Set up request obj
            request.method = URLRequestMethod.POST;
            request.data = variables;
            
            request.contentType = "application/pdf";
            var header:URLRequestHeader = new URLRequestHeader("Content-Disposition", "attachment='doc.pdf'");
            request.requestHeaders.push(header);
                            
            loader.dataFormat = URLLoaderDataFormat.VARIABLES;
            loader.load(request);
            status_txt.text += "starting...";

///...down in onComplete handler...

private function completeHandler(event:Event):void {
//At this point the user should see an open/save dialog box for the generated PDF.
            var loader:URLLoader = URLLoader(event.target);
            status_txt.text += "
completeHandler: " + loader.data;
        }
            
        }

PHP:

<?php

#creates PDF from Flash

require('fpdf.php');
require('fpdi.php');

$pdf =& new FPDI();

$pagecount = $pdf->setSourceFile('FP_DM_01_Empty.pdf');
$tplidx = $pdf->importPage(1, '/MediaBox');

$pdf->addPage();
$pdf->useTemplate($tplidx, 10, 10, 90);
$pdf->SetFont('Arial','B',30);
$pdf->Cell(40,10, $_POST['myText']);


$pdf->Output();

?>

By the way, if I remove the $_POST reference in the PHP and run in a browser, it works fine, so the issue appears to be in my AS3. Thanks in advance.