-2

I want to print the PDF document from view that contains styes and scripts. How i can read the html of the whole view after scripts and styles are applied. It should return the html after running javascript , JQuery , Angular etc scripts.

Example is that on page load jquery ajax post and fill tables.Following is the code Which Only return HTML before javascript functions running without filled tables.

 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();
        }
    }

I want to get the complete java script rendered page html like the browser page HTML

Shan Khan
  • 9,667
  • 17
  • 61
  • 111
  • I think you have a fundamental misunderstanding on how asp.net mvc works. I'd recommend reading a few intro tutorials, an introductory book, or watching some of the free intro videos on MSDN. – Philip Pittle Nov 11 '14 at 10:57
  • Javascript runs on the client. The server has no knowledge of what the client might have changed in the view after the server has sent it to the client. –  Nov 11 '14 at 10:57
  • i have developed several projects. I need to print the PDF file with view with bootstrap tables. this function only give the HTML without the scripts applied – Shan Khan Nov 11 '14 at 10:58
  • If you are trying to render a copy of a page as PDF server side, that's a very very different question. I suggest you re-ask and phrase the question in that way. Ie, How do I generate a PDF copy of my MVC view so that it includes js modifications. – Philip Pittle Nov 11 '14 at 11:12
  • @philip i have edit the question and changed the title. Kindly have a look – Shan Khan Nov 11 '14 at 11:17
  • @ShanKhan - I would highly recommend you re-ask the question. Your original question didn't really fit with the StackOverflow format, which is why it has been down-voted. Consequently, not very many people will look at it now. This is why you should create a new one; more people will look at it and you'll have a better chance for a good answer. – Philip Pittle Nov 11 '14 at 11:25
  • Also, be sure to look here: http://stackoverflow.com/questions/1324597/how-to-render-an-asp-net-mvc-view-in-pdf-format – Philip Pittle Nov 11 '14 at 11:26

1 Answers1

0

Razor/ASP.Net will only generate source, not execute client-side scripts. You need to use something that emulates a web browser to turn the client-side script into a rendered PDF.

That means it needs to understand both script and styling (i.e. just like a browser).

There are several commercial products out there, but I have personally used Essential Objects PDF converter to generate views direct to PDF. It has a built-in Javascript engine, so looks just like it will in the browser.

Please note these products are very complex (as they include complete Browser rendering engines), so most will required paid licences (for commercial use at least).

Source : How to generate PDF from HTML view after scripts and styles applied in ASP.NET MVC

TO GENERATE PDF AFTER SCRIPTS AND STYLES RENDERED

Best Tool for generating 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://stagebelweb.azurewebsites.net/race/16867";

        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;
    }
Community
  • 1
  • 1
Shan Khan
  • 9,667
  • 17
  • 61
  • 111