Creating pdf from .net mvc view

im using asp.net mvc2 for my application. I have a view that takes a model and displays its data in some format. I want this page to be converted to a PDF file so that it can be sent through the application or created and downloaded on the fly. I have some success creating pdf with the itextsharp library, but they say that it has very basic support for html and css. if someone provided complex html pages in pdf using itextsharp or any other plz library, help me solve this problem. any comments are greatly appreciated

thank

+5
source share
4 answers

I think this related question will help to some extent. Just note that HTML displays and PDFs serve different purposes, as described here . The main idea is that the latter is "designed for printing", and the other is "designed to display video."

My suggestion is that in most cases when you are printing from the Internet, it is possible to create a separate style sheet for printing using this declaration. The idea is to envelop the style and make it more print friendly (= less complicated).

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

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

PDF - PDFSharp. HTML PDF.

SO

+3

, PDF. HTML.

, .net java.

, . .

0

http://itextpdf.com/, html pdf.

"iText XML HTML PDF, . iText. : iText Word PDF!"

0

(html) Winnovative PDF Creator, , . PDF 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]

Winnovative PDF.

0

All Articles