Winnovative HTML to PDF在生成并自动打开打印机对话框后打开PDF

时间:2016-05-31 21:33:26

标签: c# asp.net-mvc-4 pdf printing winnovative

我使用Winnovative HTML与MVC 4进行PDF。基本示例工作正常(下面是代码)但我需要用户点击"打印按钮",代码必须要生成PDF,我还需要自动打开PDF生成并打开打印机对话框(用户想按最少的点击次数来打印文档):

在我的View Index.cshtml中,我有:

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

在我的HomeController中,我有:

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

View Seccion.cshtml只有:

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

我今天所做的是点击,显示在浏览器底部下载的PDF,如附图。enter image description here

任何帮助?

1 个答案:

答案 0 :(得分:0)

我自己解决了。首先,我更改了视图添加目标_blank:

doit() {
  printf "$2\n$2\n" |
  gmx rms -s md.tpr -n index_analysis.ndx -f md.xtc -o rmsd_fc_chain_${1}.xvg -tu ns
}
export -f doit
parallel doit ::: {A..Z} ::: {1..100}

其次,在Controller中我改变了一点并使用了Document并添加了Print Javascript函数:

@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" />
}

clic打印按钮的结果是下面的图像,并在浏览器的新选项卡中显示: enter image description here

问候!

相关问题