RazorPDF将pdf文件保存到MVC4中的服务器目录

时间:2013-08-09 18:31:28

标签: asp.net-mvc asp.net-mvc-4 razorpdf

我目前正在使用MVC4中的RazorPDF组装和显示PDF,并希望在返回视图的同时将PDF文件保存到文件系统。

控制器操作中的以下代码行调用视图:

return new PdfResult(claims, "PDF");

2 个答案:

答案 0 :(得分:4)

我最终通过更改RazorPDF渲染方法的代码库将pdf写入目录系统。 Rendor方法创建与响应流关联的PdfWriter对象:

        // Associate output with response stream
        var pdfWriter = PdfWriter.GetInstance(document, viewContext.HttpContext.Response.OutputStream);
        pdfWriter.CloseStream = false;

解决方案是创建另一个与FileStream对象关联的PdfWriter对象,如下所示:

        // Create the pdf file in the directory system
        var fileStream = new FileStream(myPdfFilePath, FileMode.Create);
        var pdfWriter2 = PdfWriter.GetInstance(document, fileStream);
然后我关闭了对象:

        fileStream.Close();

        pdfWriter.Close();
        pdfWriter2.Close();

我必须基本上将RazorPDF的PdfResult和PdfView类合并到我自己的项目中并显着改变代码。原因是因为我还必须将调用发送到一个将pdf发送给用户的电子邮件类。

完整的渲染方法如下所示:

    public void Render(ViewContext viewContext, TextWriter writer)
    {
        // generate view into string
        var sb = new System.Text.StringBuilder();
        TextWriter tw = new System.IO.StringWriter(sb);
        myResult.View.Render(viewContext, tw);
        var resultCache = sb.ToString();

        // detect itext (or html) format of response
        XmlParser parser;
        using (var reader = GetXmlReader(resultCache))
        {
            while (reader.Read() && reader.NodeType != XmlNodeType.Element)
            {
                // no-op
            }

            if (reader.NodeType == XmlNodeType.Element && reader.Name == "itext")
                parser = new XmlParser();
            else
                parser = new HtmlParser();
        }

        // Create a document processing context
        var document = new Document();
        document.Open();

        // Associate output with response stream
        var pdfWriter = PdfWriter.GetInstance(document, viewContext.HttpContext.Response.OutputStream);
        pdfWriter.CloseStream = false;

        // Create the pdf file in the directory system
        var fileStream = new FileStream(myPdfFilePath, FileMode.Create);
        var pdfWriter2 = PdfWriter.GetInstance(document, fileStream);

        // this is as close as we can get to being "success" before writing output
        // so set the content type now
        viewContext.HttpContext.Response.ContentType = "application/pdf";

        // parse memory through document into output
        using (var reader = GetXmlReader(resultCache))
        {
            parser.Go(document, reader);
        }

        fileStream.Close();

        // Send an email to the claimant
        Thread.Sleep(100);
        if (File.Exists(myPdfFilePath))
        {
            var subject = "PDF Documents";

            var body = Config.GetContent(ContentParams.CLAIM_DOCUMENT_EMAIL_BODY_TEXT);

            bool success;
            string errorMessage;

            Email.Send(myEmailAddress, subject, body, out success, out errorMessage, myPdfFilePath);
        }

        pdfWriter.Close();
        pdfWriter2.Close();

    }

如果将此功能以某种方式合并到当前的RazorPDF项目中,那就太好了。

答案 1 :(得分:1)

为什么不通过网络请求获取流到网址?

Dez 10, 2015 8:50:26 AM com.kube.common.HidCommunication readFromHidDevice
SCHWERWIEGEND:  Time - Barcode Message read from the Device: 2015/12/10 08:50:26:992