-1

while generating pdf on localhost using below code is fine,however it generates a pdf of 0KB size while on server . What I am doing wrong here ?

I am using iTextsharp nuget and the code runs fine on local server.

 public byte[] GetPDF(string pHTML, List<int> ideaidlist)
    {
        byte[] bPDF = null;
        MemoryStream ms = new MemoryStream();
        TextReader txtReader = new StringReader(pHTML);
       // Document doc = new Document(PageSize.LETTER, 0, 0, 0, 0);
        Document doc = new Document(new Rectangle(864f, 870f), 0, 0, 0, 0);
        string Certpath = ConfigurationManager.AppSettings["MailImagePath"]+"Certificates.pdf";//System.Configuration.
        string ImgTopPath =ConfigurationManager.AppSettings["CertificateImagePath"];
        string ImgMidPath =ConfigurationManager.AppSettings["CertificateImagePath"];
        string ImgBotPath =ConfigurationManager.AppSettings["CertificateImagePath"];
        FileInfo newExistFile = new FileInfo(Certpath);
        if (newExistFile.Exists)
        {
            newExistFile.Delete();
        }
        PdfWriter oPdfWriter = PdfWriter.GetInstance(doc,new FileStream(Certpath , FileMode.CreateNew));

        HTMLWorker htmlWorker = new HTMLWorker(doc);

        doc.Open();
        GeneratePdfVM data = new GeneratePdfVM();
        foreach (var item in ideaidlist)
        {
            data = new CommonBL().GetIdeaidListForGenerateCertificates(item);
            doc.NewPage();
            PdfPTable table = new PdfPTable(1);
            table.TotalWidth = 1000;
            table.WidthPercentage = 100;
            table.LockedWidth = true;
            table.HorizontalAlignment = 0;
            table.DefaultCell.Border = Rectangle.NO_BORDER;

            iTextSharp.text.Image imageTopURL = iTextSharp.text.Image.GetInstance(ImgTopPath + "CertiTop.PNG");
            PdfPCell imgTopCell = new PdfPCell(imageTopURL);
            imgTopCell.Border = Rectangle.NO_BORDER;              
            table.AddCell(imgTopCell);
            imageTopURL.SpacingAfter = 20;

            PdfPCell FirstTxtCell = new PdfPCell();
            Paragraph p = new Paragraph(data.EmpName);
            p.Font = new Font(Font.FontFamily.HELVETICA, 35f, Font.UNDERLINE);
            p.Alignment = Element.ALIGN_CENTER;
            FirstTxtCell.AddElement(p);
            FirstTxtCell.PaddingRight = 190f;
            FirstTxtCell.Border = 0;
            table.AddCell(FirstTxtCell);

            iTextSharp.text.Image imageMidURL = iTextSharp.text.Image.GetInstance(ImgMidPath + "CertiMid.PNG");
            PdfPCell imgMidCell = new PdfPCell(imageMidURL);
            imgMidCell.Border = Rectangle.NO_BORDER;
            imgMidCell.Border = 0;
            imageMidURL.SpacingBefore = 15f;
            imageMidURL.Alignment = Element.ALIGN_CENTER;
            imgMidCell.PaddingRight = 244f;
            table.AddCell(imgMidCell);               

            PdfPCell SecTextCell = new PdfPCell();                                         
            Paragraph para = new Paragraph(data.Title);
            para.Font = new Font(Font.FontFamily.HELVETICA, 32f, Font.ITALIC);
            para.Alignment = Element.ALIGN_CENTER;
            SecTextCell.AddElement(para);
            SecTextCell.Border = 0;
            SecTextCell.PaddingRight = 200f;
            table.AddCell(SecTextCell);                                                            

            iTextSharp.text.Image imageBotURL = iTextSharp.text.Image.GetInstance(ImgBotPath + "CertiBottom.PNG");
            PdfPCell imgBotCell = new PdfPCell(imageBotURL);
            imgBotCell.Border = 0;               
            table.AddCell(imgBotCell);             
            imageBotURL.SpacingBefore=20;

            imageTopURL.ScaleAbsolute(860f, 230f);
            imageMidURL.ScaleAbsolute(930f, 100f);
            imageBotURL.ScaleAbsolute(864f, 230f);
            doc.Open();
            doc.Add(table);
            htmlWorker.StartDocument();
            htmlWorker.Parse(txtReader);
            htmlWorker.EndDocument();
        }
        htmlWorker.Close();
        doc.Close();
        bPDF = ms.ToArray();            
        ms.Close();            
        return bPDF;
    }

Here I am calling the above function :

    public void GenerateCertificatePDF(List<int> ideaidlist)
    {
        string HTMLContent = "";            
         Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=" + "Certificates.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.BinaryWrite(GetPDF(HTMLContent, ideaidlist));
    }
Chandan
  • 217
  • 1
  • 3
  • 17

1 Answers1

1

When you run your code locally, a file is created:

new FileStream(Certpath , FileMode.CreateNew)

That same file is created on the server when you run the code on the server.

However, you also want to send the bytes of the PDF document to the browser. To achieve this by creating a MemoryStream:

MemoryStream ms = new MemoryStream();

When I search your code for the ms variable, I don't find it anywhere except at the very end:

bPDF = ms.ToArray();            
ms.Close();            
return bPDF;

In other words: you don't write any bytes to ms; the MemoryStream is empty. This is proven by the fact that you get 0 bytes.

Your code works in the sense that a PDF is written on the disk of the server, but that's not what you want, is it? You want this method to create a PDF in memory and then send its bytes to a server.

To achieve this, you need to remove all references to certPath, the part where you exist if the file exists and the FileStream. Instead, you need to write the PDF to the MemoryStream:

PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);

This is explained by Chris Haas in his answer to this question: iTextSharp - Create new document as Byte[]

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