mvc pdf 转换html,MVC中网页导出为PDF怎么实现?急急急!!!

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Mvc;usingSystem.IO;usingHiQPdf;namespaceHiQPdfMvcRazorApplication.Controllers

{public classHomeController : Controller

{publicActionResult Index()

{

ViewBag.Message= "Welcome to ASP.NET MVC!";

Session["MySessionVariable"] = "My Session Variable Value assigned in Index";returnView();

}publicActionResult About()

{returnView();

}public string RenderViewAsString(string viewName, objectmodel)

{//create a string writer to receive the HTML code

StringWriter stringWriter = newStringWriter();//get the view to render

ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext, viewName, null);//create a context to render a view based on a model

ViewContext viewContext = newViewContext(

ControllerContext,

viewResult.View,newViewDataDictionary(model),newTempDataDictionary(),

stringWriter

);//render the view to a HTML code

viewResult.View.Render(viewContext, stringWriter);//return the HTML code

returnstringWriter.ToString();

}

[HttpPost]publicActionResult ConvertThisPageToPdf()

{//get the HTML code of this view

string htmlToConvert = RenderViewAsString("Index", null);//the base URL to resolve relative images and css

String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;

String baseUrl= thisPageUrl.Substring(0, thisPageUrl.Length - "Home/ConvertThisPageToPdf".Length);//instantiate the HiQPdf HTML to PDF converter

HtmlToPdf htmlToPdfConverter = newHtmlToPdf();//hide the button in the created PDF

htmlToPdfConverter.HiddenHtmlElements = new string[] { "#convertThisPageButtonDiv"};//render the HTML code as PDF in memory

byte[] pdfBuffer =htmlToPdfConverter.ConvertHtmlToMemory(htmlToConvert, baseUrl);//send the PDF file to browser

FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");

fileResult.FileDownloadName= "ThisMvcViewToPdf.pdf";returnfileResult;

}

[HttpPost]publicActionResult ConvertAboutPageToPdf()

{//get the About view HTML code

string htmlToConvert = RenderViewAsString("About", null);//the base URL to resolve relative images and css

String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;

String baseUrl= thisPageUrl.Substring(0, thisPageUrl.Length - "Home/ConvertAboutPageToPdf".Length);//instantiate the HiQPdf HTML to PDF converter

HtmlToPdf htmlToPdfConverter = newHtmlToPdf();//render the HTML code as PDF in memory

byte[] pdfBuffer =htmlToPdfConverter.ConvertHtmlToMemory(htmlToConvert, baseUrl);//send the PDF file to browser

FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");

fileResult.FileDownloadName= "AboutMvcViewToPdf.pdf";returnfileResult;

}

}

}2. Source Code Example -Index View Razor Code

@{

ViewBag.Title= "Home Page";

}

@ViewBag.Message

To learn more about ASP.NET MVC visit ASP.NET MVC Website


@using (Html.BeginForm("ConvertThisPageToPdf", "Home", FormMethod.Post, new { id = "convertForm"}))

{

/>

}
@using (Html.BeginForm("ConvertAboutPageToPdf", "Home", FormMethod.Post, new { id = "convertForm"}))

{

/>

}

@Session["MySessionVariable"].ToString()

Source Code Example-Index View MVC Code

.Master"Inherits="System.Web.Mvc.ViewPage"%>

Home Page

To learn more about ASP.NET MVC visit ASP.NET MVC Website

/>

/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值