1

I have found very interesting way how to render PDF from XML views using iTextSharp transformation. Unfortunately this is based on iTextSharp 4.x and now the last version is 5.x which dropped support for ITextHandler.

I tried to rewrite the example to iTextSharp 5.x. I believe I am pretty close, but I don't have experience with iTextSharp to finish it. Something is wrong in my code. It throw the exception:

An exception of type 'System.IO.IOException' occurred
in itextsharp.dll but was not handled in user code
Additional information: The document has no pages.

Following is my modification of code provided by article:

/// <summary>
/// Returns a PDF action result. This method renders the view to a string then
/// use that string to generate a PDF file. The generated PDF file is then
/// returned to the browser as binary content. The view associated with this
/// action should render an XML compatible with iTextSharp xml format.
/// </summary>
/// <param name="model">The model to send to the view.</param>
/// <returns>The resulted BinaryContentResult.</returns>
protected ActionResult ViewPdf(object model)
{
    // Render the view xml to a string, then parse that string into an XML dom.
    string xmltext = this.RenderActionResultToString(this.View(model));

    using (MemoryStream outStream = new MemoryStream())
    {
        Document pdfDocument = new Document();
        PdfWriter pdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDocument, outStream);
        pdfDocument.Open();

        //pdfDocument.Add(new Paragraph("Hello World!")); - this works

        using (StringReader stringReader = new StringReader(xmltext))
        {
            //this does not works
            XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, pdfDocument, stringReader);
        }

        pdfDocument.Close();                
        pdfWriter.Flush();           
        byte[] buffer = outStream.ToArray();              

        // Send the binary data to the browser.
        return new BinaryContentResult(buffer, "application/pdf");
    }
}

And finally I simplified the View:

@model int
@{
    Layout = null;
}<?xml version="1.0" encoding="UTF-8" ?>
<itext creationdate="2/4/2003 5:49:07 PM" producer="iTextSharpXML">
    <paragraph leading="18.0" font="unknown" size="16.0" align="Default">
        <chunk>Generated PDF</chunk>
    </paragraph>
    <paragraph leading="18.0" font="unknown" size="10.0" align="Default">      
        <chunk>Hello wolrd: @Model</chunk><newline />
    </paragraph>   
</itext>

The rest of code is not interesting and is in the linked article.

Where is the problem? The ASP.NET MVC parts work, the view in XML is generated into variable xmltext correctly. The problem is generating string XML to bytes by XMLWorkerHelper. What is wrong?

UPDATE:

The problem is that the XHtml parser expect HTML file and not XML file with itext format as it works in itextSharp 4.x with ITextHandler.

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
  • 1
    The thing that you are trying to use is based on iText's DTD that was created during the "xml is awesome" phase of the internet. (I think we're in the JSON phase right now.) Anyway, iText's DTD was proprietary and it was dropped in favor of an internationally agreed upon and very widely used XML standard for document markup, XHTML. See [this post](http://stackoverflow.com/a/21679509/231316) for more. – Chris Haas Aug 10 '15 at 13:33
  • I see, that is the reason. – Tomas Kubes Aug 10 '15 at 14:09

0 Answers0