Last active
August 16, 2017 05:33
-
-
Save olafkrueger/17eaa04e7025f2b34ca23774119e4e39 to your computer and use it in GitHub Desktop.
This just shows how to create a base64 encoded image from a DisplayObject in Flex/AS3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ebasetesting.apps.kpi_analysis.core.as3.controller | |
{ | |
import com.ebase.core.interfaces.IWidget; | |
import com.ebase_testing.services.core.model.services.ConvertServiceDelegate; | |
import com.ebasetesting.model.proxy.LoginProxy; | |
import flash.display.BitmapData; | |
import flash.display.DisplayObject; | |
import flash.display.PixelSnapping; | |
import flash.net.URLRequest; | |
import flash.net.navigateToURL; | |
import flash.utils.ByteArray; | |
import mx.graphics.codec.PNGEncoder; | |
import mx.rpc.events.ResultEvent; | |
import mx.utils.Base64Encoder; | |
import org.puremvc.as3.multicore.interfaces.INotification; | |
import org.puremvc.as3.multicore.patterns.command.SimpleCommand; | |
public class CommandPrintImageToPDF extends SimpleCommand | |
{ | |
/** | |
* Print to PDF | |
* Create a PNG representation of the passed view and call image2PDF service | |
*/ | |
override public function execute(note: INotification):void { | |
var view:DisplayObject = note.getBody() as DisplayObject; | |
// Create bitmap | |
var bitmapData:BitmapData = new BitmapData(view.width, view.height); | |
bitmapData.draw(view); | |
// Create PNG from bitmap | |
var png:PNGEncoder= new PNGEncoder(); | |
var byteArray:ByteArray = png.encode(bitmapData); | |
// Encode as base64 to be able to pass the image via JSON as string | |
// (The drawback of base64 is, that it is approximately 30% more payload) | |
var base64Encoder:Base64Encoder = new Base64Encoder(); | |
base64Encoder.encodeBytes(byteArray); | |
var strBase64:String = base64Encoder.toString(); | |
var loginProxy:LoginProxy = facade.retrieveProxy(LoginProxy.NAME) as LoginProxy; | |
var service:ConvertServiceDelegate = new ConvertServiceDelegate(loginProxy.authToken); | |
// Just for demonstration: | |
// In real life it is probably not a good idea to call a async service by a command | |
service.image2PDF(strBase64, bitmapData.width, bitmapData.height, ConvertServiceDelegate.FORMAT_LANDSCAPE, | |
function onResult(event:ResultEvent):void { | |
var parsedJSON:Object = JSON.parse(event.result as String); | |
var urlRequest:URLRequest = new URLRequest(parsedJSON.url); | |
navigateToURL(urlRequest,"_blank"); | |
} | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment