首页 新闻 会员 周边 捐助

MVC中网页导出为PDF怎么实现?急急急!!!

0
[已解决问题] 解决于 2013-10-30 09:02

用的itextsharp.dll,与196k的ICSharpCode.SharpZipLib.dll有冲突

 

document.Close();这句话这老是提示未能加载文件或程序集“ICSharpCode.SharpZipLib.dll”

 

移除对ICSharpCode.SharpZipLib.dll的引用也报上面那个错误

 

换成112k的ICSharpCode.SharpZipLib.dll项目中其它压缩文件的代码又报错

 

求解

敲码的主页 敲码 | 菜鸟二级 | 园豆:202
提问于:2013-10-29 20:41
< >
分享
最佳答案
0

完整的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.IO;
using HiQPdf;

namespace HiQPdfMvcRazorApplication.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            Session["MySessionVariable"] = "My Session Variable Value assigned in Index"; 
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public string RenderViewAsString(string viewName, object model)
        {
            // create a string writer to receive the HTML code
            StringWriter stringWriter = new StringWriter();

            // 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 = new ViewContext(
                    ControllerContext,
                    viewResult.View,
                    new ViewDataDictionary(model),
                    new TempDataDictionary(),
                    stringWriter
                    );

            // render the view to a HTML code
            viewResult.View.Render(viewContext, stringWriter);

            // return the HTML code
            return stringWriter.ToString();
        }

        [HttpPost]
        public ActionResult 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 = new HtmlToPdf();

            // 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";

            return fileResult;
        }

        [HttpPost]
        public ActionResult 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 = new HtmlToPdf();

            // 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";

            return fileResult;
        }
    }
}


2. Source Code Example - Index View Razor Code

@{
    ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit ASP.NET MVC Website
</p>
<br />
@using (Html.BeginForm("ConvertThisPageToPdf", "Home", FormMethod.Post, new { id = "convertForm" }))
{
    <div id="convertThisPageButtonDiv">
        <input type="submit" value="Convert
                        This Page To PDF" 
                            />
    </div>
}
<br />
@using (Html.BeginForm("ConvertAboutPageToPdf", "Home", FormMethod.Post, new { id = "convertForm" }))
{
    <div id="convertAboutPageButtonDiv">
        <input type="submit" value="Convert
                        About Page To PDF" 
                            />
    </div>
}
<br />
<br />
<h1>
    @Session["MySessionVariable"].ToString()</h1>
<br />
<br />
<img alt="" 
            style="height: 100px" 
                            src="/Images/banner.png" />


Source Code Example - Index View MVC Code

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site
    .Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        <%: (String)ViewBag.Message %></h2>
    <p>
        To learn more about ASP.NET MVC visit ASP.NET MVC Website
    </p>
    <br />
    <form action="/Home/ConvertThisPageToPdf" id="convertThisPageForm" method="post">
    <div id="convertThisPageButtonDiv">
        <input type="submit" value="Convert
                        This Page To PDF" 
                            />
    </div>
    </form>
    <br />
    <form action="/Home/ConvertAboutPageToPdf" id="convertAboutPageForm"
                    method="post">
    <div id="convertAboutPageButtonDiv">
        <input type="submit" value="Convert
                        About Page To PDF" 
                            />
    </div>
    </form>
    <br />
    <br />
    <h1>
        <%: Session["MySessionVariable"].ToString()%></h1>
    <br />
    <br />
    <img alt="" 
                style="height: 100px" 
                                src="/Images/banner.png" />
</asp:Content>

 

http://www.codeproject.com/Articles/66948/Rendering-PDF-views-in-ASP-MVC-using-iTextSharp

这有代码下载,你下载看看。

奖励园豆:5
悟行 | 专家六级 |园豆:12559 | 2013-10-29 21:26

using HiQPdf;这是用的哪个插件啊?

敲码 | 园豆:202 (菜鸟二级) | 2013-10-29 22:20

@敲码: 上面的代码是这个HiQPdf插件,连接用的是ITextSharp插件。你可以选择一个。

悟行 | 园豆:12559 (专家六级) | 2013-10-29 22:22

@【Arnold】: 恩恩   谢谢了   我再试一下

敲码 | 园豆:202 (菜鸟二级) | 2013-10-29 22:27

@【Arnold】: 非常感谢  简单的测试了下效果可以   明天嵌入项目里看看

敲码 | 园豆:202 (菜鸟二级) | 2013-10-29 22:52

@【Arnold】: 到项目里就不行了   纠结

敲码 | 园豆:202 (菜鸟二级) | 2013-10-30 11:18

@【Arnold】: 用ajax  post不过去啊

敲码 | 园豆:202 (菜鸟二级) | 2013-10-30 11:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册