tags:

views:

280

answers:

1

I've got a two part question about using iTextSharp. I've built a simple MVC application to store information about "lessons learned" in a SQL Server database. When a user is looking at the details for a lesson I want them to be able to save the lesson as a PDF. I've got some working code but 1) I'm not sure how sound my approach is and 2) the MVC application uses a TinyMCE rich text editor and when I put the rich text into the PDF the html tags are being displayed. How do I get the PDF to honor the html formatting (bold fonts, unordered lists, paragraphs, etc.)?

Below is the code I'm using to generate the PDF. I would really appreciate feedback if I'm going about this incorrectly.

Thanks.

public FilePathResult GetPDF(int id)
    {
        Lesson lesson = lessonRepository.GetLesson(id);

        string pdf = @"C:\Projects\Forms\LessonsLearned\PDF\template_test.pdf";
        string outputFilePath = @"C:\Temp\pdf_output\test_template_filled.pdf";

        PdfReader pdfReader = null;

        try
        {

            pdfReader = new PdfReader(pdf);

            using (FileStream pdfOutputFile = new FileStream(outputFilePath, FileMode.Create))
            {
                PdfStamper pdfStamper = null;
                try
                {
                    pdfStamper = new PdfStamper(pdfReader, pdfOutputFile);

                    AcroFields acroFields = pdfStamper.AcroFields;

                    acroFields.SetField("title", lesson.Title);
                    acroFields.SetField("owner", lesson.Staff.FullName);
                    acroFields.SetField("date", lesson.DateEntered.ToShortDateString()); 
                    // field with rich text                       
                    acroFields.SetField("situation", Server.HtmlDecode(lesson.Situation));
                    acroFields.SetField("description", Server.HtmlDecode(lesson.Description));

                    pdfStamper.FormFlattening = true;

                }
                finally
                {
                    if (pdfStamper != null)
                    {
                        pdfStamper.Close();
                    }
                }
            }
        }
        finally
        {
            pdfReader.Close();
        }

        return File(outputFilePath, "application/pdf", "Lesson_" + lesson.ID + ".pdf");

    }
+1  A: 

The only major component of your approach I'd change is how the data is returned. Right now you are using a fixed file path, which means that two people making requests at the same time will result in one of them getting an error. Since you are not saving the file for later use, I would skip the FileStream entirely and use a MemoryStream. You can then use FileStreamResult to return the stream with a MIME type of application/pdf.

For the second part you will have some trouble. PDF and HTML are not related, so HTML tags (which are just plain text) have no special meaning in a PDF document. If you want to convert a users HTML (generated by your rich text control) into suitable PDF rich text you will need an HTML parser in the middle.

iText includes the HTMLWorker class, which is a partial HTML parser (meaning it won't handle all html tags or structures) designed to return PDF compatible chunks. You could also use something like the HTMLAgilityPack to have more control over which tags are converted, but you'd then have to do the translation yourself. You could also examine your rich edit control, to see if it can return rich text in an easier to parse format.

David
Thanks for the reply David. Yeah, I was worried about the FileStream. I want to be able to open a template PDF and populate it with data from my database but, as you say, I don't need to save the resulting file for later use. Do you have any example code you could share? I'm just kind of hacking this together.On the HTML tag front - I will try the HTMLWorker class.
I've got the HTMLWorker class parsing the HTML but what I don't understand is if it's possible to format the text in the PDF so that it looks similar to how it looks in HTML... need advice please!