[英]save Pdf File in the particular folder instead of downloading
我正在将html转换为pdf文件。 立即下载。 我不想立即下载。 转换后,我想将文件保存在我的项目文件夹中。
我的C#代码
string html ="<table><tr><td>some contents</td></tr></table>";
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=WelcomeLetter.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
StringReader sr = new StringReader(table);
Document ResultPDF = new Document(iTextSharp.text.PageSize.A4, 25, 10, 20, 30);
PdfPTable Headtable = new PdfPTable(7);
Headtable.TotalWidth = 525f;
Headtable.LockedWidth = true;
Headtable.HeaderRows = 5;
Headtable.FooterRows = 2;
Headtable.KeepTogether = true;
HTMLWorker htmlparser = new HTMLWorker(ResultPDF);
PdfWriter.GetInstance(ResultPDF, Response.OutputStream);
ResultPDF.Open();
htmlparser.Parse(sr);
ResultPDF.Close();
Response.Write(ResultPDF);
Response.End();
为了将pdf文件本地保存在您的项目文件夹中,您可以使用FileStream
类。
FileStream stream = new FileStream(filePath, FileMode.Create);//Here filePath is path of your project folder.
现在,在创建PdfWriter
对象的实例时,请使用此流,而不要使用Response.OutputStream
。
PdfWriter.GetInstance(ResultPDF, stream);
现在不要使用Responce.Write
因为您不想下载文件,并在最后关闭流。
stream.Close();
我将把每个人的答案组合成一个您应该可以使用的答案。 如果这有效,我将接受Manish Parakhiya的回答,因为那是最重要的部分。
首先,我假设您使用的是iTextSharp的最新版本。 我认为5.5.5是最新版本。 其次,由于这个原因,我将对您的代码进行一些重组,以使用using
模式。 如果您使用的是过时的不支持的旧版本(例如4.1.6),则需要重新调整。
几乎所有的教程都向您表明,您可以直接绑定Response.OutputStream
。 这是100%有效的,但我认为这也是一个非常糟糕的主意。 而是绑定到更通用的MemoryStream
。 这使调试变得更加容易,并且您的代码将更容易移植和修改。
以下代码包含有关每个更改以及实际操作的注释。 最上面的部分是关于用HTML字符串创建PDF。 底部实际上对其进行了某些操作,包括将其写入磁盘和/或将其流式传输到浏览器。
//Will hold our PDF eventually
Byte[] bytes;
//HTML that we want to parse
string html = "<table><tr><td>some contents</td></tr></table>";
//Create a MemoryStream to write our PDF to
using (var ms = new MemoryStream()) {
//Create our document abstraction
using (var ResultPDF = new Document(iTextSharp.text.PageSize.A4, 25, 10, 20, 30)) {
//Bind a writer to our Document abstraction and our stream
using (var writer = PdfWriter.GetInstance(ResultPDF, ms)) {
//Open the PDF for writing
ResultPDF.Open();
//Parse our HTML using the old, obsolete, not support parser
using (var sw = new StringWriter()) {
using (var hw = new HtmlTextWriter(sw)) {
using (var sr = new StringReader(html)) {
using (var htmlparser = new HTMLWorker(ResultPDF)) {
htmlparser.Parse(sr);
}
}
}
}
//Close the PDF
ResultPDF.Close();
}
}
//Grab the raw bytes of the PDF
bytes = ms.ToArray();
}
//At this point, the bytes variable holds a valid PDF file.
//You can write it disk:
System.IO.File.WriteAllBytes("your file path here", bytes);
//You can also send it to a browser:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=WelcomeLetter.pdf");
Response.BinaryWrite(bytes);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Never do the next line, it doesn't do what you think it does and actually produces corrupt PDFs
//Response.Write(ResultPDF); //BAD!!!!!!
Response.End();
string tempDirectory = Session.SessionID.ToString();
string location = Path.Combine(Server.MapPath(
WebConfigurationManager.AppSettings["PathSet"].ToString()), tempDirectory);
if (!Directory.Exists(location))
{
Directory.CreateDirectory(location);
}
string fileName="abc.pdf";
filePath = Path.Combine(location, fileName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.