0

I faced the following issue while generating pdf files on mobile browser.The file either got corrupted on some mobile browsers while in some the file got downloaded but didn't display the text,it displayed only images in the file.Meanwhile the file got generated perfectly when working on desktop browsers and the file content were displayed perfectly when downloaded. I don't know actual reason behind it as I am totally new in developing web applications.

The code I used is given below:

Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc,         Response.OutputStream);
pdfDoc.Open();
string imageUrl = Server.MapPath("~/logo//bcetlogo.jpg");
iTextSharp.text.Image ImageLogo =    iTextSharp.text.Image.GetInstance(imageUrl);
ImageLogo.ScaleToFit(80f, 80f);
pdfDoc.Add(ImageLogo);
Font f = FontFactory.GetFont("Arial", 15);
string title = "HelloWorld";
Paragraph text1 = new Paragraph(title, f);
pdfDoc.Add(text1);
pdfWriter.CloseStream = false;
pdfWriter.Close();
Response.Buffer = true;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=Example.PDF");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
INDIA IT TECH
  • 1,902
  • 4
  • 12
  • 25
  • Dear @AbhinandanKumar, was something the matter with my answer? Did you get a chance to try the proposed solution? Did it help you? – Bruno Lowagie Mar 30 '16 at 11:53

1 Answers1

1

This is certainly wrong:

Response.Write(pdfDoc);

The pdfDoc object is of type Document and Response.Write() expects bytes, not a Document object. Your claim that this works in some cases can't be true.

You also need to remove these two lines:

pdfWriter.CloseStream = false;
pdfWriter.Close();

Replace them with this line:

pdfDoc.Close();

Read more about the 5 steps in the PDF creation process here: Getting started. Where did you get your code from? Can you promise that you never look at that documentation ever again? Always use the official web site!

When you do pdfDoc.Close(), the PDF is finalized. All the DocListener instances that listen to the Document are closed. In your case, the DocListener instance that listens to pdfDoc is a PdfDocument instance that you don't know about because it's used only internally. Closing this PdfDocument instance is important, because in that close() operation, plenty of content is flushed. You skip this step by closing the pdfWriter instead of the pdfDoc.

You also keep the content stream open. That's a bad idea. The content stream should be closed. There are some known issues when using the Response.OutputStream directly. It's better to use a MemoryStream like is done in the answer to this question: iTextSharp is producing a corrupt PDF with Response

If you study this answer well, you'll see this:

byte[] bytes = memoryStream.ToArray();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=ControleDePonto.pdf");
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();

As you can see you need the BinaryWrite() method and that method expects a byte[]. Aside from the many other errors in your code, your main error is that you pass a Document object to the Write() method.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165