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

Controller 操作中的以下代码行调用 View :

return new PdfResult(claims, "PDF");

最佳答案

通过更改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项目中,那就太好了。

关于asp.net-mvc - RazorPDF将pdf文件保存到MVC4中的服务器目录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18153340/

10-09 03:38