0

我正在使用带有 MVC 4 的 Winnovative HTML 到 PDF。基本示例工作正常(下面是代码)但我需要用户在“打印按钮”中单击,代码必须生成 PDF,我还需要打开PDF 生成并自动打开打印机对话框(用户希望按最少的点击量来打印文档):

在我的视图 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,就像附图一样。在此处输入图像描述

有什么帮助吗?

4

1 回答 1

0

我自己解决了。首先,我更改视图添加目标_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" />
}

其次,在 Controller 中我稍作改动并使用了 Document 并添加了 Print Javascript 函数:

... // 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");

单击打印按钮的结果是下图,它显示在浏览器的新选项卡中: 在此处输入图像描述

问候!

于 2016-06-01T19:17:35.623 回答