Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor View To Word Document or PDF

How can I generate a Word Document or a PDF using a razor view (for determining how it is layed out) in ASP.NET MVC 3? Probably through the use of something like OpenXML SDK (for WordprocessingML) or iTextSharp (for PDF). I want to enable those that know HTML to be able to change the look of a generated Word Document or PDF without needing to write any C# code.

like image 203
SamWM Avatar asked Dec 12 '22 09:12

SamWM


2 Answers

I had a similar question, and ended up using http://code.google.com/p/wkhtmltopdf/

It generates PDF from HTML (that you can render using the usual Razor view). The result is very good. You may need to render you view into memory string, see here.

As for doc, you can specify content type to be Microsoft Word while sending HTML inside, Word will accept and convert it. E.g. see here.

If you want users to change the final document, you can render you view and set contenteditable for the resulting HTML. E.g.

 <div contenteditable>GENERATED HTML</div>

Or use something like TinyMCE. Then, when users post back this HTML, you use it to pass to wkhtmlpdf and you get PDF.

like image 55
queen3 Avatar answered Dec 29 '22 09:12

queen3


I just implemented Rotavia into my project. It took about 5 minutes to get working.

like image 45
Mason240 Avatar answered Dec 29 '22 08:12

Mason240