6 Replies Latest reply on Jun 8, 2015 12:00 AM by ethan25

    Flex 4 - Generate PDF - AlivePDF

    djh88ukwb Level 1

      Hello all.

       

      I am in need of creating PDF documents from a flash project.  THe flash project needs to be using the latest flex 4. And i will be sing the <s:Application> tags.

       

      I grabbed an example from "Tour de Flex"  but it was built using <mx:Application>. The code for this example is here

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application  xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%"
           viewSourceURL="srcview/index.html" backgroundColor="0x000000" layout="absolute">

          <mx:Script>
              <![CDATA[
                  import org.alivepdf.pages.Page;
                  import org.alivepdf.pdf.PDF;
                  import org.alivepdf.layout.Orientation;
                  import org.alivepdf.layout.Size;
                  import org.alivepdf.layout.Unit;
                  import org.alivepdf.display.Display;
                  import org.alivepdf.saving.Method;
                  import org.alivepdf.fonts.FontFamily;
                  import org.alivepdf.fonts.Style;
                  import org.alivepdf.colors.RGBColor;
                 
                  import mx.utils.UIDUtil;
                 
                  protected var claimPDF:PDF;
                 
                  [Embed(source="assets/Insuricorp-Logo.jpg", mimeType="application/octet-stream" )]
                  protected var jpgBytes:Class;
                 
                  protected function savePDF(e:MouseEvent):void
                  {
                      claimPDF = new PDF(Orientation.LANDSCAPE, Unit.MM, Size.LETTER);
                      claimPDF.setDisplayMode (Display.FULL_WIDTH);

                      claimPDF.addPage();
                      claimPDF.addImageStream(new jpgBytes() as ByteArray, 5, 5, 0, 0, 1);
                     
                      claimPDF.setFont(FontFamily.ARIAL , Style.NORMAL, 12);
                      claimPDF.addText("Claimant Name: " + this.firstName.text + " " + lastName.text,10,40);
                      claimPDF.addText("Date: " + this.date.text,10,50);
                      claimPDF.addTextNote(48,45,100,2,"Claim Filed on: " + this.date.text + " today's date: " + new Date());
                      claimPDF.addText("Policy #: " + this.policyNum.text,10,60);
                      claimPDF.addText("Contact #: " + this.contact.text,10,70);
                      claimPDF.addText(this.claimNum.text,10,80);
                      claimPDF.addText("Claim Description:",10,90);
                      claimPDF.setXY(10,95);
                      claimPDF.addMultiCell(200,5,desc.text);
                     
                      // HERE IS HOW TO SAVE FROM FLASH PLAYER 10 OR GREATER. THIS IS USED FOR THIS SAMPLE.
                      var bytes:ByteArray = claimPDF.save(Method.LOCAL);
                      var f:FileReference = new FileReference();
                      f.save(bytes,"tourdeflex-pdf-sample.pdf");
                     
                      // HERE IS HOW TO SAVE FROM AN AIR APP
                      /*
                      var fs:FileStream = new FileStream();
                      var file: File = File.desktopDirectory.resolvePath("tourdeflex-pdf-sample.pdf");
                      fs.open(file, FileMode.WRITE);
                      var pdfBytes:ByteArray = claimPDF.savePDF(Method.LOCAL);
                      fs.writeBytes(pdfBytes);
                      fs.close();
                      */     
                      
                       // HERE IS HOW TO SAVE FROM FLEX APP RUNNING FLASH PLAYER < 10
                       /* NOTE: To save the PDF from a Flex application if running Flash Player version < 10, you need to call a server-side
                                script as the 2nd parameter of the save call. More information about this can be found here:
                                http://alivepdf.bytearray.org/?p=17
                      claimPDF.save( ethod.REMOTE, "http://alivepdf.bytearray.org/wp-content/demos/create.php", "tourdeflex-pdf-sample.pdf");*/
                   }
                 
                  protected function submitClaimNum():void
                  {
                      claimNum.text='Claim # Assigned: '+ UIDUtil.createUID();//just generate a random id for sample purposes
                      this.savePDFBtn.visible=true;
                  }
              ]]>
          </mx:Script>
         
          <mx:Style>
              FormItem, Label, Button {
                  color: #FFFFFF;
              }
              TextInput, DateField {
                  color: #000000;
              }
          </mx:Style>   
          <mx:Label top="10" left="10" text="Input information and press 'File Claim' to get assigned claim #, then press Save to PDF." color="0xCCCCCC"/>   
           <mx:Label fontSize="18" text="Insurance Claim Form" left="10" top="30"/>
          <mx:HBox width="100%" height="100%" top="40" left="10" color="0x000000">
              <mx:Form id="form1" width="283" height="300">
                  <mx:FormItem label="First Name:">
                      <mx:TextInput id="firstName"/>
                  </mx:FormItem>
                  <mx:FormItem label="Last Name:">
                      <mx:TextInput id="lastName"/>
                  </mx:FormItem>
                  <mx:FormItem label="Date:">
                      <mx:DateField id="date" />
                  </mx:FormItem>
                  <mx:FormItem label="Policy #:">
                      <mx:TextInput id="policyNum" />
                  </mx:FormItem>
                  <mx:FormItem label="Contact #:">
                      <mx:TextInput id="contact" maxChars="12"/>
                  </mx:FormItem>
              </mx:Form>   
         
              <mx:Canvas top="10" left="10" width="300" height="100%">
                  <mx:Label text="Description:" y="2"/>
                  <mx:TextArea id="desc" y="18" height="133" width="205"/>   
              </mx:Canvas>
          </mx:HBox>
         
          <mx:HBox top="205" left="185">
              <mx:Button label="File Claim" click="submitClaimNum()"/>
              <mx:Button horizontalCenter="0" click="savePDF(event)" label="Save to PDF" id="savePDFBtn" visible="false"/>
          </mx:HBox>
          <mx:Label id="claimNum" top="238" left="83"/>
         
      </mx:Application>

       

       

      SO i tried changing it to <s:Application>  but i get a load of errors saying stuff isnt supported etc etc.

       

      My main question is -  Does flex 4 and <s:Application> support AlivePDF or is there another way to create PDF documents?

       

      My code for the application where i have tried to change it to the latest flex version is as follows

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
          <fx:Declarations>
              <!-- Place non-visual elements (e.g., services, value objects) here -->
          </fx:Declarations>
         
          <fx:Script>
              <![CDATA[
              import org.alivepdf.pages.Page;
              import org.alivepdf.pdf.PDF;
              import org.alivepdf.layout.Orientation;
              import org.alivepdf.layout.Size;
              import org.alivepdf.layout.Unit;
              import org.alivepdf.display.Display;
              import org.alivepdf.saving.Method;
              import org.alivepdf.fonts.FontFamily;
              import org.alivepdf.fonts.Style;
              import org.alivepdf.colors.RGBColor;
             
              import mx.utils.UIDUtil;
             
              protected var claimPDF:PDF;
             
              [Embed(source="assets/Insuricorp-Logo.jpg", mimeType="application/octet-stream" )]
              protected var jpgBytes:Class;
             
              protected function savePDF(e:MouseEvent):void
              {
              claimPDF = new PDF(Orientation.LANDSCAPE, Unit.MM, Size.LETTER);
              claimPDF.setDisplayMode (Display.FULL_WIDTH);
             
              claimPDF.addPage();
              claimPDF.addImageStream(new jpgBytes() as ByteArray, 5, 5, 0, 0, 1);
             
              claimPDF.setFont(FontFamily.ARIAL , Style.NORMAL, 12);
              claimPDF.addText("Claimant Name: " + this.firstName.text + " " + lastName.text,10,40);
              claimPDF.addText("Date: " + this.date.text,10,50);
              claimPDF.addTextNote(48,45,100,2,"Claim Filed on: " + this.date.text + " today's date: " + new Date());
              claimPDF.addText("Policy #: " + this.policyNum.text,10,60);
              claimPDF.addText("Contact #: " + this.contact.text,10,70);
              claimPDF.addText(this.claimNum.text,10,80);
              claimPDF.addText("Claim Description:",10,90);
              claimPDF.setXY(10,95);
              claimPDF.addMultiCell(200,5,desc.text);
             
              // HERE IS HOW TO SAVE FROM FLASH PLAYER 10 OR GREATER. THIS IS USED FOR THIS SAMPLE.
              var bytes:ByteArray = claimPDF.save(Method.LOCAL);
              var f:FileReference = new FileReference();
              f.save(bytes,"tourdeflex-pdf-sample.pdf");
             
              // HERE IS HOW TO SAVE FROM AN AIR APP
              /*
              var fs:FileStream = new FileStream();
              var file: File = File.desktopDirectory.resolvePath("tourdeflex-pdf-sample.pdf");
              fs.open(file, FileMode.WRITE);
              var pdfBytes:ByteArray = claimPDF.savePDF(Method.LOCAL);
              fs.writeBytes(pdfBytes);
              fs.close();
              */     
             
              // HERE IS HOW TO SAVE FROM FLEX APP RUNNING FLASH PLAYER < 10
              /* NOTE: To save the PDF from a Flex application if running Flash Player version < 10, you need to call a server-side
              script as the 2nd parameter of the save call. More information about this can be found here:
              http://alivepdf.bytearray.org/?p=17
              claimPDF.save( ethod.REMOTE, "http://alivepdf.bytearray.org/wp-content/demos/create.php", "tourdeflex-pdf-sample.pdf");*/
              }
             
              protected function submitClaimNum():void
              {
              claimNum.text='Claim # Assigned: '+ UIDUtil.createUID();//just generate a random id for sample purposes
              this.savePDFBtn.visible=true;
              }
              ]]>
          </fx:Script>
         
         
          <mx:Label top="10" left="10" text="Input information and press 'File Claim' to get assigned claim #, then press Save to PDF." color="0xCCCCCC"/>   
          <mx:Label fontSize="18" text="Insurance Claim Form" left="10" top="30"/>
          <mx:HBox width="100%" height="100%" top="40" left="10" color="0x000000">
              <mx:Form id="form1" width="283" height="300">
                  <mx:FormItem label="First Name:">
                      <mx:TextInput id="firstName"/>
                  </mx:FormItem>
                  <mx:FormItem label="Last Name:">
                      <mx:TextInput id="lastName"/>
                  </mx:FormItem>
                  <mx:FormItem label="Date:">
                      <mx:DateField id="date" />
                  </mx:FormItem>
                  <mx:FormItem label="Policy #:">
                      <mx:TextInput id="policyNum" />
                  </mx:FormItem>
                  <mx:FormItem label="Contact #:">
                      <mx:TextInput id="contact" maxChars="12"/>
                  </mx:FormItem>
              </mx:Form>   
             
              <mx:Canvas top="10" left="10" width="300" height="100%">
                  <mx:Label text="Description:" y="2"/>
                  <mx:TextArea id="desc" y="18" height="133" width="205"/>   
              </mx:Canvas>
          </mx:HBox>
         
          <mx:HBox top="205" left="185">
              <mx:Button label="File Claim" click="submitClaimNum()"/>
              <mx:Button horizontalCenter="0" click="savePDF(event)" label="Save to PDF" id="savePDFBtn" visible="false"/>
          </mx:HBox>
          <mx:Label id="claimNum" top="238" left="83"/>
         
      </s:Application>

       

       

      If anyone can help that would be great

        • 1. Re: Flex 4 - Generate PDF - AlivePDF
          Atishay Jain

          I am doing the same code, I need a favor I am creating client side PDF with image using flex and AlivePDF for a web based application. Images have been generated on that pdf but it is creating problem for large size images as half of the image disappeared from that pdf.I am taking the image inside a canvas . How do i control my images so that they come fit on that pdf file for any image size that i take.

           

          Regards:

          Atishay Jain

          • 2. Re: Flex 4 - Generate PDF - AlivePDF
            chandu3107 Level 2

            Flex 4 supports alive pdf generation.

             

            Follwing is the sample code of generating pdf.

             

            var newPDF:PDF =

            new PDF(Orientation.LANDSCAPE, Unit.MM, Size.LETTER);

            newPDF.setDisplayMode (Display.FULL_WIDTH);

             

            newPDF.addPage();

            newPDF.setFont(FontFamily.ARIAL , Style.BOLD, 18);

            newPDF.addText(

            "TITLE" ,100,10);

            newPDF.setFont(FontFamily.ARIAL , Style.NORMAL, 16);

             

            newPDF.setXY(10,95);

             

            newPDF.addImage(orderDG,50,20);

            newPDF.addText(

            "TEST 1 " ,50,120);

            newPDF.addText(

            "TEST 2 ",150,120);

             

             

             

            var fileREf:FileStream = new FileStream();

            var file:File = File.applicationStorageDirectory.resolvePath('Test.pdf');

            var stream:FileStream = new FileStream()

            stream.open(file,FileMode.WRITE);

             

            stream.writeBytes(newPDF.save(Method.LOCAL));

            stream.close();

             

             

            • 3. Re: Flex 4 - Generate PDF - AlivePDF
              Atishay Jain Level 1

              Thanks for your reply

               

              Sir see this code http://alivepdf.bytearray.org/?paged=5

               

              Here when images have been generated they are coming fit to the pdf it doesn't depends on the image size i need exactly the code for this demo shown here.

              Please help me.

              Atishay

              • 4. Re: Flex 4 - Generate PDF - AlivePDF
                chandu3107 Level 2

                [

                Embed(source="logo.jpg", mimeType="application/octet-stream" )]

                 

                protected var jpgBytes:Class;

                 

                 

                newPDF =

                 

                 

                new PDF(Orientation.LANDSCAPE, Unit.MM, Size.LETTER);

                newPDF.setDisplayMode (Display.FULL_WIDTH);

                 

                newPDF.addPage();

                newPDF.setFont(FontFamily.ARIAL , Style.BOLD, 18);

                newPDF.addText(

                "Order Capture" ,100,10);

                newPDF.addImageStream(

                 

                new jpgBytes() as ByteArray, 200, 5,30,30, 1);

                 

                 

                in addImageStream you can specify width and height of image.

                 

                Hope this will help you.

                • 5. Re: Flex 4 - Generate PDF - AlivePDF
                  K8_1984 Level 1

                  Hi all,

                  I'm struggling with this one too - I copied savePdf() along with other stuff into newly created actionscript class called Pdf.as. As soon as I try to compile all it gives me an error that there is no option to import this:

                   

                          import org.alivepdf.colors.RGBColor;
                          import org.alivepdf.display.Display;
                          import org.alivepdf.fonts.FontFamily;
                          import org.alivepdf.fonts.Style;
                          import org.alivepdf.layout.Orientation;
                          import org.alivepdf.layout.Size;
                          import org.alivepdf.layout.Unit;
                          import org.alivepdf.pages.Page;
                          import org.alivepdf.pdf.PDF;
                          import org.alivepdf.saving.Method;

                   

                  I tried to go through this article someone suggested :

                  http://murrayhopkins.wordpress.com/2011/01/07/using-alivepdf-to-print-from-air-javascript- via-actionscript3-part-1/


                  but it doesn't seem to be reasonable to do all this swc, swf and the rest.

                   

                  Any suggestions?

                  • 6. Re: Flex 4 - Generate PDF - AlivePDF
                    ethan25

                    Hello,

                     

                    I'm creating a FLEX application and im not able to generate PDF.

                     

                    please refer to this code which i'm trying How to extract the content of AdvancedDataGrid into PDF in FLEX

                     

                    and to my surprise there is no save function in FileReference class

                    i change the FLEX compiler to 12.2.0 even then the same problem

                     

                    Regards,

                    Nitesh