iTextSharp生成PDF:如何将pdf发送到客户端并添加提示?

时间:2011-12-14 14:22:07

标签: c# pdf extjs itextsharp 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�
...........

我绝对肯定我的itextsharp创建pdf是正确的,因为我可以将它保存在服务器上,但那不是我需要做的,当我尝试将它发送到客户端时我得到了上面的错误

提前致谢

5 个答案:

答案 0 :(得分:5)

如果是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();

答案 1 :(得分:3)

目前,您要将文件保存在文件服务器上,从而在每次请求时覆盖相同的pdf。如果同时收到两个PDF请求,可能会导致错误。

使用Response将PDF(从内存流)返回给用户,并跳过将PDF写入服务器本地文件。

浏览器会询问用户应该保存文件的位置。类似的东西:

  Response.ContentType = "Application/pdf";
  myMemoryStream.CopyTo(Response.OutputStream);

另请查看the answer from Alun,使用content-disposition您可以向用户提供文件名。

答案 2 :(得分:3)

您需要向用户浏览器发送内容处置标头。从内存中,代码有点像这样:

Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition","attachment; filename=nameofthefile.pdf");

答案 3 :(得分:3)

解决

错误来自提交操作,试图解释它不能的响应,因为它不是已知的格式。

我只是设置window.location来下载文件,这很好。

{
xtype:'button',           
text: 'Generate PDF',           
handler: function () {
     window.location = '/AddData.ashx?action=pdf';                   
}
}

除了设置位置,您还可以执行window.open()。

是下载还是打开文件取决于浏览器设置。

答案 4 :(得分:1)

您无需使用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"));
}

这是how to add the proper HTTP headers。 (获取保存文件的提示)如果您的代码位于web form,(按钮单击处理程序),请在Response.End()语句之后将using添加到上面的代码示例中,以便Web表单的HTML输出不会附加到PDF文档中。