Skip to main content
Inspiring
January 11, 2011
Question

Create a PDF from a Flash page

  • January 11, 2011
  • 2 replies
  • 6714 views

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.

This topic has been closed for replies.

2 replies

TezS56Author
Inspiring
January 28, 2011

Sorry still can't work this out. I have looked at the tutorials and tried 
your code below but no luck. Do you think I could ask you to show me exactly 
where I put this code. I take it, it is in a class and I have fiddled with 
this for hours but to no avail. I also noticed that the PDF is created, with 
your code, server side. I need the PDF to be created on the client. Is this 
possible in FLASH? The tutorials seem to say that FLEX is the one to do this 
client side?

Sorry to be a bother with this but I really need this to  work.

Kenneth Kawamoto
Community Expert
Community Expert
January 31, 2011

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");
        }
    }   
}

TezS56Author
Inspiring
January 31, 2011

Thank you so much for your help, again. I have created the example as shown

but I am still getting an error message:

"PDFDemo.as, Line 39 1061: Call to a possibly undefined method save

through a reference with static type Class."

37 private function click(e:MouseEvent):void

38 {

39 new FileReference .save(_pdfByteArray, "hello-world!.pdf");

40

41 }

Kenneth Kawamoto
Community Expert
Community Expert
January 11, 2011

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.

TezS56Author
Inspiring
January 11, 2011

Thank you so much for your reply. I actually do need a client side solution. I did look at AlivePDF but I could not work out how to use it. If you could help me here I would be grateful but otherwise I will investigate further. Again thanks much for your interest.

Terry Sayers

Sent from my iPad

Kenneth Kawamoto
Community Expert
Community Expert
January 12, 2011

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.