PDF direct download with jQuery


Hi guys,

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
  1. 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.
  2. 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 01

This is the client side JavaScript code. $.download("apps/reports",data) looks like an ajax request but it's actually not. This is coming from a simple plugin that we have used. The following plugin allows you to make a request that returns a file in a similar syntax to jQuery's native Ajax functions.

Step 02

Following is the server side C# code. We have to set relevant headers for the response.
That's it. As soon as the browser agent detects the response header as "application/pdf" it will directly download the pdf document. 


Cheers!

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. I 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

    ReplyDelete
  3. Bonus Codes 2021 - BonusBets777
    For 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

    ReplyDelete

Post a Comment

Popular posts from this blog

Dependency Injection in ASP.NET MVC 5 with StructureMap

Real-time web with Node.js & Socket.io

First Look at AngularJS 2.0