6 Replies Latest reply on Jul 16, 2013 3:44 AM by Aur15

    Writing an image to PDF

    Aur15

      Hi everybody,

       

      I encountered a problem when writing a pdf file in Flash Builder. I need to save some images from jpg/png files, but the method:


      pdf.addImage(image, null, 20, 200);

      requires the image to be a DisplayObject, which I do not know how to obtain from my jpg/png files.

      I would be extremely grateful if you can give me a suggestion. Thank you!

        • 1. Re: Writing an image to PDF
          Craberoid

          Hello!

          You don't write what library you are using for export image to PDF, but I can suggest that is AlivePDF.

          If it true and your image is valid png/jpg file in ByteArray you can try to use addImageStream method like that:

           

          pdf.addImageStream(image, ColorSpace.DEVICE_RGB, new Resize(Mode.NONE, Position.LEFT), 0, 0, 20, 200);

          • 2. Re: Writing an image to PDF
            Aur15 Level 1

            Hi,

             

            I am using AlivePDF. I tried both addImage (...) and add ImageStream (...), but they didn't wok.  In the second method for example the method requests image in ByteArray.  How can I transform the image from jpg/png/...file in an array of bytes? I tried various ways but they did not work. I would appreciate if you can suggest something. Thank you.

            • 3. Re: Writing an image to PDF
              Craberoid Level 1

              For example, you can load image by using Loader/URLLoader. After this you will have a Bitmap instance with your image. And then you can use addImage(bitmap...).

              • 4. Re: Writing an image to PDF
                Aur15 Level 1

                I did that already, but it did not work...no idea why.

                Here is my code:

                 

                <?xml version="1.0" encoding="utf-8"?>

                <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"

                                                                     xmlns:s="library://ns.adobe.com/flex/spark"

                                                                     xmlns:mx="library://ns.adobe.com/flex/mx">

                 

                <fx:Script>

                                    <![CDATA[

                                              import mx.events.FlexEvent;

                                              import mx.graphics.codec.JPEGEncoder;

                 

                                              import org.alivepdf.display.Display;

                                              import org.alivepdf.fonts.*;

                                              import org.alivepdf.layout.*;

                                              import org.alivepdf.pages.Page;

                                              import org.alivepdf.pdf.PDF;

                                              import org.alivepdf.saving.Method;

                 

                                              import spark.components.*;

                 

                                              private var image:Loader = new Loader;

                                              private var pdf:PDF;

                 

                 

                                              protected function onBtnGeneratePDF(event:MouseEvent):void

                                              {

                                                        pdf = new PDF( Orientation.PORTRAIT, Unit.MM, Size.A4 );

                                                        pdf.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE );

                 

                                                        var newPage:Page = new Page ( Orientation.PORTRAIT, Unit.MM, Size.A4 );

                                                        pdf.addPage( newPage );

                 

                                                        image.load(new URLRequest("bear.jpg"));

                                                        image.contentLoaderInfo.addEventListener(Event.COMPLETE, done);

                 

                                                        var fs:FileStream = new FileStream(); 

                                                        var file:File = File.desktopDirectory.resolvePath("testPdf.pdf");  

                                                        fs.open(file, FileMode.WRITE);  

                                                        var bytes:ByteArray = pdf.save(Method.LOCAL);  

                                                        fs.writeBytes(bytes);  

                                                        fs.close();

                                              }

                 

                 

                                              private function done (e:Event):void {

                                                        pdf.addImage(image, null, 20, 20);

                                              }

                 

                 

                                    ]]>

                          </fx:Script>

                 

                 

                 

                          <s:Button x="10" y="179" label="Generate PDF" click="onBtnGeneratePDF(event)"/>

                 

                 

                </s:WindowedApplication>

                 

                Please, let me know if you understand why the image doesn't appear in the pdf file?

                Thank you!

                • 5. Re: Writing an image to PDF
                  Craberoid Level 1

                  pdf.save can performed before pdf.addImage in your code. Maybe this is reason why it don't work.

                  Try to put

                  var bytes:ByteArray = pdf.save(Method.LOCAL); 

                                                          fs.writeBytes(bytes); 

                                                          fs.close();

                   

                  after

                  pdf.addImage(image, null, 20, 20);

                  1 person found this helpful
                  • 6. Re: Writing an image to PDF
                    Aur15 Level 1

                    Thank you! It works like that:

                     

                    <?xml version="1.0" encoding="utf-8"?>

                    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"

                                                                         xmlns:s="library://ns.adobe.com/flex/spark"

                                                                         xmlns:mx="library://ns.adobe.com/flex/mx">

                     

                              <fx:Script>

                                        <![CDATA[

                     

                    import mx.events.FlexEvent;

                                                  import mx.graphics.codec.JPEGEncoder;

                                                  import org.alivepdf.display.Display;

                                                  import org.alivepdf.fonts.*;

                                                  import org.alivepdf.layout.*;

                                                  import org.alivepdf.pages.Page;

                                                  import org.alivepdf.pdf.PDF;

                                                  import org.alivepdf.saving.Method; 

                                                  import spark.components.*; 

                                                  private var image:Loader = new Loader;

                                                  private var pdf:PDF;

                     

                      protected function onBtnGeneratePDF(event:MouseEvent):void

                      {

                      loadImage();  

                      }

                     

                     

                                                  protected function loadImage():void{ 

                                                            image.load(new URLRequest("bear.jpg"));

                                                            image.contentLoaderInfo.addEventListener(Event.COMPLETE, done);

                                                  }

                     

                                                  private function done (e:Event):void {

                                                            pdf = new PDF( Orientation.PORTRAIT, Unit.MM, Size.A4 );

                                                            pdf.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE );

                     

                                                            var newPage:Page = new Page ( Orientation.PORTRAIT, Unit.MM, Size.A4 );

                                                            pdf.addPage( newPage );

                                                            pdf.addImage(image, null, 20, 20);

                     

                                                            var fs:FileStream = new FileStream(); 

                                                            var file:File = File.desktopDirectory.resolvePath("testPdf.pdf");  

                                                            fs.open(file, FileMode.WRITE);  

                                                            var bytes:ByteArray = pdf.save(Method.LOCAL);  

                                                            fs.writeBytes(bytes);  

                                                            fs.close(); 

                                                  }

                     

                                        ]]>

                              </fx:Script>

                     

                              <s:Button x="10" y="179" label="Generate PDF" click="onBtnGeneratePDF(event)"/>

                     

                    </s:WindowedApplication>

                     

                    I hope this would be helpful for someone else struggling with writing pdf in Flex.