Skip navigation
Currently Being Moderated

Create a PDF from a Flash page

Jan 11, 2011 7:00 AM

I need some help with a request from a client. I have created a form that a user fills out and the answers they give are added to a email as text. This works great but now the client wants the text answers in a PDF not just as text in an email. My question is, is there a way to create a PDF from the answers on a form from within the Flash program. The answers are saved in variables then added as text to the email.

 
Replies
  • Currently Being Moderated
    Jan 11, 2011 7:32 AM   in reply to TezS56

    Alive http://alivepdf.bytearray.org/ lets you create PDF from Flash in client-side, but sounds like you need a server-side solution, which can be done by calling a server-side script (eg. PHP http://php.net/manual/en/book.pdf.php) from Flash.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 12, 2011 3:10 AM   in reply to TezS56

    All right then - here's a super rudimentary example, "Hello World!" (what else?)

     

    ...
     
        import org.alivepdf.display.Display;
        import org.alivepdf.fonts.CoreFont;
        import org.alivepdf.fonts.EmbeddedFont;
        import org.alivepdf.fonts.FontFamily;
        import org.alivepdf.fonts.IFont;
        import org.alivepdf.layout.Orientation;
        import org.alivepdf.layout.Size;
        import org.alivepdf.layout.Unit;
        import org.alivepdf.pdf.PDF;
        import org.alivepdf.saving.Download;
        import org.alivepdf.saving.Method;
     
    ...
     
            private function generatePDF():void {
                var pdf:PDF = new PDF(Orientation.PORTRAIT, Unit.MM, Size.A4);
                var helvetica:IFont = new CoreFont(FontFamily.HELVETICA);
                pdf.setDisplayMode(Display.REAL);
                pdf.addPage();
                pdf.setFont(helvetica, 24);
                pdf.writeText(24, "Hello World!");
                pdf.save(Method.REMOTE, "http://alivepdf.bytearray.org/wp-content/demos/create.php", Download.INLINE, "helloworld.pdf");
            }
    

     

    The PDF should appear in your browser window.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 31, 2011 2:20 AM   in reply to TezS56

    Here's an again super rudimental example - click on the stage will invoke the "save file as..." dialogue.

    package {
        import flash.display.Sprite;
        import flash.events.MouseEvent;
        import flash.net.FileReference;
        import flash.utils.ByteArray;
        
        import org.alivepdf.fonts.CoreFont;
        import org.alivepdf.fonts.FontFamily;
        import org.alivepdf.fonts.IFont;
        import org.alivepdf.layout.Orientation;
        import org.alivepdf.layout.Size;
        import org.alivepdf.layout.Unit;
        import org.alivepdf.pdf.PDF;
        import org.alivepdf.saving.Download;
        import org.alivepdf.saving.Method;
     
        public class PDFDemo extends Sprite {
            private var _pdfByteArray:ByteArray;
            
            public function PDFDemo():void {
                var pdf:PDF = new PDF(Orientation.PORTRAIT, Unit.MM, Size.A4);
                var helvetica:IFont = new CoreFont(FontFamily.HELVETICA);
                pdf.addPage();
                pdf.setFont(helvetica, 24);
                pdf.writeText(24, "Hello World!");
                _pdfByteArray = pdf.save(Method.LOCAL, null, Download.ATTACHMENT);
                
                stage.addEventListener(MouseEvent.CLICK, click);
            }
            
            private function click(e:MouseEvent):void {
                new FileReference().save(_pdfByteArray, "hello-world!.pdf");
            }
        }    
    }
     
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jan 31, 2011 3:18 AM   in reply to TezS56

    Have you imported FileReference class? Also your code is missing the parentheses. You could of course write it like this as well:

            private function click(e:MouseEvent):void {
                var file:FileReference = new FileReference();
                file.save(_pdfByteArray, "hello-world!.pdf");
            }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Dec 20, 2011 12:35 PM   in reply to kennethkawamoto2

    This was very helpful! Now, what I would like to do, is automatically save a copy of that same pdf to a folder of my choosing in the same level as the create.php file.  It's great that the user has a copy but I'd like a copy of it myself. Is that possible?

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 21, 2011 3:04 AM   in reply to TexZaa

    Generate your PDF using PHP and save it to wherever you like.

     

    --

    Kenneth Kawamoto

    http://www.materiaprima.co.uk/

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points