iTextSharp generated PDF: How to send the pdf to the client and add a prompt?

前端 未结 5 483
半阙折子戏
半阙折子戏 2020-12-01 17:25

I have generated a pdf using iTextSharp, when its created it saves automatically in the location provided in my code on the server not on the client side and of course witho

相关标签:
5条回答
  • 2020-12-01 17:59

    You need to send a content disposition header to the users browser. From memory the code is something sort of like this:

    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition","attachment; filename=nameofthefile.pdf");
    
    0 讨论(0)
  • 2020-12-01 18:01

    SOLVED

    The error is from the submit operation trying to interpret the response which it can not because it is not in a known format.

    I just set window.location to download files and this works fine.

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

    Instead of setting the location you can also do window.open().

    Whether the file will be downloaded or opened depends on browser settings.

    0 讨论(0)
  • 2020-12-01 18:05

    Currently you are saving your file on the file server, thereby overwriting the same pdf with every request. And probably causing errors if you get two requests for a PDF at the same time.

    Use Response to return the PDF (from the memorystream) to the user, and skip the writing of the PDF to a file locally on your server.

    The browser will ask the user where the file should be saved. Something like:

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

    Also look at the answer from Alun, using content-disposition you can propose a filename to the user.

    0 讨论(0)
  • 2020-12-01 18:06

    In case of a web application you probably want to stream the pdf as binary to user, that would either open the pdf or prompt user to save the file.

    Remember pdf generation is happening at server, even if user provides the path it won't be of any use on server. See following links -

    • How To Write Binary Files to the Browser Using ASP.NET and Visual C# .NET

    In your case you are generating the file and hence will already be having a binary stream instead of file, hence you can directly use Response.BinaryWrite instead of Response.WriteFile.

    Modified sample:

    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();
    
    0 讨论(0)
  • 2020-12-01 18:10

    You do not need to use MemoryStream. Use Response.OutputStream instead. That's what it's there for. No need to use Response.BinaryWrite() or any other call to explicitly write the document either; iTextSharp takes care of writing to the stream when you use Response.OutputStream.

    Here's a simple working example:

    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"));
    }
    

    Here's how to add the proper HTTP headers. (getting the prompt to save the file) And if your code is in a web form, (button click handler), add Response.End() to the code example above after the using statement so that the web form's HTML output is not appended the PDF document.

    0 讨论(0)
提交回复
热议问题