5

i m using asp.net mvc2 for my application. i have a view that accepts a model and renders its data in some format. i want this page to be converted into pdf file so it can be sent via attachment or created and downloaded on the fly. i have some success creating pdf with itextsharp library but they say it has very basic support for html and css. if anyone has rendered complex html pages to pdf using itextsharp or any other library plz help me in solving this problem. any comments are highly appreciated

thanks

Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155

4 Answers4

3

I think this related question will help to a degree. Just note that HTML displays and PDF documents serve different purposes as described here. The basic thought is that the latter "is designed for print output", and the other "is designed for video display".

My suggestion is that in most cases when one prints from the web, there is an option to create a separate stylesheet for printing using this declaration. The idea is to dumb down the styling and make it more print friendly (=less complex).

The following article from A List Apart provides some further insight into this option.

<link rel="stylesheet" type="text/css" media="print" href="print.css" /> 

The other free PDF library to look at is PDFSharp. However, they do not support conversion from HTML to PDF.

Other related SO questions

Community
  • 1
  • 1
Ahmad
  • 22,657
  • 9
  • 52
  • 84
0

Although not free, you may want to look at the PDF Conversion Services. The latest beta supports HTML conversion with great precision.

Have a look at this post, it works well with .net and java based environments.

I worked on this product so the usual disclaimers apply. Happy to answer any questions.

Jeroen Ritmeijer
  • 2,772
  • 3
  • 24
  • 31
0

You could also look into using http://itextpdf.com/ which offers html to pdf conversion.

"iText can convert an XML or an HTML file to PDF, but only on a very basic level. Converting documents from one format to another is outside the scope of iText. And no: iText does not convert Word documents to PDF!"

Buildstarted
  • 26,529
  • 10
  • 84
  • 95
0

Capture the rendered view (html) as a string and pass it to Winnovative PDF creator which is a really good tool which preserves complex formatting. Here is the code to create the PDF from the captured html

    string test="\r\n\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\" >\r\n<head>\r\n <link rel=\"Stylesheet\" href=\"../../Content/style.css\" type=\"text/css\" />\r\n <title>Cover Page</title>\r\n <style type=\"text/css\">\r\n html, body\r\n {\r\n\t font-family: Arial, Helvetica, sans-serif;\r\n\t font-size: 13pt;\r\n\t padding: 0px;\r\n\t margin: 0px;\r\n\t background-color: #FFFFFF;\r\n\t color: black;\r\n\t width: 680px;\r\n }\r\n </style>\r\n</head>\r\n<body>\r\n <div>\r\n Ssotest Ssotest, \r\n </div> \r\n</body>\r\n</html>\r\n"

               FileStreamResponseContext response = new FileStreamResponseContext();
Response .clear();
     Document doc = new Document();
                doc.DocumentInformation.CreationDate = DateTime.Now;
                doc.DocumentInformation.Title = "Test Plan";
                doc.DocumentInformation.Subject = "Test Plan";
                doc.CompressionLevel = CompressionLevel.NormalCompression;
                doc.Margins = new Margins(0, 0, 0, 0);
                doc.Security.CanPrint = true;
                doc.ViewerPreferences.HideToolbar = false;
                doc.ViewerPreferences.FitWindow = false;

    string baseUrl = String.Format("http://localhost{0}/", Request.Url.Port == 80?"":":" + Request.Url.Port.ToString());

    PdfPage docTestPlan = doc.AddPage(PageSize.Letter, new Margins(0, 0, 0, 0), PageOrientation.Portrait);
    // passing the string test returned from the string writer

       HtmlToPdfElement htmlToPdf = new HtmlToPdfElement(test, baseUrl);
                htmlToPdf.FitWidth = false;
                docTestPlan.AddElement(htmlToPdf);


                /******************************************
                 * put doc in a memory stream for return */
                response.FileDataStream = new MemoryStream();
                doc.Save(response.FileDataStream);
                doc.Close();
                response.FileDataStream.Position = 0;

                return new FileStreamResult(response.FileDataStream, "application/pdf");



If you need to capture the controller action follow my post here.[http://stackoverflow.com/questions/5553674/capturing-html-to-string-in-mvc2-0][1]

You may get the sample codes from the official website of Winnovative PDF creator.

Gokul
  • 1,361
  • 3
  • 19
  • 31