1

I new in using ITextSharp and want create a simple pdf file. And I trying in localhost, it work fine but put in server it fail to create and show the error message " The document has no pages.". I got try find the solution in web but still the same. How to solve this kind of problem?

Below is my coding.

 var html = GenerateHTML(lst, getuser);

                        Response.ContentType = "application/pdf";

                        Response.AddHeader("content-disposition", "attachment;filename=Commision_" + name + "_" + DateTime.Now.ToString("yyyyMM") + ".pdf");

                        Response.Cache.SetCacheability(HttpCacheability.NoCache);

                        //Render PlaceHolder to temporary stream

                        StringReader reader = new StringReader(html);

                        //Create PDF document

                        Document doc = new Document(PageSize.A4);

                        HTMLWorker parser = new HTMLWorker(doc);

                        PdfWriter.GetInstance(doc, Response.OutputStream);

                        doc.Open();
                        try
                        {
                            doc.NewPage();
                            parser.Parse(reader);
                        }
                        catch (Exception )
                        {
                        }
                        finally
                        {
                            doc.Close();
                        }
askingPPl
  • 263
  • 3
  • 20
  • I'd also get rid of that `catch` clause. You may be hiding an error. – David Crowell May 28 '14 at 17:36
  • possible duplicate of [Saving Panel to PDF not working nor getting errors](http://stackoverflow.com/questions/21459525/saving-panel-to-pdf-not-working-nor-getting-errors) – Chris Haas May 28 '14 at 17:55

1 Answers1

-1

I guess you missed the following 2 lines after doc.close

Response.Write(doc);
Response.End();
Sunil
  • 150
  • 7
  • 1
    `doc` is an instance of `Document` which is a helper class internal to iTextSharp that's used to create PDFs. In itself it doesn't actually represent a PDF so writing to a stream doesn't make any sense. Lots of code out there tries this incorrectly and fails. Writing this to the stream would be like creating an instance of a `Person` class and writing that to the stream. Without a serialization context (which `Document` doesn't have) unexpected things will happen. – Chris Haas May 28 '14 at 17:54