Winnovative HTML to PDF open PDF after generated and open printer dialog automatically

1k Views Asked by At

I'm using Winnovative HTML to PDF with MVC 4. The basic example works fine (below is the code) but I need the user do a click in "Print button", the code must to generate the PDF, also I need open the PDF generated and open printer dialog box automatically (the user wants to press the least amount of clicks to print a document):

In my View Index.cshtml I have:

@using (Html.BeginForm("ImpresionSeccionVistaActual", "Home", FormMethod.Post))
{       
    <input type="submit" id="impresion" name="impresion" value="imprimir sólo sección" />
}

In my HomeController I have:

using Winnovative;

        [HttpPost]
        public ActionResult ImpresionSeccionVistaActual(FormCollection collection)
        {
            object model = null;
            ViewDataDictionary viewData = new ViewDataDictionary(model);

            // transmit the posted data to view
            viewData.Add("nombre", collection["nombre"]);

            // The string writer where to render the HTML code of the view
            StringWriter stringWriter = new StringWriter();

            // Render the Index view in a HTML string
            ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext, "Seccion", null);
            ViewContext viewContext = new ViewContext(
                    ControllerContext,
                    viewResult.View,
                    viewData,
                    new TempDataDictionary(),
                    stringWriter
                    );
            viewResult.View.Render(viewContext, stringWriter);

            // Get the view HTML string
            string htmlToConvert = stringWriter.ToString();

            // Get the base URL
            String currentPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
            String baseUrl = currentPageUrl.Substring(0, currentPageUrl.Length - "Home/Seccion".Length);

            // Create a HTML to PDF converter object with default settings
            HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

            //htmlToPdfConverter.OpenAction.Action = New PdfActionJavaScript("print()")

            htmlToPdfConverter.JavaScriptEnabled = true;            

            // Set license key received after purchase to use the converter in licensed mode
            // Leave it not set to use the converter in demo mode
            htmlToPdfConverter.LicenseKey = key;

            // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed
            // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish
            htmlToPdfConverter.ConversionDelay = 2;

            // Convert the HTML string to a PDF document in a memory buffer
            byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlToConvert, baseUrl);

            // Send the PDF file to browser
            FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");
            fileResult.FileDownloadName = "ImpresionSeccionVistaActual.pdf";            

            return fileResult;
        }

The View Seccion.cshtml only have:

@{
    ViewBag.Title = "Seccion ejemplo";
    var nombre = ViewBag.Nombre;    
}

What I have done today is that the click, display the PDF downloaded at the bottom of the browser, like the attached image.enter image description here

Any help?

1

There are 1 best solutions below

0
Hernaldo Gonzalez On

I solved by myself. First, I change the View adding target _blank:

@using (Html.BeginForm("ImprimeVistaExterna", "Home", FormMethod.Post, new { target= "_blank" }))
{
    <input type="submit" id="impresion2" name="impresion2" value="ver pdf generado directo en otra pestaña" />
}

Second, in the Controller I change a little and used a Document and I added the Print Javascript function:

... // same code
byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
MemoryStream stream = new MemoryStream(outPdfBuffer);

Document document = new Document(stream);
document.LicenseKey = "myKey";
document.OpenAction.Action = new PdfActionJavaScript("print()");
byte[] b = document.Save();
Stream strm = new MemoryStream(b);

Response.AppendHeader("content-disposition", "inline; filename=file.pdf");
return new FileStreamResult(strm, "application/pdf");

The result to clic the print button is the image below and this show in a new tab in browser: enter image description here

Regards!