Save the generated PDF file directly to the server directory folder without a user prompt

One of my friends gave me this task so I could get a little bit of pre-programming. I am currently doing a html page to pdf conversion using itext sharp and sending the PDF as an attachment. My idea is to first save the pdf file to the server machine in the "ToBeEmailedPDF" folder before using it as an email attachment. My concern is that this dialog, as you can see in the picture, appears using the code I have below.alt text

StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
pnlPDF.RenderControl(hw); // pnlPDF contains the html contents to be converted to pdf
string htmlDisplayText = sb.ToString();
Document document = new Document();
MemoryStream ms = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, ms);
StringReader se = new StringReader(htmlDisplayText);
HTMLWorker obj = new HTMLWorker(document);
document.Open();
obj.Parse(se);
// step 5: we close the document
document.Close();
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=report.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();

      

I know many of you know the best way or know how to solve my problem. Please help me. Thank!

+2


source to share


3 answers


There is a big difference between the client and server side.



Response

the class can output the content to the client machine, if you need to save the file to the server use something like File.WriteAllBytes

( msdn )

+3


source


What you are trying to achieve is not possible for security reasons. Only the user can decide where he wants to save the attachment. Imagine if it was possible: you could save any viruses in some folders on the user's computer. As an alternative to displaying the Save dialog box, you can open the PDF file:



Response.AddHeader("Content-Disposition", "inline; filename=report.pdf");

      

+2


source


You can save it using the following code:

PdfWriter.GetInstance(pdfDoc, new FileStream(context.Server.MapPath("~") + "/PDF/" + reportName + ".pdf", FileMode.Create));

      

0


source







All Articles