NodeJS AJAX Ubuntu service
NodeJS AJAX Ubuntu service
by Luis Valle
HTML
<div id="dvHTML">
<div id="dvPDFarea">
<div style="padding:8px;border:1px solid orange;background-color:yellow;text-align:center;">
<span style="font-weight:bold;">Welcome to my HTML to PDF Example</span>
<img src="http://www.evicore.net/Src/images/icon-pdf-small.png">
</div>
<div style="height:20px;"></div>
<div>
First Name: <textarea id="txtFirstName" rows="1" cols="18"></textarea>
<select>
<option value="">(Gender)</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
<div>
Last Name: <input type="text" id="txtLastName">
<span style="margin-left: 15px;color:blue;">I feel:</span>
<input type="radio" name="moodNow" value="Happy">Happy
<input type="radio" name="moodNow" value="Sad" style="margin-left: 15px;">Sad
</div>
<p>
The logic behind this approach is to first capture all the desired html and pass it to my ASMX Web Service which converts it to a functional PDF and pushes it back to the Client as a download.
</p>
<p>
</p><div style="font-weight:bold;">
Passing Parameters to Format your PDF file
</div>
<div style="padding:8px;margin-left:20px;border:1px solid black;background-color:#FFFFD9;display:inline-block;">
<b>fileformat</b> - "specifies the format your HTML...
JavaScript
$(document).ready(function () {
var parm = {
docType: 'doc',
inHTML: $('#dvHTML').html()
}
AJAXcall('convertHtmlToFile', parm)
.then(function (tblRes) {
if (tblRes.error == 1) {
alert(tblRes.errorCode);
} else {
//alert(JSON.stringify(tblRes.row[0]));
//alert(tblRes.row[0].length);
//alert(JSON.stringify(tblRes.row[0][0]));
downloadFile(tblRes.fileName, tblRes.docType);
}
});
});
function downloadFile(fileNamex, extension) {
$('body').append("<form method='POST' action='' style='position:fixed;top:-3333333333px;'" +
" id='tempForm'><input" +
" type='submit' value='Submit' style='opacity:0;' class='HiddenBtn' /></form>");
$('#tempForm').attr('action', 'https://www.morningsidemovers.com');
var data = JSON.stringify({
"tFunction": 'downloadFile',
'params': {
'fileName': fileNamex,
'docType': extension
}
})
$('<input type="hidden" name="json"/>').val(data).appendTo('#tempForm');
$('#tempForm').submit().remove();
}
function AJAXcall(aFunction, spParams) {
jQuery.support.cors = true;
var d = $.Deferred();
$.ajax({
crossDomain: true,
url: "https://morningsidemovers.com",
data: JSON.stringify({ "tFunction": aFunction, "params": spParams }),
dataType: 'json',
type: 'POST',
contentType: 'application/json'
})
.done(function (response) {
d.resolve(response);
})
.fail(function (jqXHR, textStatus, errorThrown) {
errorThrown = (errorThrown != '' ? 'Ajax communication failed!\n\n(Error Thrown: "' + errorThrown + '")' : '');
d.resolve({ error: '1', errorCode: errorThrown });
});
return d.promise();
}