[RDMLX/JS] Create PDF from AJAX response attachment

Please do not use to report errors- use your regional help desk.
Please mark posts as being for RPG or RDMLX (LANSA) developer.
To subscribe by email, display this forum, scroll to the end and select ‘Subscribe Forum’.
Post Reply
edwardtn
Posts: 33
Joined: Fri Aug 28, 2015 11:53 pm

[RDMLX/JS] Create PDF from AJAX response attachment

Post by edwardtn »

I am trying various methods of creating PDF documents for printing/emailing from within LongRange. I am making and AJAX call and have a PDF generated in a cloud service which is encoded in the HTTP response. If I make the call with a synchronous form, the PDF is opened in the current webview...what I would like to do instead is take the raw response data and use the LocalFiles class to write the PDF file and then use the UI class to open the saved document. This would allow for the PDF to be shared/printed using the native handler.

Is there something I am missing to be able to create the binary PDF file? Sample code is below:

Code: Select all

        var apiUrl = "http://api.pdflayer.com/api/convert?access_key=84c03d10fbfd249c76da02b672cfc06a&test=1";
        var pdfHTML = "<h2>Test Printout</h2><br><p style='color: red;'>How can I save this file?</p>";
	if (typeof LONGRANGE != "undefined") {
		$.ajax({
			method: 'POST',
			url: apiUrl,
			data: 'document_html=' + pdfHTML,
			dataType: "text",
			contentType: "application/x-www-form-urlencoded",
			success: function(pdfData) {
				var pdfPath = "/mobile/pictures/test.pdf";
				LONGRANGE.LocalFiles.writeFile( {
					data: pdfData,
					path: pdfPath,
					type: "binary", 
					dataEncoding: "base64",
					onCompleted: function(result) {
						if ( result.status != LONGRANGE.Status.Ok ) {
							alert(result.message); 
							return;
						}
						LONGRANGE.UI.openDocument({
							file: pdfPath,
							mode: "open",
							onCompleted: function(result) {
								if ( result.status != LONGRANGE.Status.Ok ) {
									alert(result.message); 
									return;
								}
							}
						});
					}
				} );
			}
		})
	}
 
tsupartono
Posts: 289
Joined: Wed Apr 18, 2012 10:21 am

Re: [RDMLX/JS] Create PDF from AJAX response attachment

Post by tsupartono »

Below works for me on iOS.

We can also add a native function to download a remote file and save it to the local file system, which would eliminate any cross domain issues and performance issue related to base-64 encoding for large PDF files. If you'd like the enhancement, please log a request with LANSA Support.

Code: Select all

// Get PDF as binary data (overrideMimeType)
var url = "http://internal.lansa.com.au/exam0317.pdf";
var xhr = new XMLHttpRequest();
xhr.open("get", url, false);
xhr.overrideMimeType('text\/plain; charset=x-user-defined');
xhr.send(null);

// Convert bytes to 64 encoded string
var bytes = [];
var response = xhr.responseText;            
for(var i = 0; i < response.length; i++)
{
    var byte = response.charCodeAt(i) & 0xFF;
    bytes.push(String.fromCharCode(byte));
}
bytes = bytes.join("");
var base64Data = btoa(bytes);

//            
var pdfPath = "/temp/1.pdf";
LONGRANGE.LocalFiles.writeFile( 
{
    data: base64Data,
    path: pdfPath,
    type: "binary", 
    dataEncoding: "base64"
})
.then(function()
{
    return LONGRANGE.UI.openDocument(
    {
        file: pdfPath,
        mode: "open"
    });
})
.catch(function(e)
{
    alert(e.message);
});
Post Reply