Click here to Skip to main content
15,889,858 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I got this error in online server, Exception Details: System.IO.IOException: The document has no pages.


What I have tried:

<pre><pre> byte[] bytes = null;
           var cssText = File.ReadAllText(MapPath("~/css/bootstrap.min.css"));

            //Read our HTML as a .Net stream
            using (var sr = new StringReader(htmlTable.ToString()))
            {
                //try
                //{
                //Standard PDF setup using a MemoryStream, nothing special
                // using (var ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText)))
                using (var ms = new MemoryStream())
                {

                    using (var pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f))
                    {

                        //Bind a parser to our PDF document
                        using (var htmlparser = new HTMLWorker(pdfDoc))
                        {

                            //Bind the writer to our document and our final stream
                            using (var w = PdfWriter.GetInstance(pdfDoc, ms))
                            {

                                pdfDoc.Open();

                                //Parse the HTML directly into the document
                                htmlparser.Parse(sr);

                                pdfDoc.Close();

                                //Grab the bytes from the stream before closing it
                                bytes = ms.ToArray();
                            }
                        }
                    }
                }
                //}
                //catch (Exception ex)
                //{

                //}
            }

            //Assuming that the above worked we can now finally modify the HTTP response
            Response.Clear();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=xyz.pdf");
            Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
            //Send the bytes from the PDF
            Response.BinaryWrite(bytes);
            Response.End();
Posted
Updated 20-Aug-19 23:56pm
v2
Comments
Richard MacCutchan 21-Aug-19 3:45am    
Where does the error occur?
sunil kumar 21-Aug-19 6:52am    
locally its working but error in online , i think error here is occur
using (var htmlparser = new HTMLWorker(pdfDoc))
RickZeeland 21-Aug-19 4:34am    
What PDF library are you using ?
Maciej Los 21-Aug-19 5:43am    
Looks like iTextSharp is in usage ;)

1 solution

A pdf document is empty, because you did put nothing into it.
C#
pdfDoc.Open();
//Parse the HTML directly into the document
htmlparser.Parse(sr); //we don't know what Parse method returns, but result is not written into a pdf document
pdfDoc.Close();


To be able to write something, you have to use Add() method. Below piece of code adds a table with custom font style:
C#
//define fonts
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, false);
Font bf = new Font(bfTimes, 11);
Font headerf = new Font(bfTimes, 9, Font.ITALIC | Font.BOLD, BaseColor.WHITE);
Font sf = new Font(bfTimes, 9, Font.NORMAL);
//add table
colcount = 3;
table = new PdfPTable(colcount);
widths = new float[] { 1f, 1f, 1f };
table.SetWidths(widths);
table.HorizontalAlignment = 0;
//add headers
for(int c=1; c<=colcount;c++)
{
	s = $"Header {c}";
	PdfPCell header = new PdfPCell(new Phrase(s, headerf));
	header.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
	header.BackgroundColor = BaseColor.DARK_GRAY;
	table.AddCell(header);
}
//add rows
for(int r=1; r<=5; r++)
{
	for(int c=1; c<=colcount;c++)
	{
		s = $"Col {c} Row {r}";
		cell = new PdfPCell(new Phrase(s, sf));
		cell.BackgroundColor = r % 2 == 0 ? BaseColor.WHITE : BaseColor.LIGHT_GRAY;
		table.AddCell(cell);
	}
}
pdfdoc.Add(table);
 
Share this answer
 
Comments
RickZeeland 21-Aug-19 6:00am    
Plausible +5 !
Maciej Los 21-Aug-19 6:12am    
Thank you, Rick.
sunil kumar 21-Aug-19 6:13am    
Here htmltable which is htmldata and its working in locally but error in online

using (var sr = new StringReader(htmlTable.ToString()))
Maciej Los 21-Aug-19 6:24am    
So, debug your programme to find out what's going on.
Note: i have no access to your computer, i can't read direct from your screen and i'm not able to resolve your issue based on such of small portion of information.
Richard Deeming 22-Aug-19 7:58am    
I think the HTMLWorker's Parse method writes to the Document passed to its constructor, and doesn't return anything. :)

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