【问题标题】:Why generated pdf attachment having 0 KB Size?为什么生成 0 KB 大小的 pdf 附件?
【发布时间】:2016-02-05 16:12:19
【问题描述】:

虽然使用下面的代码在 localhost 上生成 pdf 很好,但是它在服务器上生成 0KB 大小的 pdf。我在这里做错了什么?

我正在使用 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));
    }

【问题讨论】:

    标签: asp.net-mvc-4 pdf pdf-generation itextsharp


    【解决方案1】:

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

    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[]

    【讨论】:

      猜你喜欢
      • 2018-03-01
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      • 2015-08-24
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多