0

I can generate the PDF file and save it successfully in the server, however, I don't want the newly created file to be opened on the current browser.

I am generating multiple files and mailing them at one go.

How do I stop the browser from opening the newly created file.

        Response.ContentType = "application/pdf";
        //Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        WholeForm.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 50f, 50f, 30f, 20f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        PdfWriter.GetInstance(pdfDoc, new FileStream("\\\\sever\\d$\\PDFs\\" + hdnFileName.Text + ".pdf", FileMode.Create));
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        pdfDoc.Dispose();

        //SendEmail();

        //Response.Write(pdfDoc);
        //Response.End();

1 Answers1

0

You are setting the content type to PDF:

Response.ContentType = "application/pdf";

And you are sending bytes to the browser using this line:

PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

Remove all the lines involving the Response object, including:

Response.Cache.SetCacheability(HttpCacheability.NoCache);

The main question that remains, is: what do you want to send to the browser? If you want to inform the end user that his PDF is being saved, you may want to show some simple HTML code with this message, for instance by using a response status code 300 (redirect). Or you don't send anything by returning the response code 204 (No Content).

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165