I'm looking for a way to take a view in my MVC site and save it to a PDF and save it locally on disk. I have found a lot of answers that are similar to what I'm asking, but none of them actually answer it. How can I do this?
3 Answers
I've used WkHtmlToPdf before with good success.
It's a command line tool that you can install on your server.
6 Comments
CMS
does it save with all the css styles applied? and hide the elements that are hidden on print?
Johnie Karr
Yes. It renders the URL you give it and then generates a PDF based on what is rendered. Same as if you had a PDF Print driver installed and printed it locally to a pdf that way from the web browser...except this sits on your server.
CMS
do they have a nuget package? or i need to install something in my pc?
Johnie Karr
I found
Codaxy.WkHtmlToPdf on nuget, but the Codazy prefix throws me off, don't know if it's a port or from the WkHtmlToPdf folks.CMS
i tried Rotativa problem is that i don't see an option to save it on disk
|
I have used Rotativa, it worked great
Rotativa
The implementation is quite simple ,very few lines of code.
For example you would need just these lines to render view as a pdf
public ActionResult PrintInvoice(int invoiceId)
{
return new ActionAsPdf(
"Invoice",
new { invoiceId= invoiceId })
{ FileName = "Invoice.pdf" };
}
For more info
Code Project
Comments
I've used the Aspose.pdf library (not free) for this in the past.
I believe this blog post here gives a very in-depth explanation of how to do it.
http://daveaglick.com/posts/using-aspnet-mvc-and-razor-to-generate-pdf-files