save Pdf File in the particular folder instead of downloading

别等时光非礼了梦想. 提交于 2020-01-11 13:22:12

问题


I am doing html to pdf file . Its Downloading instantly . I dont want download instantly. i want to save the file in my project folder once converted.

My C# Code

string html ="<table><tr><td>some contents</td></tr></table>";

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=WelcomeLetter.pdf");

        Response.Cache.SetCacheability(HttpCacheability.NoCache);



        StringWriter sw = new StringWriter();


        HtmlTextWriter hw = new HtmlTextWriter(sw);

        StringReader sr = new StringReader(table);

        Document ResultPDF = new Document(iTextSharp.text.PageSize.A4, 25, 10, 20, 30);         


        PdfPTable Headtable = new PdfPTable(7);
        Headtable.TotalWidth = 525f;
        Headtable.LockedWidth = true;
        Headtable.HeaderRows = 5;
        Headtable.FooterRows = 2;
        Headtable.KeepTogether = true;

        HTMLWorker htmlparser = new HTMLWorker(ResultPDF);
        PdfWriter.GetInstance(ResultPDF, Response.OutputStream);
        ResultPDF.Open();
        htmlparser.Parse(sr);
        ResultPDF.Close();
        Response.Write(ResultPDF);
        Response.End();

回答1:


For saving pdf file locally in your project folder you can use FileStream class like this.

FileStream stream = new FileStream(filePath, FileMode.Create);//Here filePath is path of your project folder.

Now use this stream instead of using Response.OutputStream when you create instance of PdfWriter object.

PdfWriter.GetInstance(ResultPDF, stream);

Now do not use Responce.Write as you don't want to download your file.And close your stream at end.

stream.Close();



回答2:


I'm going to combine everyone's answer into one that you should be able to drop in and use. If this works, I would accept Manish Parakhiya's answer because that had the most important part.

First, I'm going to assume you are using a recent version of iTextSharp. I think 5.5.5 is the most recent version. Second, because of this, I'm going to restructure your code a bit in order to use the using pattern. If you're stuck on an older obsolete unsupported version like 4.1.6 you'll need to re-adjust.

Almost every tutorial out there shows you that you can bind directly the Response.OutputStream. This is 100% valid but I would argue that it is also a really bad idea. Instead, bind to a more generic MemoryStream. This makes debugging much easier and your code will port and adapt that much easier.

The below code includes comments about each of the changes and what things are actually doing. The top section is all about creating a PDF from a string of HTML. The bottom actually does something with it, including writing it to disk and/or streaming it to a browser.

//Will hold our PDF eventually
Byte[] bytes;

//HTML that we want to parse
string html = "<table><tr><td>some contents</td></tr></table>";

//Create a MemoryStream to write our PDF to
using (var ms = new MemoryStream()) {

    //Create our document abstraction
    using (var ResultPDF = new Document(iTextSharp.text.PageSize.A4, 25, 10, 20, 30)) {

        //Bind a writer to our Document abstraction and our stream
        using (var writer = PdfWriter.GetInstance(ResultPDF, ms)) {

            //Open the PDF for writing
            ResultPDF.Open();

            //Parse our HTML using the old, obsolete, not support parser
            using (var sw = new StringWriter()) {
                using (var hw = new HtmlTextWriter(sw)) {
                    using (var sr = new StringReader(html)) {
                        using (var htmlparser = new HTMLWorker(ResultPDF)) {
                            htmlparser.Parse(sr);
                        }
                    }
                }
            }

            //Close the PDF
            ResultPDF.Close();
        }
    }

    //Grab the raw bytes of the PDF
    bytes = ms.ToArray();
}

//At this point, the bytes variable holds a valid PDF file.
//You can write it disk:
System.IO.File.WriteAllBytes("your file path here", bytes);

//You can also send it to a browser:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=WelcomeLetter.pdf");
Response.BinaryWrite(bytes);
Response.Cache.SetCacheability(HttpCacheability.NoCache);

//Never do the next line, it doesn't do what you think it does and actually produces corrupt PDFs
//Response.Write(ResultPDF); //BAD!!!!!!
Response.End();



回答3:


string tempDirectory = Session.SessionID.ToString();
string location = Path.Combine(Server.MapPath(
    WebConfigurationManager.AppSettings["PathSet"].ToString()), tempDirectory);
if (!Directory.Exists(location))
{
    Directory.CreateDirectory(location);
}
string fileName="abc.pdf";
filePath = Path.Combine(location, fileName);


来源:https://stackoverflow.com/questions/28621581/save-pdf-file-in-the-particular-folder-instead-of-downloading

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!