Asp.net mvc 4 为什么生成的pdf附件大小为0 KB?
使用下面的代码在本地主机上生成pdf是可以的,但在服务器上生成的pdf大小为0KB。我做错了什么 我使用的是iTextsharp nuget,代码在本地服务器上运行良好Asp.net mvc 4 为什么生成的pdf附件大小为0 KB?,asp.net-mvc-4,pdf,pdf-generation,itextsharp,Asp.net Mvc 4,Pdf,Pdf Generation,Itextsharp,使用下面的代码在本地主机上生成pdf是可以的,但在服务器上生成的pdf大小为0KB。我做错了什么 我使用的是iTextsharp nuget,代码在本地服务器上运行良好 public byte[] GetPDF(string pHTML, List<int> ideaidlist) { byte[] bPDF = null; MemoryStream ms = new MemoryStream(); TextReader tx
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 byte[]GetPDF(字符串pHTML,列表ideaidlist)
{
字节[]bPDF=null;
MemoryStream ms=新的MemoryStream();
TextReader txtReader=新的StringReader(pHTML);
//Document doc=新文档(PageSize.LETTER,0,0,0);
文档文档=新文档(新矩形(864f,870f),0,0,0,0);
字符串Certpath=ConfigurationManager.AppSettings[“MailImagePath”]+“Certificates.pdf”;//System.Configuration。
字符串imgtopath=ConfigurationManager.AppSettings[“CertificateImagePath”];
字符串ImgMidPath=ConfigurationManager.AppSettings[“CertificateImagePath”];
字符串ImgBotPath=ConfigurationManager.AppSettings[“CertificateImagePath”];
FileInfo newExistFile=newfileinfo(Certpath);
if(newExistFile.Exists)
{
newExistFile.Delete();
}
PdfWriter optdfwriter=PdfWriter.GetInstance(doc,newfilestream(Certpath,FileMode.CreateNew));
HTMLWorker HTMLWorker=新HTMLWorker(文档);
doc.Open();
generatePDFm data=new generatePDFm();
foreach(ideaidlist中的变量项)
{
数据=新的CommonBL().GetIdeaidListForGenerateCertificates(项);
doc.NewPage();
PdfPTable table=新的PdfPTable(1);
表1.TotalWidth=1000;
表1.1:百分比=100;
table.LockedWidth=true;
表1.1水平对齐=0;
table.DefaultCell.Border=矩形。无边框;
iTextSharp.text.Image-imageTopURL=iTextSharp.text.Image.GetInstance(imgtopath+“CertiTop.PNG”);
PdfPCell imgTopCell=新的PdfPCell(imageTopURL);
imgTopCell.Border=Rectangle.NO_Border;
表2.AddCell(imgTopCell);
imageTopURL.SpacingAfter=20;
PdfPCell FirstTxtCell=新的PdfPCell();
段落p=新段落(data.EmpName);
p、 Font=新字体(Font.FontFamily.HELVETICA,35f,Font.下划线);
p、 对齐=元素。对齐\u中心;
第一单元加法(p);
FirstTxtCell.PaddingRight=190f;
FirstTxtCell.Border=0;
表.AddCell(FirstTxtCell);
iTextSharp.text.Image imageMidURL=iTextSharp.text.Image.GetInstance(ImgMidPath+“CertiMid.PNG”);
PdfPCell imgMidCell=新的PdfPCell(imageMidURL);
imgMidCell.Border=Rectangle.NO_Border;
imgMidCell.Border=0;
imageMidURL.SpacingBefore=15f;
imageMidURL.Alignment=Element.ALIGN\u CENTER;
imgMidCell.PaddingRight=244f;
表1.AddCell(imgMidCell);
PdfPCell SecTextCell=新的PdfPCell();
段落段落=新段落(数据标题);
para.Font=新字体(Font.FontFamily.HELVETICA,32f,Font.ITALIC);
para.Alignment=Element.ALIGN_CENTER;
SecTextCell.附录(第段);
SecTextCell.Border=0;
SecTextCell.PaddingRight=200f;
表.AddCell(SecTextCell);
iTextSharp.text.Image imageBotURL=iTextSharp.text.Image.GetInstance(ImgBotPath+“CertiBottom.PNG”);
PdfPCell imgBotCell=新的PdfPCell(imageBotURL);
imgBotCell.Border=0;
表1.AddCell(imgBotCell);
imageBotURL.SpacingBefore=20;
可缩放溶质(860f,230f);
可缩放溶质(930f,100f);
可缩放溶质(864f,230f);
doc.Open();
单据新增(表);
htmlWorker.StartDocument();
解析(txtReader);
htmlWorker.EndDocument();
}
htmlWorker.Close();
doc.Close();
bPDF=ToArray女士();
Close女士();
返回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));
}
public void GenerateCertificatePDF(列表ideaidlist)
{
字符串HTMLContent=“”;
Response.Clear();
Response.ContentType=“application/pdf”;
AddHeader(“内容处置”、“附件;文件名=“+”Certificates.pdf”);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
BinaryWrite(GetPDF(HTMLContent,ideaidlist));
}
在本地运行代码时,将创建一个文件:
new FileStream(Certpath , FileMode.CreateNew)
在服务器上运行代码时,会在服务器上创建相同的文件
但是,您还希望将PDF文档的字节发送到浏览器。要实现这一点,请创建内存流
:
MemoryStream ms = new MemoryStream();
PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);
当我在你的代码中搜索ms
变量时,除了在末尾之外,我在任何地方都找不到它:
bPDF = ms.ToArray();
ms.Close();
return bPDF;
换句话说:您不需要将任何字节写入ms
;MemoryStream
为空。这一点得到了0字节的证明
您的代码可以在服务器磁盘上写入PDF,但这不是您想要的,不是吗?您希望此方法在内存中创建PDF,然后将其字节发送到服务器
要实现这一点,您需要删除对certPath
的所有引用,如果文件存在,则删除您所在的部分以及FileStream
。相反,您需要将PDF文件写入MemoryStream
:
MemoryStream ms = new MemoryStream();
PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);
克里斯·哈斯在回答这个问题时解释了这一点: