unable to create a pdf from html file using itextsharp with asp.net

.net , asp.net Thrissūr, India
  • 12 years ago
    private void Button1_Click(object sender, System.EventArgs e)

    {

    Document doc=
    new Document(PageSize.A4);System.IO.MemoryStream FStream = new MemoryStream();

     

    try

    {

     

     

    PdfWriter.GetInstance(doc,
    new FileStream("DiplomaTest.pdf",FileMode.Create));

    doc.Open();

     

    HtmlParser.Parse(doc,Server.MapPath(
    "Test.html"));

     

    //doc.Close();

     

    }

    catch(Exception ex)

    {

    throw ex;

    }

    Response.Clear();

    Response.AddHeader(
    "Content-Disposition","attachment;filename=DiplomaTest.pdf");Response.ContentType="application/pdf";

    Response.BinaryWrite(FStream.ToArray());

    Response.End();

    }

    giving the error---------document has no pages and opening the pdf with a message saying that file has corrupted.

    I am trying a lot on this from so many days.

    Please help me.

     

    With Regards,

    Sreenivasulu.M

  • 12 years ago

    HtmlParser.Parse does NOT throw any error , but the pdf file generated from this could be blank/empty.

    Debug output shows the messages from parser, if Html file has invalid structure.

    The post of Creating pdf in .NET from html has a lot of interesting comments,

    including suggestion  to use HTML Agility Pack.

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The difference between theory and practice is smaller in theory than in practice.”