转换PartialView的HTML字符串为iTextSharp的的HTMLParser [英] Convert PartialView Html to String for ITextSharp HtmlParser
问题描述
我有一个局部视图,我尝试使用iTextSharp的对HTML转换为PDF。我如何能在HTML转换成字符串,所以我可以使用ItextSharps的HTMLParser?
我已经试过这样的事情没有运气...任何想法?
VAR内容= System.IO.File.ReadAllText(Url.Action(myPartial,myController的新{n = 1},HTTP));
我已经创建了一个特殊的ViewResult类,您可以返回作为行动的结果。
您可以看到在到位桶的code(看PdfFromHtmlResult类)。
那么,它基本上是:
- 渲染通过剃刀引擎为HTML视图(或者其他注册引擎)
- 给HTML以iTextSharp的
- 返回PDF作为的ViewResult(使用正确的MIME类型,等等)。
我的ViewResult类是这样的:
公共类PdfFromHtmlResult:{的ViewResult 公共覆盖无效的ExecuteReuslt(ControllerContext上下文){
如果(上下文== NULL){
抛出新的ArgumentNullException(上下文);
}
如果(string.IsNullOrEmpty(this.ViewName)){
this.ViewName = context.RouteData.GetRequiredString(行动);
} 如果(this.View == NULL){
this.View = this.FindView(上下文)。查看;
} //首先从HTML视图的HTML
使用(VAR作家=新的StringWriter()){
VAR vwContext =新ViewContext(背景下,this.View,this.ViewData,this.TempData,作家);
this.View.Render(vwContext,作家); //转换为PDF格式 VAR响应= context.HttpContext.Response; 使用(VAR pdfStream =新的MemoryStream()){
VAR pdfDoc =新的文件();
VAR pdfWriter = PdfWriter.GetInstance(pdfDoc,pdfStream); pdfDoc.Open(); 使用(VAR htmlRdr =新StringReader(writer.ToString())){ VAR分析= iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(htmlRdr,NULL); 的foreach(在解析的VAR parsedElement){
pdfDoc.Add(parsedElement);
}
} pdfDoc.Close(); response.ContentType =应用程序/ PDF
response.AddHeader(内容处置,this.ViewName +.PDF);
字节[] = pdfBytes pdfStream.ToArray();
response.OutputStream.Write(pdfBytes,0,pdfBytes.Length);
}
}
}
}
通过正确的扩展方法(见到位桶)等,在我的控制器中的code是这样的:
公众的ActionResult MyPdf(INT ID){
变种基于myModel = findDataWithID(ID); //这个假设有一个MyPdf.cshtml / MyPdf.aspx作为视图
返回this.PdfFromHtml(基于myModel);
}
的注意的:你的方法行不通,因为你将检索服务器上的HTML,因此你失去了存储在客户端上的所有Cookie(=会话信息)。
I've got a partial view, i'm trying to use ITextSharp to convert the html to pdf. How can I convert the html to string so I can use ItextSharps HtmlParser?
I've tried something like this with no luck...any ideas?:
var contents = System.IO.File.ReadAllText(Url.Action("myPartial", "myController", new { id = 1 }, "http"));
I have created a special ViewResult class that you can return as the result of an Action.
You can see the code on bitbucket (look at the PdfFromHtmlResult class).
So what it basically does is:
- Render the view through the Razor engine (or any other registered engine) to Html
- Give the html to iTextSharp
- return the pdf as the ViewResult (with correct mimetype, etc).
My ViewResult class looks like:
public class PdfFromHtmlResult : ViewResult {
public override void ExecuteResult(ControllerContext context) {
if (context == null) {
throw new ArgumentNullException("context");
}
if (string.IsNullOrEmpty(this.ViewName)) {
this.ViewName = context.RouteData.GetRequiredString("action");
}
if (this.View == null) {
this.View = this.FindView(context).View;
}
// First get the html from the Html view
using (var writer = new StringWriter()) {
var vwContext = new ViewContext(context, this.View, this.ViewData, this.TempData, writer);
this.View.Render(vwContext, writer);
// Convert to pdf
var response = context.HttpContext.Response;
using (var pdfStream = new MemoryStream()) {
var pdfDoc = new Document();
var pdfWriter = PdfWriter.GetInstance(pdfDoc, pdfStream);
pdfDoc.Open();
using (var htmlRdr = new StringReader(writer.ToString())) {
var parsed = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(htmlRdr, null);
foreach (var parsedElement in parsed) {
pdfDoc.Add(parsedElement);
}
}
pdfDoc.Close();
response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", this.ViewName + ".pdf");
byte[] pdfBytes = pdfStream.ToArray();
response.OutputStream.Write(pdfBytes, 0, pdfBytes.Length);
}
}
}
}
With the correct extension methods (see BitBucket), etc, the code in my controller is something like:
public ActionResult MyPdf(int id) {
var myModel = findDataWithID(id);
// this assumes there is a MyPdf.cshtml/MyPdf.aspx as the view
return this.PdfFromHtml(myModel);
}
Note: Your method does not work, because you will retrieve the Html on the server, thereby you loose all cookies (=session information) that are stored on the client.
这篇关于转换PartialView的HTML字符串为iTextSharp的的HTMLParser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!