tags:

views:

73

answers:

1

I need to create a pdf file from the HTML provided from database which was stored from a editor...

i have to show the corresponding HTML with all the styles in the pdf...please help

I have uses itextsharp method but i am not getting the correct content i provided in the editor when i convert it to pdf,

Code i used

    string content = "<the html content from database>";

    StringReader reader = new StringReader(content);
    MemoryStream ms = new MemoryStream();
    Document doc = new Document(PageSize.A4,50,50,30,30);
    HTMLWorker parser = new HTMLWorker(doc);
    PdfWriter.GetInstance(doc, ms);
    doc.Open();
    try
    {
        parser.Parse(reader);
    }
    catch(Exception ex)
    {
        Paragraph paragraph = new Paragraph("Error! " + ex.Message);
        paragraph.SetAlignment("center");
        Chunk text = paragraph.Chunks[0] as Chunk;
        doc.Add(paragraph);
    }
    finally
    {
        doc.Close();
    }
    Byte[] buffer = ms.GetBuffer();
    if (buffer != null)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-length", buffer.Length.ToString());
        Response.BinaryWrite(buffer);
    }

is anything wrong in this please help with code create pdf from html

A: 

Here is the code which is working for me.

    `using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Web;
     using System.Web.UI;
     using System.Web.UI.WebControls;
     using iTextSharp.text;
     using iTextSharp.text.pdf;
     using System.IO;
     using System.Collections;
     using System.Text;
     using iTextSharp.text.xml;
     using iTextSharp.text.html;

     public partial class Default2 : System.Web.UI.Page
     {
           protected void Page_Load(object sender, EventArgs e)
           {
            //create document
            Response.Write(Server.MapPath("."));
            Document document = new Document();
            try
            {
              //writer - have our own path!!!
               PdfWriter.GetInstance(document, new FileStream(Server.MapPath(".") +     "parsetest.pdf", FileMode.Create));
        document.Open();
        //html -text - kan be from database or editor too
        String htmlText = "<font  " +
    " color=\"#0000FF\"><b><i>Title One</i></b></font><font   " +
    " color=\"black\"><br><br>Some text here<br><br><br><font   " +
    " color=\"#0000FF\"><b><i>Another title here   " +
    " </i></b></font><font   " +
    " color=\"black\"><br><br>Text1<br>Text2<br><OL><LI>hi</LI><LI>how are u</LI></OL>";

        //make an arraylist ....with STRINGREADER since its no IO reading file...

        List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);

        //add the collection to the document
        for (int k = 0; k < htmlarraylist.Count; k++)
        {
            document.Add((IElement)htmlarraylist[k]);
        }

        document.Add(new Paragraph("And the same with indentation...."));

        // or add the collection to an paragraph
        // if you add it to an existing non emtpy paragraph it will insert it from
        //the point youwrite -
        Paragraph mypara = new Paragraph();//make an emtphy paragraph as "holder"
        mypara.IndentationLeft = 36;
        mypara.InsertRange(0, htmlarraylist);
        document.Add(mypara);
        document.Close();

    }
    catch (Exception exx)
    {
        Console.Error.WriteLine(exx.StackTrace);
        Console.Error.WriteLine(exx.Message);
    }
 }
}`
Rupeshit
im getting exception at this line... the list The non-generic type 'iTextSharp.text.List' cannot be used with type arguments List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);
deepu
when i used array instead of listi am getting exceptionCannot implicitly convert type 'system.collection,generic.list to system.collections.arraylist
deepu
i corrected these issues but if any styles present in the html then itz not getting in the pdf
deepu
You just go here and you will get how to include css also while generating pdf from htmlhttp://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html
Rupeshit
its all about java, is it the problem with itextsharp..that the inline styles never come..?
deepu
Can you plz try this one -http://hspinfo.wordpress.com/tag/convert-html-to-pdf-using-itextsharp/ This is might be helpful for you because in this tutorial css also used with html.......
Rupeshit
thanks for helping me out...yes i have gone through and applied the same like style to pdf but that also wont make any effect...
deepu