converter.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | var apiKey, formData, toType, isInline; $(document).ready( function () { $( "#resultBlock" ).hide(); $( "#errorBlock" ).hide(); $( "#result" ).attr( "href" , '' ).html( '' ); }); $(document).on( "click" , "#submit" , function () { $( "#resultBlock" ).hide(); $( "#errorBlock" ).hide(); $( "#inlineOutput" ).text( '' ); // inline output div $( "#status" ).text( '' ); // status div formData = $( "#form input[type=file]" )[0].files[0]; // file to upload toType = $( "#convertType" ).val(); // output type isInline = $( "#outputType" ).val() == "inline" ; // if we need output as inline content or link to output file $( "#status" ).html( 'Requesting presigned url for upload... <img src="ajax-loader.gif" />' ); $.ajax({ type: 'GET' , headers: { 'x-api-key' : apiKey }, // passing our api key success: function (result) { if (result[ 'error' ] === false ) { var presignedUrl = result[ 'presignedUrl' ]; // reading provided presigned url to put our content into $( "#status" ).html( 'Uploading... <img src="ajax-loader.gif" />' ); $.ajax({ url: presignedUrl, // no api key is required to upload file type: 'PUT' , data: formData, processData: false , success: function (result) { $( "#status" ).html( 'InProgress <img src="ajax-loader.gif" />' ); $.ajax({ url: 'https://api.pdf.co/v1/xls/convert/to/' + toType + '?url=' + presignedUrl + '&encrypt=true&inline=' + isInline + '&async=True' , type: 'POST' , headers: { 'x-api-key' : apiKey }, success: function (result) { if (result.error) { $( "#status" ).text( 'Error uploading file.' ); } else { checkIfJobIsCompleted(result.jobId, result.url); } } }); }, error: function () { $( "#status" ).text( 'error' ); } }); } } }); }); function checkIfJobIsCompleted(jobId, resultFileUrl) { $.ajax({ type: 'GET' , headers: { 'x-api-key' : apiKey }, // passing our api key success: function (jobResult) { $( "#status" ).html(jobResult.Status + ' <img src="ajax-loader.gif" />' ); if (jobResult.Status == "InProgress" ) { // Check again after 2 seconds setTimeout(checkIfJobIsCompleted(jobId, resultFileUrl), 2000) } else if (jobResult.Status == "Finished" ) { $( "#status" ).text( 'Done converting.' ); $( "#resultBlock" ).show(); if (isInline && toType != "pdf" ) { $.ajax({ url: resultFileUrl, dataType: 'text' , success: function (respText) { $( "#inlineOutput" ).text(respText); } }); } else { $( "#result" ).attr( "href" , resultFileUrl).html(resultFileUrl); } } } }); } |
Click here to get your Free Trial version of the SDK
IMPORTANT:
Cloud API is deprecated and was replaced with more powerful and secure www.PDF.co Web API
CLICK HERE
TO LEARN MORE
ABOUT NEW
www.PDF.co
w/ Web API
On-Premise API Server
Cloud API Server