Click here to Skip to main content
15,911,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
i'am using ITextsharp library to convert html url to create pdf file. and i have on the head and body online links with style sheets and java script functions.
when i used the below code,
file has no sense with those style sheets and javascript ,

any ideas to solve this problem

C#
public void Text2PDF(string PDFText)
   {
       //HttpContext context = HttpContext.Current;
       StringReader reader = new StringReader(PDFText);
       //Create PDF document
       Document document = new Document(PageSize.A4);
       HTMLWorker parser = new HTMLWorker(document);
       string PDF_FileName = Server.MapPath("PDFddddd_File.pdf");
       PdfWriter.GetInstance(document, new FileStream(PDF_FileName, FileMode.Create));
       document.Open();
       try
       {
           parser.Parse(reader);
       }
       catch (Exception ex)
       {
           //Display parser errors in PDF.
           Paragraph paragraph = new Paragraph("Error!" + ex.Message);
           Chunk text = paragraph.Chunks[0] as Chunk;
           if (text != null)
           {
               text.Font.Color = BaseColor.RED;
           }
           document.Add(paragraph);
       }
       finally
       {
           document.Close();
           DownLoadPdf(PDF_FileName);
       }
   }
Posted

Hi,

I tried Different way


Using images.websnapr.com - 'it help to take screen shot of web page with style and return an Image '

Then, we can add this to PDF


HttpWebRequest request = HttpWebRequest.Create("http://images.websnapr.com/?size=s&url=http%3A%2F%2Fwww.google.com") as HttpWebRequest;
    Bitmap bitmap;
    using (Stream stream = request.GetResponse().GetResponseStream())
    {
        bitmap = new Bitmap(stream);
         bitmap.save(imagepath + "/img1.jpg");
    }


    Image img= Image.GetInstance(imagepath + "/img1.jpg");
    doc.Add(img);


Thanks

Siva Rm K
 
Share this answer
 
Comments
NNos2012 4-Jun-13 7:42am    
did this code use ITextSharp also???
rmksiva 4-Jun-13 8:29am    
Yes ! You can add this set of code into your Code - below to

document.Add(paragraph); Line
Try doing HTML to pdf, you can include CSS in html.
 
Share this answer
 
Comments
NNos2012 4-Jun-13 6:29am    
i can't because i take this html from online url and style sheets loads from online too

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900