3

I am trying to generate some PDFs from an MVC view and it's been quite a ride. I eventually came across this and am trying to use it but it has this strange markup as shown below (it's based on ITextSharp):

<row>
    <cell borderwidth="0.5" left="false" right="false" top="false" bottom="true">User Name</cell>
    <cell borderwidth="0.5" left="false" right="false" top="false" bottom="true">Description</cell>
    <cell borderwidth="0.5" left="false" right="false" top="false" bottom="true" horizontalalign="right">Lucky Number</cell>
    <cell borderwidth="0.5" left="false" right="false" top="false" bottom="true" horizontalalign="right">Doubled</cell>
</row>

Source

All I am really trying to do at this point is set the width of the column (cell, as it's called here) but cell isn't accepting my style: width or width or anything.

How can I use this thing? Are there any better ways to render PDF from MVC view?

Carsten
  • 11,287
  • 7
  • 39
  • 62
user1477388
  • 20,790
  • 32
  • 144
  • 264

2 Answers2

1

The simplest thing to do would be to create your view in html and convert it to a pdf then send that as your action result.

Simple way to get the html of a razor template http://razorengine.codeplex.com/

string template = "Hello @Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });

Pdf conversion http://www.essentialobjects.com/Products/EOPdf/Default.aspx

DalSoft
  • 10,673
  • 3
  • 42
  • 55
  • That costs like $600... Probably why I've never heard of it until now. Got anything cheaper? – user1477388 Mar 07 '13 at 15:44
  • 1
    From http://stackoverflow.com/a/2129296/324497 you could try wkhtmltopdf. There are also other solutions offered in for that question. – DalSoft Mar 07 '13 at 15:51
  • 2
    Look like it's XML using the iText convention. http://stderr.org/doc/libitext-java-doc/www/tutorial/ch07.html Unless you have a reason for doing it with XML I'd avoid the extra complexity and go with html to pdf conversion. – DalSoft Mar 07 '13 at 15:57
1

Like others have said this is iTextSharp xml. Here is a post that posts list the valid XML elements. How to create tables in a PDF document using XML and iTextSharp?

It looks like you could potentially use CDATA tags to set the width.

Community
  • 1
  • 1
Jamie
  • 3,094
  • 1
  • 18
  • 28
  • That seems so annoying... I feel like setting width of an element is like the simplest thing; it shouldn't be so difficult. – user1477388 Mar 08 '13 at 15:30
  • 1
    iTextSharp is, IMO, very difficult to work with. It is not that straightforward if you did it in C# either. – Jamie Mar 08 '13 at 15:33
  • Is there something you recommend? I am trying to use wkhtmltopdf, now, but I am having trouble with the .EXE and am hoping I can use a .DLL, instead http://stackoverflow.com/questions/15285363/running-exe-from-mvc – user1477388 Mar 08 '13 at 15:44
  • wkhtmltopdf would be the option I would recommend. – Jamie Mar 08 '13 at 16:52