-1

I am using below code in asp.net to export html to pdf

protected void btnExport_Click(object sender, EventArgs e)
{
    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);
    pnlPerson.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}

This code will download the pdf file in downloads folder.. I want to download it at specific path without download prompt..

Albireo
  • 10,977
  • 13
  • 62
  • 96
  • Please simplify your code to the minimum required to demonstrate the problem; I think you'll find that your question has nothing to do with PDF and everything to do with what happens to data when it is sent to a client. – GHC Jan 19 '16 at 11:52

3 Answers3

0

You are writing the PDF to the Response.OutputStream.

Look here: how to save pdf on server map path using iTextsharp

Community
  • 1
  • 1
diiN__________
  • 7,393
  • 6
  • 42
  • 69
0

This is not a problem with C# or ASP.NET, it's the user agent (i.e. the browser) who decides where a file is downloaded, usually either after asking the user or by using a default directory set in its configuration.

A web developer has no way to alter this behaviour, and rightfully so. Letting a web site decide where a file should be downloaded would be obnoxious to the user who would then have to hunt the file down and a security flaw by letting a third-party to place harmful things in places they should never be in.

Albireo
  • 10,977
  • 13
  • 62
  • 96
0

1) IMHO, you can't bybass browser asking for download for security reasons (it is an unaccessible client app setting for general web apps). You can try to redirect to some online pdf-viewer service or provide your own instead of download.

2) I don't recommend such code like you've shown (btnExport_Click, populate download content using current page response). This causing your page become unresponsive (basically broken) after clicking 'Export' since page has only one response. I recommend to create separate page or http handler to render pdfs (according to request parameters): just redirect to this page/handler on button click and you will get correct behavior.

3) I highly recommend using wkhtmltopdf and some wrapper (for example Pechkin). It works really better out of the box in 99% of real scenarios and requires less code for achieving basic html2pdf rendering things handling links, css, images, unicode etc.

SalientBrain
  • 2,431
  • 16
  • 18