-1

while im exporting, one pdf is downloading in desired location but another extra pdf is downloading in downloads folder

How to stop downloading extra one pdf?

            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=" + Projname + ".pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            design.RenderControl(htmlWrite);
            string myText = stringWrite.ToString().Replace("&", "&");
            StringReader sr = new StringReader(myText.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            string strPath = "D:\\WeeklyReport of " + Projname + ".pdf";
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(strPath, FileMode.Create));
            pdfDoc.Open();
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
            pdfDoc.Close();
            pdfDoc.Dispose();
  • The first three lines of your code initialize the (HTTP?)Response for returning a PDF as attachment, i.e. for download; the rest creates a PDF and saves it onto your D: drive. Unless you hide something relevant, the caller should retrieve an empty PDF file as download. If you don't want that, don't initialize Response that way. – mkl May 28 '15 at 11:44
  • in local host im able to export, but in live server getting error - Access to the path 'D:\WeeklyReport of Testing project.pdf' is denied.@mkl –  May 28 '15 at 11:47
  • *Access ... denied* - Sounds natural. Why should a production server allow a web application to store something into its D:\ folder? – mkl May 28 '15 at 11:55
  • i just want to download anywhere but not in the application folder which is hosted on live. i mean if anyone clicks export button they can get the file in their system .if it saves in downloads folder also its ok, but it should download in the system of client/user @mkl –  May 28 '15 at 11:58
  • So why do you retrieve a `PdfWriter` targeting a `FileStream` on the server? – mkl May 28 '15 at 12:04
  • i just want to export a div in pdf . user can download the pdf in their system, i nt doing to download pdf in production server.can i use like this string strPath = Server.MapPath("~") + "/Temp/" + "WeeklyReport" + Projname + ".pdf"; but i dnt want to store in production server temp folder @mkl –  May 28 '15 at 12:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79004/discussion-between-coder-and-mkl). –  May 28 '15 at 12:08

1 Answers1

0

This line

PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(strPath, FileMode.Create));

of your code calls for a PdfWriter writing to a file on the server file system.

If you don't want that, you have to replace the new FileStream(strPath, FileMode.Create) by something else, e.g. the HTTP response output stream:

PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

This has potential disadvantages,

  • the HTTP response does not have a header containing the size of its payload. Some browser versions are known to have issues in such a case; and
  • in case of an error during PDF creation, the client retrieves half a PDF and not a proper error message.

If these drawbacks worry you, you may probably instead want to instantiate a MemoryStream, target the PdfWriter to it, build the PDF, retrieve the byte[] from the stream, create a content length header for the size of that array, and finally write the array to Response.OutputStream.

mkl
  • 90,588
  • 15
  • 125
  • 265