2016-05-31 6 views
0

私はWinnovative HTMLをMVC 4のPDFに使用しています。基本的な例はうまくいきますが(コードは下です)、ユーザーは "Printボタンをクリックすると、コードがPDFを生成する必要があります。また、生成されたPDFを開いてプリンタのダイアログボックスを自動的に開く必要があります(ユーザは、文書を印刷するために最小のクリック数を押したい)。私が持っているCSHTML:私は私にHomeControllerでWinnovative HTML to PDF PDFを生成して開いた後にPDFを開きます。

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

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しかいない:。

今日はやっていること
@{ 
    ViewBag.Title = "Seccion ejemplo"; 
    var nombre = ViewBag.Nombre;  
} 

は、クリックは、添付画像のように、ブラウザの下部にあるダウンロードしたPDFを表示するということですenter image description here

ヘルプがありますか?

答えて

0

私は自分で解決しました。 CLICする

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

結果:私は少し変更し、ドキュメントを使用して、私は印刷の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" /> 
} 

第二に:まず、私は、ターゲット_blankを追加するビューを変更します印刷ボタンは下の画像で、ブラウザの新しいタブに表示されます: enter image description here

よろしくお願いします!

関連する問題