SamWM
SamWM

Reputation: 5356

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.

Upvotes: 5

Views: 10725

Answers (3)

Ben Cull
Ben Cull

Reputation: 9514

This question's a little old now, but for the sake of future visitors I've written a blog post about how to return a regular MVC view as a Word Document.

http://benjii.me/2014/01/download-an-asp-net-mvc-view-as-a-word-document/

The core piece of this functionality comes from the WordDocumentAttribute Action Filter I created: https://gist.github.com/bjcull/8702230

Upvotes: 4

Mason240
Mason240

Reputation: 3034

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

Upvotes: 4

queen3
queen3

Reputation: 15511

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.

Upvotes: 5

Related Questions