Community discussion forum

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

  • 1 year 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

  • 1 year 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).

Want to stay in touch with what's going on? Follow us on twitter!