PDF direct download with jQuery
There was a requirement in my project that needs to download a pdf out of html content. After some research we found that princeXML would be a good candidate for html to pdf conversion process as it provides a good api for pdf designing as we want.(Although we had to purchase a license). This blog post isn't about the princeXML but the front-end and back-end implementation of pdf direct download.
Challenges
- The HTML content that we wanted to covert to a pdf was quite big. Due to various browser limitations on GET request's length, we couldn't just use a simple GET request.
- Even though we have set up the correct response headers for our POST request browser seems to have not downloading the content as a pdf. (Ajax is not suitable).
So the options we had were, either to use a hidden iframe submission or build a dynamic form and submit it on the fly. We chose form submission method as it's the best option.
Step 01This is the client side JavaScript code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var createPdfReport = function () { | |
var reportData = $(".report-data").html(); | |
var data = { | |
htmldata: reportData, | |
path: "some more data" | |
} | |
$.download("apps/reports", data); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.download = function (url, data, method) { | |
if (url && data) { | |
data = typeof data == 'string' ? data : jQuery.param(data); | |
var inputs = ''; | |
jQuery.each(data.split('&'), function () { | |
var pair = this.split('='); | |
inputs += '<input type="hidden" name="' + pair[0] + '" value="' + pair[1] + '" />'; | |
}); | |
jQuery('<form action="' + url + '" method="' + (method || 'post') + '">' + inputs + '</form>').appendTo('body').submit().remove(); | |
}; | |
}; |
Following is the server side C# code. We have to set relevant headers for the response.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeController : Controller | |
{ | |
[AllowAnonymous] | |
[HttpPost] | |
public void PdfReport([FromBody] string path, [FromBody] string htmldata) | |
{ | |
HttpResponse response = System.Web.HttpContext.Current.Response; | |
response.Clear(); | |
response.ClearHeaders(); | |
response.Clear(); | |
response.AddHeader("Content-Type", "application/pdf"); | |
// pdfBytes come from the prince html string conversion | |
response.AddHeader("Content-Length", pdfBytes.Length.ToString()); | |
response.AddHeader("Content-Disposition", "attachment; filename=report.pdf"); | |
response.BinaryWrite(pdfBytes); | |
response.Flush(); | |
response.End(); | |
} |
That's it. As soon as the browser agent detects the response header as "application/pdf" it will directly download the pdf document.
Cheers!
This comment has been removed by the author.
ReplyDeleteI think you did an dbrau ba 3rd year result awesome job explaining it. Sure beats having bu ba final year result to research it on my own. Thanks
ReplyDeleteGreat post. Thanks for sharing your brain. sooryavanshi release date Check mine out sometime jungle cruise full movie download 720p
ReplyDeleteBonus Codes 2021 - BonusBets777
ReplyDeleteFor the best bonus codes 1xbet download and promotions, you need to keep in mind 실시간 배당 that the game can be played for free 22bet or for real money without any risk, 1x bet all without risk ibet789 sports betting asian handicap