ITextSharp generated PDF: how to send a PDF file to a client and add an invitation?

I created a pdf file using iTextSharp when it created it automatically saves it to the location specified in my code on the server, and not on the client side, and, of course, without telling the user anything.

I need to send it to the client, and I need to call a dialog box to ask the user where he wants to save his pdf file.

How can i do this?

this is my pdf code:

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

EDIT

this is an example of the result i want http://examples.extjs.eu/?ex=download

thanks to your answers, I changed my code to this:

 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(); 

but I get this error:

 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  ........... 

im absolutely sure that my itextsharp to create pdf is correct, because I can save it to the server, but this is not what I need to do, when I try to send it to the client, I got the error above

early

+6
source share
5 answers

solvable

An error from a send operation trying to interpret a response that it cannot, because it is not in a known format.

I just set window.location to download files and it 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 is downloaded or opened depends on your browser settings.

+3
source

In the case of a web application, you probably want to transfer the PDF as binary to a user who will either open the pdf file or prompt the user to save the file.

Remember that generation of generation occurs on the server, even if the user provides a path that he will not use on the server. See the following links -

In your case, you are creating a file and therefore will already have a binary stream instead of a file, so 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(); 
+5
source

Currently, you save the file on the file server, thereby overwriting the same pdf with each request. And they probably cause errors if you get two PDF requests at the same time.

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

The browser will ask the user where the file should be saved. Sort of:

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

Also see the answer from Alun , using content-disposition , you can offer the file name to the user.

+3
source

You need to send the content header to the user's browser. From memory, the code looks something like this:

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

You do not need to use a MemoryStream . Use Response.OutputStream instead. What is it there for. No need to use Response.BinaryWrite() or any other call to explicitly write the document; iTextSharp takes care of writing to the stream when using Response.OutputStream .

Here is 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 correct HTTP headers . (getting hints to save the file). And if your code is in a web form , (button click handler), add Response.End() to the sample code above, after the using statement, so the PDF document will not be added to the HTML form of the web page.

+1
source

Source: https://habr.com/ru/post/903772/


All Articles