为什么生成的 pdf 附件大小为 0 KB?

Why generated pdf attachment having 0 KB Size?

虽然使用以下代码在本地主机上生成 pdf 没问题,但它在服务器上生成的 pdf 大小为 0KB。我在这里做错了什么?

我正在使用 iTextsharp nuget,代码在本地服务器上运行良好。

 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;
    }

这里我调用上面的函数:

    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));
    }

当您 运行 在本地编写代码时,会创建一个文件:

new FileStream(Certpath , FileMode.CreateNew)

当您 运行 服务器上的代码时,会在服务器上创建相同的文件。

但是,您还想将 PDF 文档的字节发送到浏览器。通过创建 MemoryStream:

来实现这一点
MemoryStream ms = new MemoryStream();

当我在您的代码中搜索 ms 变量时,我没有在任何地方找到它,除了最后:

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

换句话说:您不向 ms 写入任何字节; MemoryStream 是空的。您得到 0 个字节的事实证明了这一点。

您的代码在将 PDF 写入服务器磁盘的意义上起作用,但这不是您想要的,对吗?您希望此方法在内存中创建 PDF,然后将其字节发送到服务器。

为此,您需要删除对 certPath 的所有引用,如果文件存在,您所在的部分和 FileStream。相反,您需要将 PDF 写入 MemoryStream:

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

Chris Haas 在他对这个问题的回答中解释了这一点:iTextSharp - Create new document as Byte[]