1

good morning all, I found sample code to let me save current page to a pdf using itextsharp library and then save it to a temp folder within the site automatically. But the one thing that it kept doing was that it kept triggering the browser to prompt me to download or save the file. The file is already save in a temp folder, I just want to disable that browser dialog download somehow. please advise. Any help is really appreciated. thank you

    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=RequestSummaryReport.pdf");
    HttpContext.Current.Response.Charset = "";
    HttpContext.Current.Response.ContentType = "application/pdf";

    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    iTextSharp.text.Document pdfDocument = new iTextSharp.text.Document(PageSize.A3, 45, 5, 5, 5);
    //PdfWriter writer = PdfWriter.GetInstance(pdfDocument, Response.OutputStream);
    PdfWriter writer = PdfWriter.GetInstance(pdfDocument, new FileStream(Server.MapPath("~/temps/") + "mypdf.pdf", FileMode.CreateNew));

    pdfDocument.Open();

    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
    ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);

    IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDocument, writer)));

    XMLWorker worker = new XMLWorker(pipeline, true);
    XMLParser xmlParse = new XMLParser(true, worker);

    this.Page.RenderControl(htw);
    StringReader sr = new StringReader(sw.ToString());
    xmlParse.Parse(sr);
    xmlParse.Flush();
    pdfDocument.Close();
    //Response.Write(pdfDocument);
    Response.End();
user3731575
  • 121
  • 11
  • Could you post your full method (with signature)? You might want to change the Response.ContentType to "application/json" or "application/html" instead of "application/pdf" depending on what you want to return. Also, You need to change the header. Try removing everything starting with HttpContext first (the first 4 lines). – Jorge Zuverza Apr 01 '16 at 15:11
  • I agree with @JorgeZuverza that the full method signature would aid with determining your intent. – iCode Apr 01 '16 at 15:39
  • A tip for writing better questions: remove the "good morning all" (you wrote that in my late afternoon), "please advise", "any help is appreciated" etc... These words are polite if you say them to a person, but on a technical Q&A site it's actually the opposite: fluffy words come across as less polite because it adds noise and wastes the time of the reader. – Amedee Van Gasse Apr 02 '16 at 00:48

1 Answers1

1

So you want to save it on the server? Do you want to display the pdf to the browser without prompting the user to save? Perhaps the answer from this previous post on SO can help. I recall that the behavior might vary depending on the browser (IE, Chrome, Firefox, Safari). When using:

Response.AddHeader("Content-Disposition", "inline;filename=somefile.ext")

The browser will attempt to render the pdf to the browser.

If you remove the following line of code, the browser should not prompt you to save the file:

HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=RequestSummaryReport.pdf");
Community
  • 1
  • 1
iCode
  • 1,254
  • 1
  • 13
  • 16
  • the pdf is already being saved on the server. I do not want the page to popup any thing or try to open the PDF. Currently it keeps trying to send it to the browser and open it. That is what i do not want to do. thank you iCode – user3731575 Apr 01 '16 at 16:39
  • If you do not want the PDF rendered in the browser then perhaps you do not need to have the line of code for HttpContext.Response.AddHeader which is causing the save prompt. Also change the content type from application/pdf to whatever you desire that is not a PDF. – iCode Apr 01 '16 at 16:44
  • i do want to save it as pdf i just dont want the browser to try and open it – user3731575 Apr 01 '16 at 16:49
  • yes removing the response.addheader did the trick. thank you so much for your help – user3731575 Apr 01 '16 at 16:50
  • That's great! I updated the answer with the suggestion from my comment to make more clear for others that stumble upon this page. – iCode Apr 01 '16 at 17:01