-4

How can I get the view from the controller? I am trying to convert the page to an image and using the webclientPrint to send the image to a thermal printer.

I can not use window.print(); because the thermal printer may not accept the data and I also don't want to show the printer dialog to the user also.

Review

I want to capture whole web page and convert it to an image.

  function Printlabel() {
        debugger
        if (navigator.appName == "Microsoft Internet Explorer") {
            var PrintCommand = '<object ID="PrintCommandObject" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>';
            document.body.insertAdjacentHTML('beforeEnd', PrintCommand);
            PrintCommandObject.ExecWB(6, -1); PrintCommandObject.outerHTML = "";
        }
        else {
            var contents = document.getElementById("PrintArea").innerHTML;
            var frame1 = document.createElement('iframe');
            frame1.name = "frame1";
            frame1.style.position = "absolute";
            frame1.style.top = "-1000000px";
            document.body.appendChild(frame1);
            var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
            frameDoc.document.open();
            frameDoc.document.write('<html><head><title></title>');
            frameDoc.document.write('<link rel="stylesheet" type="text/css" href="/Styles/labelStyle.css" media="print">');
            frameDoc.document.write('</head><body>');
            frameDoc.document.write(contents);
            frameDoc.document.write('</body></html>');
            frameDoc.document.close();
            setTimeout(function () {
                window.frames["frame1"].focus();
                window.frames["frame1"].print();
                document.body.removeChild(frame1);
            }, 500);
        }
        setTimeout("window.open('', '_self', ''); window.close();", 5000);
        return false;
    }
vbCoder
  • 75
  • 3
  • 12
  • I'm afraid this lacks enough detail to be answerable. – Ian Jul 24 '15 at 07:54
  • what i am trying to ask was, i want to capture the whole page layout and content in the controller and convert it to a pdf file.something like print screen . – vbCoder Jul 24 '15 at 08:18
  • I think what you're after is not something that you'd do in JavaScript - you'd need a browser extension or to use a headless browser like phantomJS to generate a screenshot for you. – Ian Jul 24 '15 at 08:22
  • That said, I just noticed you want an **image** of the page?! That's not going to be possible from the server side. You can't screen shot text which the view/HTML effectively is. The browser renders this text into something that you can see, it's a markup language. You'd need to somehow render the text and then convert this rendering into an image, I can't see how this would be possible – Liam Jul 24 '15 at 08:24
  • You could render the view as a string, then convert to a pdf and send that to a printer. Capturing as an image makes no real sense as it depends on the browser / host (eg mobile vs 40" monitor, ie vs gc vs ff). Have a look here: http://stackoverflow.com/questions/1403167/how-to-email-screen-in-asp-net-mvc/2249319 – freedomn-m Jul 24 '15 at 08:30
  • i am just using screen capture as an example.Rendering the html will capture the content of the text but the style of the tags will not be capture.i show u guys my script to have better understanding – vbCoder Jul 24 '15 at 08:43

1 Answers1

0

Yes you can, pass the ActionResult from controller and getting back html from the view as a string, with this method :

    private string RenderActionResultToString(ActionResult result)
    {
        // Create memory writer.
        var sb = new StringBuilder();
        var memWriter = new StringWriter(sb);

        // Create fake http context to render the view.
        var fakeResponse = new HttpResponse(memWriter);
        var fakeContext = new HttpContext(System.Web.HttpContext.Current.Request,
            fakeResponse);
        var fakeControllerContext = new ControllerContext(
            new HttpContextWrapper(fakeContext),
            this.ControllerContext.RouteData,
            this.ControllerContext.Controller);
        var oldContext = System.Web.HttpContext.Current;
        System.Web.HttpContext.Current = fakeContext;

        // Render the view.
        result.ExecuteResult(fakeControllerContext);

        // Restore old context.
        System.Web.HttpContext.Current = oldContext;

        // Flush memory and return output.
        memWriter.Flush();
        return sb.ToString();
    }
Luca
  • 1,588
  • 2
  • 22
  • 26