将Pdf文件保存在特定文件夹中而不是下载 [英] save Pdf File in the particular folder instead of downloading

查看:172
本文介绍了将Pdf文件保存在特定文件夹中而不是下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做html到pdf文件。它立即下载。我不想立即下载。我希望转换后将文件保存在我的项目文件夹中。

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.

我的C#代码

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();


推荐答案

我将把每个人的答案合并为一个你应该能够投入使用。如果这样可行,我会接受Manish Parakhiya的回答,因为那里有最重要的部分。

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.

首先,我假设您使用的是最新版本的iTextSharp。我认为5.5.5是最新版本。其次,因为这个原因,我将重新构建你的代码,以便使用使用模式。如果您坚持使用较旧的不受支持的版本(如4.1.6),则需要重新调整。

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.

几乎每个教程都会向您显示您可以绑定直接 Response.OutputStream 。这是100%有效,但我认为这也是一个非常糟糕的主意。相反,绑定到更通用的 MemoryStream 。这使得调试变得更加容易,并且您的代码将更容易移植和调整。

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.

以下代码包含有关每个更改的评论以及实际执行的操作。最上面的部分是关于从HTML字符串创建PDF。底部实际上做了一些事情,包括将其写入磁盘和/或将其流式传输到浏览器。

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();

这篇关于将Pdf文件保存在特定文件夹中而不是下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆