PDF export to local using AlivePDF with flex - apache-flex

I need to export charts and data tables to pdf file in flex application.
For this we can user AlivePDF but i need to export to local not server.
Can we export to local system prompting user to select the location to export?
Thanks in advance.

Since FP10 the FileReference Class should support this via the save() function. The code to do this in Flash Player 10 or better is shown below:
var bytes:ByteArray = pdf.save(Method.LOCAL);
var file:FileReference = new FileReference();
file.save(bytes, "myPDF.pdf");

Try this
var pdfFile:PDF = new PDF();
var pdfByteArray:ByteArray = new ByteArray ();
pdfByteArray = pdfFile.save(Method.LOCAL);

With the latest version of AlivePDF (0.1.5 RC), you can do this:
var element:IBitmapDrawable; // Chart to export
var pdf:PDF = new UnicodePDF();
pdf.addPage();
var bitmapData:BitmapData = new BitmapData(element.width, element.height, false, 0xffffff);
try{
bitmapData.draw(element as IBitmapDrawable);
}catch(e:*)
{
throw new Error("bitmap draw failed");
}
var jpegencoder:JPEGEncoder = new JPEGEncoder(100);
var byteArray:ByteArray = jpegencoder.encode(bitmapData);
pdf.addImageStream(byteArray);
var file : FileReference = new FileReference()
file.save(pdf.save(Method.LOCAL),"my.pdf");

Related

Xamarin.Forms using SQLite.Net.Async

I have followed the instructions here http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/databases/ - to connect to the SQLite database synchronously.
public SQLiteConnection GetConnection()
{
var dbFilname = "localDB.db3";
string docsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(docsPath, dbFilname);
var plat = new SQLitePlatformAndroid();
var conn = new SQLiteConnection(plat, path);
return conn;
}
I want to change it to an asynchronous connection (SQLiteAsyncConnection) but can't get it to work.
According to the instructions here - https://components.xamarin.com/gettingstarted/sqlite-net -it just needs the path as a parameter
var conn = new SQLiteAsyncConnection(path);
that doesn't work, the error says that the parameters expected are:
a connection function, a TaskScheduler and TaskCreationOptions
I have no idea what to do and have not been able to find any examples that work.
Thanks in advance
You could simply reuse the GetConnection method you already have and create async connection like this:
var asyncDb = new SQLiteAsyncConnection(() => GetConnection());
Xamarin Platforms Android Config, iOS and WP basicaly equal
public SQLiteAsyncConnection GetConnectionAsync()
{
const string fileName = "MyDB.db3";
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var path = Path.Combine(documentsPath, fileName);
var platform = new SQLitePlatformAndroid();
var param = new SQLiteConnectionString(path, false);
var connection = new SQLiteAsyncConnection(() => new SQLiteConnectionWithLock(platform, param));
return connection;
}

Loading saved contents back in their places in flex air using flash builder 4.5

I have now gotten the idea of saving multiple components text into one file. But now when I open that file all the text in the file goes into the text of only one component. Here is the code,
<s:click>
var f:File = File.desktopDirectory;
f.browseForSave("Save As");
f.addEventListener(Event.SELECT, function (event:Event):void {
var stream:FileStream = new FileStream();
stream.open((event.target as File),FileMode.WRITE);
stream.writeUTFBytes(rte.htmlText);
stream.writeUTFBytes(ta.text);
stream.writeUTFBytes(rich.text);
stream.close();
});
</s:click>
</s:Button>
<s:click>
var f:File = File.desktopDirectory;
f.browseForOpen("Select file to open", []);
f.addEventListener(Event.SELECT, function (event:Event):void {
var fs:FileStream = new FileStream();
fs.open(event.target as File, FileMode.READ);
rte.htmlText = fs.readUTFBytes(fs.bytesAvailable);
ta.text = fs.readUTFBytes(fs.bytesAvailable);
fs.close();
});
</s:click>
</s:Button>
Some suggestions?
You already read all the stream from the fs.
As soon as you execute once fs.readUTFBytes(fs.bytesAvailable); the read header/pointer will be indicating to your end of your file, so in the next line the bytesAvailable will be zero.
I suggest you use the following code:
<s:click>
var f:File = File.desktopDirectory;
f.browseForOpen("Select file to open", []);
f.addEventListener(Event.SELECT, function (event:Event):void {
var fs:FileStream = new FileStream();
fs.open(event.target as File, FileMode.READ);
var dataStr:String = fs.readUTFBytes(fs.bytesAvailable);
rte.htmlText = dataStr;
ta.text = dataStr;
// if you want only the non formatted text use: ta.text = rte.text;
fs.close();
});
</s:click>

ActionScript POST

How to post data from a flex file to a php file? I am not able to create an action.
What you need is to create a URLRequest object where you set up your method and data to send. You then start the request with a Loader object.
var req:URLRequest = new URLRequest(yourURL);
req.method = URLRequestMethod.POST;
var vars:URLVariables = new URLVariables();
vars.yourVar = 'yourValue';
req.data = vars;
var ldr:Loader = new Loader();
ldr.load(req);
You need to create an HTTPService in order to send data to a server application like a PHP file from Flex. The data that is going to be sent could be an XML, that way in your PHP file you can parse that XML and get the information that is in it.
I use this function to transform my objects to XML and then send that XML in the HTTPService:
public function objectToXML(obj:Object, root:String):XML {
var qName:QName = new QName(root);
var xmlDocument:XMLDocument = new XMLDocument();
var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDocument);
var xml:XML = new XML(xmlDocument.toString());
return xml;
}
That way I create objects with normal properties and don't worry about how to create the XML, then when you are going to send the XML in the HTTPService you just call the method "objectToXML" on the send method of your HTTPService.
You do that like this:
var myData:Object=new Object();
myData.name="Information";
var myService:HTTPService = new HTTPService();
myService.url = "http://example.com/yourFile.php";
myService.method = "POST";
myService.contentType="application/xml";
myService.send(objectToXML(myData,"parent"));

Download a file in AIr,Flex

I need to download a file for example say, a pdf file from a url and store it in applicationsDirectory or ApplicationStorage directory , I have a code to download but its opening save dialog box to get the userinput for where to save the downloaded file.
This is the code that I am using
downloadURL.url = urlLocation;
configureListeners(file);
file.download(downloadURL);
I need to download the file with out opening any window, and file needs to be downloaded to ApplicationStorage directory.
Thanks in advance
You need the File and FileStream Classes... first load what you need as a binary URLLoader, retrieve the ByteArray, and then save it to the location you want with something like this:
var file:File = new File(File.applicationStorageDirectory.nativePath+"/myfile.jpg");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(bytes);
fileStream.close();
Where "bytes" is the bytearray you loaded...
var loader:URLLoader = new URLLoader ();
var request:URLRequest = new URLRequest ();
request.url = "http://" + "your file adress here";
loader.load(request);
loader.addEventListener(Event.COMPLETE , function (e:Event):void {
var file:File = new File(File.applicationStorageDirectory.nativePath+"/yourfile name here");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(loader.data);
fileStream.close();
});

How to Upload CSV file in flex?

i want to uplod csv file in to java from flex screen ? How can i do that ? is it possible,if it is please tell me any one ?
Use the FileReference class:
http://livedocs.adobe.com/flex/3/langref/flash/net/FileReference.html
var loader:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest(serverUrl);
req.data = csvString;
req.method = URLRequestMethod.POST;
loader.load(req);

Resources