1

I'm using MVC3 and iTextSharp.

At First a added a Save and CreatePDF Button. With the PDF Button I call a Method which returns me the actual HTML Code of the shown WebPage.

So far so good, now I started to generate a PDF with iTextSharp. I was wondering, it worked on the first try, but after I added a MVC Grid Table to the with iTextSharp still creats a PDF, but with an Error Message in it.

ERRORURI-Formate are not supported.

Any Suggestions to bring this to work?

The Grid is built like that

        @Html.DataGridFor(m => m.ConfigurationArticleList, ItemContainerType.tr,
        _S.H<ConfigurationArticle>(
        @<text>         
             <td class="ToDo">
                @item.ValidationMessageFor(m => m.ArtNr, "*")
                @item.TextBoxFor(m => m.ArtNr)
             </td>
             <td class="ToDo">
                @item.ValidationMessageFor(m => m.Bezeichnung, "*")
                @item.TextBoxFor(m => m.Bezeichnung)
             </td>
             <td class="ToDo">
                @item.ValidationMessageFor(m => m.Anzahl, "*")
                @item.TextBoxFor(m => m.Anzahl)
             </td>
        </text>
        ),
         _S.H<ConfigurationArticle>(
        @<text>         
            <td class="ToDo">
                @item.ValidationMessageFor(m => m.ArtNr, "*")
                @item.DisplayField(m => m.ArtNr)
            </td>
            <td class="editor-field">
                @item.ValidationMessageFor(m => m.Bezeichnung, "*")
                @item.DisplayField(m => m.Bezeichnung)
            </td>
            <td class="ToDo">
                @item.ValidationMessageFor(m => m.Anzahl, "*")
                @item.DisplayField(m => m.Anzahl)
            </td>
        </text>

Furthermore the FilePath for the PDF is still hardcoded, any idea to get a SaveFile-Dialog or something else?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ICantSeeSharp
  • 171
  • 2
  • 13
  • `iTextSharp` doesn't officially support converting HTML to PDF and certain functions simply don't work. You will have to use another framework to do the conversion if you want all HTML and CSS features to be supported. – Darin Dimitrov Jun 21 '12 at 08:22
  • [This](http://www.codeproject.com/Articles/66948/Rendering-PDF-views-in-ASP-MVC-using-iTextSharp) or [this](http://stackoverflow.com/questions/6660093/html-to-pdf-asp-net-mvc) can help. – B. Bilgin Jun 21 '12 at 08:43

2 Answers2

2

ABCpdf - is a good tool to do even more. We do prefer it in our projects and it works without problem. ABCpdf

  • it seems to be a good choice to go with. Also there is "itext pdf" open-source generator tool, that worse to look. – Yusubov Jun 21 '12 at 13:29
0

I'm suggesting you to use wkhtmltopdf instead of ITextSharp. It is an opensource executable file which is using web kit rendering engine. It produces very good pdf output. If you want to use it from a mvc web site then you can run it with following code:

var procStartInfo = new ProcessStartInfo("wkhtmltopdf.exe", "input.html output.pdf");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();

It will create output on your server's file system, but you can easily read it and write to http response.

Response.ContentType = "Application/pdf";
Response.AddHeader("Content-Type", "application/pdf");
Response.AddHeader("Content-Disposition", "attachment; filename=output.pdf");
string filePath = "output.pdf";
Response.WriteFile(filePath);
Response.End();
Marian Ban
  • 8,158
  • 1
  • 32
  • 45