[RDMLX/JS] Create PDF from AJAX response attachment
Posted: Wed Nov 23, 2016 7:46 am
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:
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;
}
}
});
}
} );
}
})
}