[英]how to save pdf on server map path using iTextsharp
i am using following code for generate pdf and it is work perfect: 我使用以下代码生成pdf,它是完美的工作:
string strQuery = "select * from userdata";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);
//Create a dummy GridView
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
it works good. 它运作良好。 but i am able to save this pdf to on server map path.
但我能够将此pdf保存到服务器地图路径上。
i have written below after pdfDoc.Close(); 我在pdfDoc.Close()之后写了下面的内容。
String path = Server.MapPath("~/mypdf.pdf");
But it is not saving pdf to server map path. 但它不是将pdf保存到服务器映射路径。
how can i do this? 我怎样才能做到这一点?
You are currently writing the document to the following output stream: Response.OutputStream
您当前正在将文档写入以下输出流:
Response.OutputStream
Once you do pdfDoc.Close();
一旦你做了
pdfDoc.Close();
, the PDF bytes are gone. ,PDF字节消失了。
If you want to save the PDF to the server, then you need to replace the following line: 如果要将PDF保存到服务器,则需要替换以下行:
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
With this line: 有了这条线:
PdfWriter.GetInstance(pdfDoc, new FileStream(context.Server.MapPath("~") + "mypdf.pdf");
Now your bytes won't be sent to the browser, but the PDF will be created on your server. 现在您的字节不会被发送到浏览器,但PDF将在您的服务器上创建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.