2016-02-04 44 views
-1

虽然使用下面的代码在本地主机上生成pdf是很好的,但是它会在服务器上生成0KB大小的pdf。我在这里做错了什么?为什么生成pdf附件有0 KB大小?

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

回答

0

当你在本地运行代码,创建了一个文件:

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

这是由克里斯·哈斯在他的回答解释了这个问题:iTextSharp - Create new document as Byte[]

相关问题