2

I am trying to find a library/tool that would allow me to generate PDF from MVC views.

Any ideas, suggestions?

Regards

sTodorov
  • 5,435
  • 5
  • 35
  • 55

3 Answers3

3

use iTextSharp for C# which is ported from iText from JAVA

check out ITextSharp HTML to PDF?

Community
  • 1
  • 1
Mujah Maskey
  • 8,654
  • 8
  • 40
  • 61
  • Thanks. I've created a small test project with this one however it throws a FormatException when I pass in the html. – sTodorov Jul 12 '11 at 07:20
1

I've created a project for doing this using iTextSharp, and published on bitbucket.

I override a ViewResult, to return a Pdf stream to the client. The pdf is generated in a MVC View (.cshtml file). And added some Extensions for the controller to make the experience smooth.

I have two options:

  • Create the Pdf from a "normal" view that returns Html. It's very easy, but you do not have "fine" control over the layout.
  • Create a Pdf using code, the layout code is in the View (using server side code). This is easy for customization, and testing. Because you do not need to recompile the whole project. Also IMHO the View (.cshtml file) is responsible for the layout, if the result is html or pdf does not mather, its still the view.
GvS
  • 52,015
  • 16
  • 101
  • 139
0

You will need to call RenderView to get the HTML result and then convert to PDF. Check this entry out for a similar solution

http://www.jimzimmerman.com/blog/2009/10/06/PdfResult+A+Custom+ActionResult+In+ASPNET+MVC.aspx

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • I see that this one is using the "expert pdf to html" library which is the one I am currently using. However, I experience loss in quality of embedded images. That is why I wanted to test with other ones. – sTodorov Jul 12 '11 at 07:19
  • do you care if its purchased or not? – Adam Tuliper Jul 12 '11 at 16:20