首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Winnovative HTML to PDF打开PDF后生成并自动打开打印机对话框

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

Stack Overflow用户
提问于 2016-06-01 05:33:26
回答 1查看 744关注 0票数 0

我使用Winnovative HTML与MVC 4的PDF。基本的例子工作良好(下面是代码),但我需要用户做一个点击“打印按钮”,代码必须生成的PDF,我还需要打开PDF生成和打开打印机对话框自动(用户想按下最少的点击打印文档):

在我的视图Index.cshtml中,我有:

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

在我的HomeController中,我有:

代码语言:javascript
复制
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;
        }

视图Seccion.cshtml只有:

代码语言:javascript
复制
@{
    ViewBag.Title = "Seccion ejemplo";
    var nombre = ViewBag.Nombre;    
}

我今天所做的是点击,在浏览器的底部显示下载的PDF,就像附加的图像一样。

有什么帮助吗?

EN

回答 1

Stack Overflow用户

发布于 2016-06-02 03:17:36

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

代码语言: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" />
}

其次,在控制器中,我做了一点更改,使用了一个文档,并添加了打印Javascript功能:

代码语言: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");

循环打印按钮的结果如下图所示,并显示在浏览器的新选项卡中:

致以问候!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37555993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档