1

In my method in controller I use the following code to save pdf.

HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(htmlContent);
            HtmlNode node = doc.GetElementbyId("DetailsToPDF");
            HtmlToPdfConverter htmlToPdf = new HtmlToPdfConverter();
            var pdfBytes = htmlToPdf.GeneratePdf("<html><body>" + node.InnerHtml + "</body></html>");

            Response.ContentType = "application/pdf";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-Disposition", "attachment; filename=TEST.pdf");
            Response.BinaryWrite(pdfBytes);
            Response.Flush();
            Response.End();

Everything has passed without any exceptions in debugger. However file is not saved. What am I doing wrong?

3
  • This doesn't actually save the file. It'll only ask your browser to handle the file. Depending on how the browser is configured it might save it, it might display it Commented Sep 24, 2017 at 21:59
  • Browser(Chrome) didn't show anything. Where should I configure it? Commented Sep 24, 2017 at 22:01
  • Actually you're writing the file contents to the HTTP response using BinaryWrite, it doesn't returning any file to download. Try to return FileResult/FileContentResult, which showing option to open or download the file in browser. Commented Sep 25, 2017 at 1:42

2 Answers 2

2

The recommended way to return a File in ASP.NET MVC is using the File() helper method:

public ActionResult Download()
{
  // Starting with pdfBytes here...
  // ...
  var pdfBytes = htmlToPdf.GeneratePdf("<html><body>" + node.InnerHtml + "</body></html>");
  var contentDisposition = new System.Net.Mime.ContentDisposition
  {
      FileName = "TEST.pdf",
      Inline = false
  };
  Response.AppendHeader("Content-Disposition", contentDisposition.ToString());
  return File(pdfBytes, "application/pdf");
}
Sign up to request clarification or add additional context in comments.

9 Comments

In this solution, can I set encoding on UTF.8?
Is there any particular reason for that? The PDF file has it's own encoding, the File result is returning only the bytes. If you get some malformed characters, the issue might lie somewhere in the PDF generation process itself. But you can still try to call Response.ContentEncoding = System.Text.Encoding.UTF8; or Response.Charset = "utf-8";and see if it helps
Reason: Polish letters :)
It doesn't help.
So maybe you just need to put the Encoding into the HTML Text? After <html> and before <body> in the code above, put: <head><meta http-equiv="content-type" content="text/html; charset=utf-8" /></head>. If it doesn't work, check if the "node.InnerHtml" (I don't know where it's coming from) has the right letters
|
0
string path = Server.MapPath("~/Content/files/newPDFFile.pdf");
    WebClient client = new WebClient();
    Byte[] buffer = client.DownloadData(path);
    if (buffer != null)
    {

        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=" + "PDFfile.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.BinaryWrite(buffer);
        Response.End();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.