简体   繁体   English

如何使用itextsharp在mvc3(razor)中生成和保存视图为pdf

[英]How to generate and save view as pdf in mvc3(razor) using itextsharp

http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC3 http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC3

I have used the code given in above link and finding the issue in saving the generated pdf. 我使用了上面链接中给出的代码,并找到了保存生成的pdf的问题。 So please give me an idea for saving the pdf, so that I could able to send it in mail. 所以请给我一个保存pdf的想法,以便我能够通过邮件发送它。

OK, having read the article, I think what you are asking is how to send the PDF as an attachment to an email rather than stream it to the user in the browser. 好了,看过这篇文章后,我你要问的是如何将PDF作为电子邮件的附件发送,而不是在浏览器中将其发送给用户。

The code in the link includes this segment, which I assume you'll be able to recognize and locate in your own code: 链接中的代码包含此段,我假设您将能够识别并找到您自己的代码:

string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model);

// Let the html be rendered into a PDF document through iTextSharp.
byte[] buffer = standardPdfRenderer.Render(htmlText, pageTitle);

At that point you would have a byte array containing a PDF document. 此时,您将拥有一个包含PDF文档的字节数组。 So all you have to do to save it to disk is this: 所以你需要做的就是把它保存到磁盘上:

using(FileStream fs = new FileStream("your file name.pdf", FileMode.Create))
{
    fs.Write(buffer, 0, buffer.Length);
}

You can then use your file to create an email attachment. 然后,您可以使用您的文件来创建电子邮件附件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM