如何生成在ASP.NET MVC应用脚本和样式后,从HTML浏览PDF [英] How to generate PDF from HTML view after scripts and styles applied in ASP.NET MVC

查看:114
本文介绍了如何生成在ASP.NET MVC应用脚本和样式后,从HTML浏览PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成的我查看HTML生成PDF文档。它,当它在浏览器中打开样式和脚本应用。
我尝试以下code,但它只是给出了视图之前修改脚本的HTML。
我需要的脚本后修改得到的观点HTML文本一样改变了相同的浏览器。

 公共字符串RenderRazorViewToString(字符串的viewName,对象模型)
{
        ViewData.Model =模型;
        使用(VAR SW =新的StringWriter())
        {
            VAR的ViewResult = ViewEngines.Engines.FindPartialView(ControllerContext,
                                                                     的viewName);
            VAR viewContext =新ViewContext(ControllerContext,viewResult.View,
                                         ViewData的,TempData的,SW);
            viewResult.View.Render(viewContext,SW);
            viewResult.ViewEngine.ReleaseView(ControllerContext,viewResult.View);
            返回sw.GetStringBuilder()的ToString()。
        }
}


解决方案

最好的工具,我已经找到,并用于生成的JavaScript的PDF和风格渲染视图或HTML页面的 phantomJS

下载的.exe文件中的 rasterize.js 功能的示例文件夹中的exe根发现把里面的解决方案。

继code生成PDF文件:

 公众的ActionResult DownloadHighChartHtml()
    {
        串SERVERPATH =使用Server.Mappath(〜/ phantomjs /);
        字符串文件名= DateTime.Now.ToString(ddMMyyyy_hhmmss)+.PDF;
        字符串URL =htt​​p://wwwabc.com;        新主题(新的ParameterizedThreadStart(X =>
        {
            ExecuteCommand(的String.Format(CD {0}急症:放大器; phantomjs rasterize.js {1} {2} \\A4 \\,SERVERPATH,URL,文件名));
                               // E:是的使用Server.Mappath驱动器
        }))。开始();        VAR文件路径= Path.Combine(使用Server.Mappath(〜/ phantomjs /),文件名);        VAR流=新的MemoryStream();
        字节[]字节= DoWhile(文件路径);        Response.ContentType =应用程序/ PDF
        Response.AddHeader(内容处置,附件;文件名= Image.pdf);
        Response.OutputStream.Write(字节,0,bytes.Length);
        到Response.End();
        返回RedirectToAction(HighChart);
    }    私人无效ExecuteCommand(字符串命令)
    {
        尝试
        {
            的ProcessStartInfo ProcessInfo;
            工艺过程;            ProcessInfo =新的ProcessStartInfo(cmd.exe的,/ K+命令);            ProcessInfo.CreateNoWindow = TRUE;
            ProcessInfo.UseShellExecute = FALSE;            流程=的Process.Start(ProcessInfo);
        }
        赶上{}
    }
    私人字节[] DoWhile(字符串文件路径)
    {
        字节[]字节=新的字节[0];
        布尔失败= TRUE;        而(失败)
        {
            尝试
            {
                使用(的FileStream文件=新的FileStream(文件路径,FileMode.Open,FileAccess.Read))
                {
                    字节=新的字节[file.Length]
                    file.Read(字节,0,(int)的file.Length);
                }                失败= F​​ALSE;
            }
            抓住
            {
                Thread.sleep代码(1000);
            }
        }        System.IO.File.Delete(文件路径);
        返回字节;
    }

I want to generate HTML of my View to generate PDF document. It has styles and scripts applied when it opens in browser. I tried the following code but it only gives the html of view before scripts modifications. I need to get HTML of view after scripts modifications like text changed same as browser.

public string RenderRazorViewToString(string viewName, object model)
{
        ViewData.Model = model;
        using (var sw = new StringWriter())
        {
            var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext,
                                                                     viewName);
            var viewContext = new ViewContext(ControllerContext, viewResult.View,
                                         ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);
            viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
            return sw.GetStringBuilder().ToString();
        }
}

解决方案

Best Tool i have found and used for generating PDF of javascript and styles rendered views or html pages is phantomJS.

Download the .exe file with the rasterize.js function found in root of exe of example folder and put inside solution.

Following code generate PDF File :

 public ActionResult DownloadHighChartHtml()
    {
        string serverPath = Server.MapPath("~/phantomjs/");
        string filename = DateTime.Now.ToString("ddMMyyyy_hhmmss") + ".pdf";
        string Url = "http://wwwabc.com";

        new Thread(new ParameterizedThreadStart(x =>
        {
            ExecuteCommand(string.Format("cd {0} & E: & phantomjs rasterize.js {1} {2} \"A4\"", serverPath, Url, filename));
                               //E: is the drive for server.mappath
        })).Start();

        var filePath = Path.Combine(Server.MapPath("~/phantomjs/"), filename);

        var stream = new MemoryStream();
        byte[] bytes = DoWhile(filePath);

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=Image.pdf");
        Response.OutputStream.Write(bytes, 0, bytes.Length);
        Response.End();
        return RedirectToAction("HighChart");
    }



    private void ExecuteCommand(string Command)
    {
        try
        {
            ProcessStartInfo ProcessInfo;
            Process Process;

            ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + Command);

            ProcessInfo.CreateNoWindow = true;
            ProcessInfo.UseShellExecute = false;

            Process = Process.Start(ProcessInfo);
        }
        catch { }
    }


    private byte[] DoWhile(string filePath)
    {
        byte[] bytes = new byte[0];
        bool fail = true;

        while (fail)
        {
            try
            {
                using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    bytes = new byte[file.Length];
                    file.Read(bytes, 0, (int)file.Length);
                }

                fail = false;
            }
            catch
            {
                Thread.Sleep(1000);
            }
        }

        System.IO.File.Delete(filePath);
        return bytes;
    }

这篇关于如何生成在ASP.NET MVC应用脚本和样式后,从HTML浏览PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆