iTextSharp生成PDF:如何将PDF发送到客户端并添加提示符
本文关键字:PDF 客户端 添加 提示符 生成 iTextSharp | 更新日期: 2023-09-27 18:17:45
我使用iTextSharp生成了一个pdf,当它创建时,它会自动保存在服务器上的代码中提供的位置,而不是在客户端,当然也没有告诉用户任何事情。
我需要把它发送到客户端,我需要提示一个对话框,询问用户他想在哪里保存他的pdf.
我该怎么做呢?
这是我的PDF代码:using (MemoryStream myMemoryStream = new MemoryStream())
{
Document document = new Document();
PdfWriter PDFWriter = PdfWriter.GetInstance(document, myMemoryStream);
document.AddHeader("header1", "HEADER1");
document.Open();
//..........
document.Close();
byte[] content = myMemoryStream.ToArray();
// Write out PDF from memory stream.
using (FileStream fs = File.Create(HttpContext.Current.Server.MapPath("~''report.pdf")))
{
fs.Write(content, 0, (int)content.Length);
}
编辑
这是我想要的结果的一个例子http://examples.extjs.eu/?ex=download
感谢您的回复,我修改了我的代码如下:
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AppendHeader( "Content-Disposition", "attachment; filename=test.pdf");
using (MemoryStream myMemoryStream = new MemoryStream())
{
Document document = new Document();
PdfWriter PDFWriter = PdfWriter.GetInstance(document, myMemoryStream);
document.AddHeader("Content-Disposition", "attachment; filename=wissalReport.pdf");
document.Open();
//..........
document.Close();
byte[] content = myMemoryStream.ToArray();
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AppendHeader("content-disposition","attachment;filename=" + "my_report.pdf");
HttpContext.Current.Response.ContentType = "Application/pdf";
//Write the file content directly to the HTTP content output stream.
HttpContext.Current.Response.BinaryWrite(content);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
但是我得到这个错误:
Uncaught Ext.Error: You're trying to decode an invalid JSON String:
%PDF-1.4 %���� 3 0 obj <</Type/XObject/Subtype/Image/Width 994/Height 185/Length 13339/ColorSpace/DeviceGray/BitsPerComponent 8/Filter/FlateDecode>>stream x���|E�
...........
我绝对确定我的itextsharp创建PDF是正确的,因为我可以将它保存在服务器上,但这不是我需要做的,当我试图将它发送到客户端时,我得到了上面的错误
thanks in advance
在web应用程序的情况下,您可能希望以二进制格式将pdf流式传输给用户,这将打开pdf或提示用户保存文件。
请记住,pdf生成是在服务器上进行的,即使用户提供了路径,它在服务器上也不会有任何用处。参见以下链接-
- 如何使用ASP编写二进制文件到浏览器。.NET和Visual c# .NET
在你的情况下,你正在生成文件,因此已经有一个二进制流而不是文件,因此你可以直接使用响应。BinaryWrite而不是Response.WriteFile.
修改示例:
Response.Buffer = false;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Write the file content directly to the HTTP content output stream.
Response.BinaryWrite(content);
Response.Flush();
Response.End();
您需要向用户浏览器发送一个内容处置头。记忆中的代码是这样的:
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition","attachment; filename=nameofthefile.pdf");
当前您将文件保存在文件服务器上,因此每个请求都覆盖相同的pdf。如果你同时收到两个PDF请求,可能会导致错误。
使用Response
将PDF(从内存流)返回给用户,并跳过将PDF写入服务器上的本地文件。
浏览器将询问用户文件应该保存在哪里。比如:
Response.ContentType = "Application/pdf";
myMemoryStream.CopyTo(Response.OutputStream);
也看看Alun的答案,使用content-disposition
你可以向用户提出一个文件名
SOLVED
提交操作试图解释响应,但由于响应不是已知格式,因此无法解释。
我刚刚设置了窗口。下载文件的位置,这个工作正常。
{
xtype:'button',
text: 'Generate PDF',
handler: function () {
window.location = '/AddData.ashx?action=pdf';
}
}
除了设置位置,您还可以使用window.open()。
文件是否下载或打开取决于浏览器设置。
不需要使用MemoryStream
。使用Response.OutputStream
代替。这就是它存在的意义。不需要使用Response.BinaryWrite()
或任何其他调用来显式地编写文档;当您使用Response.OutputStream
时,iTextSharp负责写入流。
下面是一个简单的工作示例:
Response.ContentType = "application/pdf";
Response.AppendHeader(
"Content-Disposition",
"attachment; filename=test.pdf"
);
using (Document document = new Document()) {
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
document.Add(new Paragraph("This is a paragraph"));
}
下面是如何添加正确的HTTP标头。(得到保存文件的提示)如果你的代码是在web form
,(按钮点击处理程序),在using
语句之后添加Response.End()
到上面的代码示例中,这样web表单的HTML输出就不会追加到PDF文档中。