-2

How to convert .aspx page with css class design to pdf format which contains asp.net controls.

I have used some code but its not covert the page with proper allignment and design.

    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    a.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();

I have used this code..

Can you help me.please.

user3575611
  • 1
  • 1
  • 4
  • Define "proper allignment and design". What does this code do and how exactly does that not match your expectations? – CodeCaster Jul 04 '14 at 10:19

2 Answers2

0

One of the easiest ways would be to use the wkhtmltopdf library/tool

Running a command like this:

wkhtmltopdf http://yoursite.com/yourpage.aspx yoursit-1970-01-01.pdf

Will create a pdf render of your url using the WebKit enginge. It might be best to run this outside of the ASP.NET request. For example store the url's you want to render in a database and a have scheduled job convert all those urls to PDFs. Of course if you have to serve the file straight away, this is not an option.

Using the C library will probably be the best solution if you are going to do this inside a request.


Edit:

It seems that there is already a .NET wrapper written for the library: https://github.com/gmanny/Pechkin

data
  • 2,563
  • 1
  • 21
  • 25
0

You need to apply the correct style using XMLWorkerHelper and CSSResolver YOu can refer the following threads to fix this.

CSS styles not being applied to PDF with iTextSharp

Cannot get CSS to work in iTextSharp (5.4.3) when making pdf

First you need to check whether your stylesheet is applied properly or not. You can try this by removing all styles but keeping one css let's say

p
{
   color:red;
}

iTextSharp removes/overrides some styles from the stylesheet. You can have seperate css file for the pdf and apply styles one by one. You cannot convert the aspx page as it is to the pdf using iTextSharp library.

Community
  • 1
  • 1
Kiran Hegde
  • 3,651
  • 1
  • 16
  • 14