Click here to Skip to main content
15,067,251 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Following is the code for convert webpage to pdf using itextsharp


protected void btnExport_Click(object sender, EventArgs e)
        {
            StringWriter sw = new StringWriter();
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
            pdfDoc.Close();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=HTML.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();

}



This gives me "The document has no pages" in this line
pdfDoc.Close();



How can I resolve this

What I have tried:

I added itextsharp dll file and
using iTextSharp.tool.xml;

both
Posted
Updated 14-Jan-21 20:59pm
Comments
Richard Deeming 10-Oct-17 11:46am
   
Looks like you've forgotten to render anything to the StringWriter before creating the StringReader. You're passing an empty string to the ParseXHtml method, so there's no content to add to the document.

1 solution

I found, that iTestSharp has problem with tag <br>.
   
Comments
Jindrich Matous 15-Jan-21 3:02am
   
Use <br/> instead.

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