Posted By

adrianparr on 12/16/10


Tagged

pdf create generate as3 alivepdf addPage addImage


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

burnandbass
dubogii


AS3 Generate PDF of Sprite or MovieClip using AlivePDF


 / Published in: ActionScript 3
 

URL: http://blog.unthinkmedia.com/2008/09/05/exporting-pdfs-in-flex-using-alivepdf/

Note: The width of 540 and height of 790 seems to fit an A4 sheet quite nicely.

  1. // Useful Resources
  2. // http://alivepdf.bytearray.org/
  3. // http://blog.unthinkmedia.com/2008/09/05/exporting-pdfs-in-flex-using-alivepdf/
  4. // http://blog.juanbonfante.com/?p=272
  5. // http://lucamezzalira.com/2009/02/28/create-pdf-in-runtime-with-actionscript-3-alivepdf-zinc-or-air-flex-or-flash/
  6.  
  7.  
  8. package
  9. {
  10. import flash.display.Sprite;
  11. import flash.events.Event;
  12. import flash.net.FileReference;
  13. import flash.utils.ByteArray;
  14.  
  15. import org.alivepdf.pdf.PDF;
  16. import org.alivepdf.layout.Orientation;
  17. import org.alivepdf.layout.Size;
  18. import org.alivepdf.layout.Unit;
  19. import org.alivepdf.display.Display;
  20. import org.alivepdf.saving.Method;
  21.  
  22. [SWF(width = '540', height = '790', backgroundColor = '#FFFFFF', frameRate = '25')]
  23.  
  24. public class Main extends Sprite
  25. {
  26.  
  27. private var _mySprite:Sprite;
  28. private var myPDF:PDF;
  29. private var _pdfFilename:String = "drawing.pdf";
  30.  
  31. public function Main():void
  32. {
  33. if (stage) init();
  34. else addEventListener(Event.ADDED_TO_STAGE, init);
  35. }
  36.  
  37. private function init(e:Event = null):void
  38. {
  39. removeEventListener(Event.ADDED_TO_STAGE, init);
  40.  
  41. _mySprite = new Sprite();
  42. drawCrossBox(_mySprite, 540, 790, 0, 0, 0xCCCCCC, 1, 1, 0x000000, 1);
  43. addChild(_mySprite);
  44. generatePDF();
  45. }
  46.  
  47.  
  48. private function generatePDF():void
  49. {
  50. myPDF = new PDF(Orientation.PORTRAIT, Unit.MM, Size.A4);
  51. myPDF.setDisplayMode(Display.FULL_WIDTH);
  52.  
  53. myPDF.addPage();
  54. myPDF.addImage(_mySprite);
  55.  
  56. var fileReference:FileReference = new FileReference();
  57. var byteArray:ByteArray = myPDF.save(Method.LOCAL);
  58. fileReference.save(byteArray, _pdfFilename);
  59. }
  60.  
  61.  
  62. private function drawCrossBox($sprite:Sprite, $width:int=100, $height:int=100, $x:int=0, $y:int=0, $bgColor:uint=0xFFFFFF, $bgAlpha:Number=1, $lineThickness:Number=1, $lineColor:uint=0x000000, $lineAlpha:Number=1):void
  63. {
  64. $sprite.graphics.beginFill($bgColor, $bgAlpha);
  65. $sprite.graphics.drawRect($x, $y, $width, $height);
  66. $sprite.graphics.endFill();
  67.  
  68. $sprite.graphics.lineStyle($lineThickness, $lineColor, $lineAlpha);
  69. $sprite.graphics.moveTo($x,$y);
  70. $sprite.graphics.lineTo($x+$width, $y);
  71. $sprite.graphics.lineTo($x+$width, $y+$height);
  72. $sprite.graphics.lineTo($x, $y+$height);
  73. $sprite.graphics.lineTo($x, $y);
  74. $sprite.graphics.lineTo($x+$width, $y+$height);
  75. $sprite.graphics.moveTo($x+$width, $y);
  76. $sprite.graphics.lineTo($x, $y+$height);
  77. }
  78.  
  79. }
  80.  
  81. }

Report this snippet  

You need to login to post a comment.