[英]How to add new page in itextsharp pdf generated using html table
[英]iTextSharp generated PDF: How to send the pdf to the client and add a prompt?
我已经使用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�
...........
我绝对确定我创建pdf的itextsharp是正确的,因为我可以将其保存在服务器上,但是那不是我需要做的,当我尝试将其发送到客户端时,出现了以上错误
提前致谢
对于Web应用程序,您可能希望将pdf以二进制形式流式传输给用户,这将打开pdf或提示用户保存文件。
请记住,即使用户提供了它在服务器上没有任何用处的路径,PDF生成也在服务器上发生。 请参阅以下链接-
在您的情况下,您正在生成文件,因此已经有了二进制流而不是文件,因此可以直接使用Response.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();
当前,您正在将文件保存在文件服务器上,从而每次请求都覆盖相同的pdf。 如果您同时收到两个PDF请求,可能会导致错误。
使用“ Response
将PDF(从内存流)返回给用户,并跳过将PDF写入服务器本地文件的操作。
浏览器将询问用户文件应保存在何处。 就像是:
Response.ContentType = "Application/pdf";
myMemoryStream.CopyTo(Response.OutputStream);
还要看一下Alun的回答 ,使用content-disposition
可以向用户建议一个文件名。
您需要将内容处置标头发送到用户浏览器。 从内存中看,代码是这样的:
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition","attachment; filename=nameofthefile.pdf");
解决了
错误是由于提交操作试图解释响应,因为它不是已知格式,所以无法执行。
我只是将window.location设置为下载文件,所以效果很好。
{
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文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.