A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Generate Vector File During Runtime?

  1. #1
    Junior Member
    Join Date
    Oct 2008
    Posts
    4

    Generate Vector File During Runtime?

    I am just wondering if anyone can help with my query.

    I want to know if it is possible to create a vector based file (EPS) from a web based Flash application. The application would be a 'Design it yourself' type deal, it would just be a fancy text generator. All this will be designed by the user using the flash application.

    What I want to know is once the user has finished designing their text, can this 'creation' then be exported as a Vector based EPS, Ai or PDF file during runtime? The application currently outputs a PNG file, but I want it to output an editable vector based file.

    Anyone know if its possible, any working examples? if so can they provide details?

    Hope that makes sense, let me know if more clarification is needed.

    Any help greatly appreciated.

    David.

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    20
    Doing some searching for something similar, and I found this old thread with no replies.

    Bummer.

    I am looking for something like that too...

    I wonder if David is still around and if he ever found anything.

  3. #3
    Member
    Join Date
    May 2012
    Posts
    51
    There is no such built-in capability. You have to write the code yourself. PDF is a text-based format. Create one containing only vector graphic is not going to be crazy hard. From the specs, here's what a chunk of uncompressed PDF looks like:

    % Draw a rectangle, 1 unit light blue border,
    % filled with red
    200 200 m
    .5 .75 1 rg %light blue for fill color
    1 0 0 RG %red for stroke color
    200 300 50 75 re
    B

    http://www.pdf-tools.com/public/down...eference12.pdf
    m is moveto and re is rectangle. B means both fill and stroke the path.

    The only somewhat trickier part is keeping track of the file offsets in the cross-reference table.

  4. #4
    Member
    Join Date
    May 2012
    Posts
    51
    To help you get started, here's a very simple class that generates a one-page PDF file.

    Actionscript Code:
    package
    {
        import flash.utils.ByteArray;

        public class PDFGraphics
        {  
            protected var commands:Array = [];
           
            public function PDFGraphics() {
                // use top-down coordinates
                commands.push('1 0 0 -1 0 792 cm');
            }
           
            public function beginFill(color:uint):void {
                // RGB values are 0.0 to 1.0
                var r:Number = Number(color >> 16 & 0xFF) / 255;
                var g:Number = Number(color >> 8 & 0xFF) / 255;
                var b:Number = Number(color >> 0 & 0xFF) / 255;
                commands.push(r + ' ' + g + ' ' + b + ' rg');
            }
           
            public function drawRect(x:int, y:int, width:int, height:int):void {
                commands.push(x + ' ' + y + ' ' + width + ' ' + height + ' re');
            }
           
            public function endFill():void {
                commands.push('f*');
            }
           
            public function get data():ByteArray {
                var i:int;
                var pdfObjects:Array = new Array;
                pdfObjects[1] = '<</Type /Catalog /Pages 3 0 R /Outlines 2 0 R>>';
                pdfObjects[2] = '<</Type /Outlines /Count 0>>';
                pdfObjects[3] = '<</Type /Pages /Count 1 /Kids [4 0 R]>>';
                pdfObjects[4] = '<</Type /Page /Parent 3 0 R /Resources << /ProcSet 6 0 R >> /MediaBox [0 0 612 792] /Contents 5 0 R>>';               
                pdfObjects[6] = '[/PDF]';
               
                var streamData:String = commands.join('\n');
                pdfObjects[5] = '<</Length ' + streamData.length + '>>\nstream\n' + streamData + '\nendstream';
               
                // write the objects, remember their offsets
                var offsets:Array = new Array;
                var ba:ByteArray = new ByteArray;
                ba.writeMultiByte('%PDF-1.0\n', 'ascii');
                for(i = 1; i < 7; i++) {
                    offsets[i] = ba.position;
                    ba.writeMultiByte(i + ' 0 obj\n', 'ascii');
                    ba.writeMultiByte(pdfObjects[i] + '\n', 'ascii');
                }
               
                // write the cross-reference table, remember its offset
                var xrefOffset:uint = ba.position;
                ba.writeMultiByte('xref\n', 'ascii');
                ba.writeMultiByte('0 7\n', 'ascii');
                ba.writeMultiByte('0000000000 65535 f\n', 'ascii');
                for(i = 1; i < 7; i++) {
                    var offsetStr:String = offsets[i];
                    offsetStr = '0000000000'.substr(offsetStr.length) + offsetStr;
                    ba.writeMultiByte(offsetStr + ' 00000 n\n', 'ascii');
                }
               
                // write the trailer
                ba.writeMultiByte('trailer\n', 'ascii');
                ba.writeMultiByte('<</Size 7 /Root 1 0 R>>', 'ascii');
                ba.writeMultiByte('startxref\n', 'ascii');
                ba.writeMultiByte(xrefOffset + '\n', 'ascii');
                ba.writeMultiByte('%%EOF', 'ascii');
                return ba;
            }
        }
    }

    Barebone Flex test application:

    Actionscript Code:
    <?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:Script>
            <![CDATA[
                protected function button1_clickHandler(event:MouseEvent):void
                {
                    var canvas:PDFGraphics = new PDFGraphics;
                    var fileRef:FileReference = new FileReference;
                    canvas.beginFill(0x0000FF);
                    canvas.drawRect(100, 200, 250, 100);
                    canvas.drawRect(110, 210, 230, 80);
                    canvas.endFill();
                    fileRef.save(canvas.data, 'test.pdf');             
                }
            ]]>
        </fx:Script>
       
        <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
        </fx:Declarations>
        <s:Button x="10" y="10" label="Save" click="button1_clickHandler(event)"/>
    </s:Application>

    I only implemented the drawRect() function. The rest should be fairly simple. Just follow the specs.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center