11 Replies Latest reply: Mar 10, 2010 9:56 AM by hybridmindset RSS

    Can Flex print to PDF

    lee704 Community Member

      Does Flex have the ability to print to a PDF instead of just printing to a printer installed on a user's machine?  I want to print a datagrid.  I can do so but must select an installed printer.  I would prefer Flex print directly to a PDF instead of relying upon a local printer.  Can this be done?

       

      Thanks!

        • 1. Re: Can Flex print to PDF
          Anthony T DeBonis Community Member

          Yes it can be done - you have some options but you need a server on the backend... unless user just used a PDF Print Driver...

           

          Check out:

          http://alivepdf.org/

          http://flexblog.faratasystems.com/?p=237

          http://www.kalengibbons.com/blog/index.php/tag/custom-printing-with-flex/

           

          LCDS also had good server side PDF Generation with more exact text format and image location.

          • 2. Re: Can Flex print to PDF
            lee704 Community Member

            Thanks Anthony.  Would it be best to assume that Flex does not have "built-in" print to PDF libraries?

            • 3. Re: Can Flex print to PDF
              AKloft Community Member

              From my use, we have always used LiveCycle to do our PDF generation.  Anthony is correct about their information though, but you certainly can't require someone to install a "print to pdf" driver on their machine.  Another thing you could do is create a a Flex page that is laid out much like a pdf (look and feel), and just populate text boxes within that page and they can do a file->save as... on the page.

              • 4. Re: Can Flex print to PDF
                hybridmindset Community Member

                you might want to check out my little prototype which goes from flex tlf to print read

                y pdfs. http://hybridmindset.com/blog/How-I-Got-WYSIWYG-TLF-Text-To-PDF-from-Flex-Part-1

                • 5. Re: Can Flex print to PDF
                  Cr99 Community Member

                  hybrid, that demo is incredible!  Now the question I have is can this be reproduced for non-text components, specifically- charts?

                   

                  You say on your blog:

                   

                  "This method renders the text in the pdf as vector text, so when printing you get the exact representation of the text keeping perfect print fidelity. The XSL-FO language facilitates inline graphic images"

                   

                  But are those graphic images also vector based?  I can get images into a pdf, but they are not vector based so they scale horribly and this defeats the whole purpose for me of having them in a pdf format.

                  • 6. Re: Can Flex print to PDF
                    Karl_Sigiscar_1971 Community Member

                    You can have your bitmaps filtered with bilinear filtering in PDF just as you can in Flex so that they look good when stretched.

                    • 7. Re: Can Flex print to PDF
                      Cr99 Community Member

                      Hmm, that's the first I've heard about this algorithm.  How do you enable it?  I am just sending a byte array now to the server and creating the png file there:

                       

                      // flex code for image generation

                      ImageSnapshot.defaultEncoder = PNGEncoder;

                       

                      private function sendAllImages():void {

                          var imgArray:ArrayCollection = new ArrayCollection();

                          for (var i:int=0; i<vFrameList.length; i++) {

                                var vFrame:VBox = vFrameList.getItemAt(i) as VBox;

                                var cp:ChartPanel = getChartPanelFromBox(vFrame);

                                if (cp != null) {

                                    var imageSnap:ImageSnapshot = ImageSnapshot.captureImage(cp);

                                     var imageByteArray:ByteArray = imageSnap.data as ByteArray;

                                    imgArray.addItem(imageByteArray);

                               }

                          }  

                          reportRetrieval.saveAllImages(report, imgArray);        

                          }

                      • 8. Re: Can Flex print to PDF
                        Karl_Sigiscar_1971 Community Member

                        If you use a PDF template created with LiveCycle Designer, you have created an ImageField property to display your image dynamically. Just make sure the sizing property is set to Scale Image Proportionally. By default, the image will be filtered.

                         

                        PS: If you want an example of what this algorithm give in Flex (with the smoothing property of a Bitmap set to true), check this comparison sample:

                        http://sigiscarinc.com/

                        • 9. Re: Can Flex print to PDF
                          hybridmindset Community Member

                          Thanks, well I haven't tried printing flex components to pdf but I know you could use the method I layed out in my post for charts and graphs of data. As long as the charts and graphs are svg objects. And as for images, I was referring to svg vector images. So the only way you will get scaling of images and keep their quality is to use a vector based image. I hope to post the Part II that I am working on, up on my blog pretty soon which will show the demo of svg images, bitmaps, and text rendered to pdf with exact replication!

                          • 10. Re: Can Flex print to PDF
                            Cr99 Community Member

                            @hybrid:  I'll be interested to see what you come up with.  Charts are DisplayObjects and I'm not sure how to get them into SVG format.  I know flex draws them using vectors while in the app and I just wish there was a way to go straight from that vector based rendering to PDF.

                             

                            @karl:  The smoothing example looks decent enough.  It's just too bad there is not a way to preserve the kind of crispness you get with vector based images.

                            • 11. Re: Can Flex print to PDF
                              hybridmindset Community Member

                              here is a link to an tutorial on creatng svg charts with data in an xml file. You can just export the data from your flex charts and form an xml file and process it on the server side using xslt files. http://fgeorges.blogspot.com/2008/04/simple-svg-chart-generation-with-xslt.html