我正在做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();
答案 0 :(得分:1)
要在项目文件夹中本地保存pdf文件,可以使用FileStream
这样的类。
FileStream stream = new FileStream(filePath, FileMode.Create);//Here filePath is path of your project folder.
现在,在创建Response.OutputStream
对象的实例时,请使用此流而不是PdfWriter
。
PdfWriter.GetInstance(ResultPDF, stream);
现在不要使用Responce.Write
,因为您不想下载您的文件。并在结束时关闭您的信息流。
stream.Close();
答案 1 :(得分:1)
我将把每个人的答案合并到一个你应该能够投入使用的答案中。如果这样做,我会接受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();
答案 2 :(得分:0)
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);