2014-02-17 44 views
7

我有一個ASPX頁面,包含許多字段,當我點擊一個「導出爲PDF」按鈕時生成PDF文檔。打開一個包含ASPX回髮結果的彈出框

我現在想在JavaScript中的「打印PDF」按鈕,做這樣的事情:

w = window.open(?); 
w.print(); 
w.close(); 

其中"?"將執行相同的回發我的「導出爲PDF」按鈕。

+0

生成PDF。 CrystalExport ... ExportToHttpResponse – Serge

回答

2

啓發如果您需要提交(回傳)您的表格,新的窗口,你可以嘗試,以形成目標更改爲假,如:

var form = $("form"); 
form.attr("target", "__foo"); 

提交表格。

form.submit(); 

,並刪除目標(setitmeout(1) - 鍋在JS 「事件隊列」 結束的情況下,在我們的情況 - 提交表單後):

setTimeout(function() { form.removeAttr("target"); }, 1); 

而且,前提交你可以嘗試打開與__foo ID窗口更多的造型和形式將提交(回傳)在該窗口中,而不是一個新問題:

var wnd = window.open('', '__foo', 'width=450,height=300,status=yes,resizable=yes,scrollbars=yes'); 

但我不知道如何處理提交的窗口,捕獲onload或jquery準備好的事件。如果您可以做到,請分享解決方法,並致電wnd。打印();你可以在這個wnd裏玩iframe,也許你會找到一個解決方案。

更新時間:

嘗試在這個原型[在Chrome測試]看看:

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.0.min.js"></script> 
    <script type="text/javascript"> 
     function PrintResult() { 
      var wnd, checker, debug; 

      debug = true; 

      // create popup window 
      wnd = window.open('about:blank', '__foo', 'width=700,height=500,status=yes,resizable=yes,scrollbars=yes'); 

      // create "watermark" __loading. 
      wnd.document.write("<h1 id='__loading'>Loading...</h1>"); 

      // submit form to popup window 
      $("form").attr("target", "__foo"); 
      setTimeout(function() { $("form").removeAttr("target"); }, 1); 

      if (debug) 
      { 
       $("#log").remove(); 
       $("body").append($("<div id='log'/>")); 
      } 

      // check for watermark 
      checker = 
       setInterval(function() { 
        if (debug) $("#log").append('. '); 

        try { 

         if (wnd.closed) { clearInterval(checker); return; } 

         // if watermark is gone 
         if (wnd.document == undefined || wnd.document.getElementById("__loading") == undefined) { 
          if (debug) $("#log").append(' printing.'); 
          //stop checker 
          clearInterval(checker); 

          // print the document 
          setTimeout(function() { 
           wnd.print(); 
           wnd.close(); 
          }, 100); 
         } 
        } catch (e) { 
         // ooops... 
         clearInterval(checker); 
         if (debug) $("#log").append(e); 
        } 
       }, 10); 
     } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:Button runat="server" ID="ReportButton" OnClick="ReportRenderClick" Text="Export to PDF" OnClientClick="PrintResult()"/> 
     <asp:Button runat="server" Text="Just a button."/> 
    </div> 
    </form> 
</body> 
</html> 

這裏是cs文件:

public partial class Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void ReportRenderClick(object sender, EventArgs e) 
    { 
     Response.Clear(); 
     Thread.Sleep(2000); 
     Response.ContentType = "application/pdf"; 
     Response.WriteFile("d:\\1.pdf"); 

     //Response.ContentType = "image/jpeg"; 
     //Response.WriteFile("d:\\1.jpg"); 

     //Response.Write("Hello!"); 
     Response.End(); 
    } 
} 
+0

我接近那個解決方案,但不幸的是,一個PDF需要被嵌入到HTML體內,通過JavaScript打印。所以在這一天結束的時候,我仍然需要能夠擁有一個相當於回傳的url。 – Serge

+0

您打算支持哪種瀏覽器? –

+0

通常,Chrome,FireFox等 – Serge

1

在你的問題標籤中你有asp.net標籤,所以我想你可以訪問某種ASP.NET服務器技術。

我會建議做這樣的:

  1. 創建HttpHandler或者返回FileContentResult
  2. 在頁面中的ASP.NET MVC行動,使用此代碼下載和打印文件(實際上發現它here,粘貼,以備日後參考!)

    < A HREF = 「/路徑/到/ file.pdf」 的onClick = 「window.print();返回false」 >點擊這裏下載可打印版本< /一>

有編寫服務器端的一些好的教程:

+0

@Serge:更新了一些有用的鏈接。 –

+0

它不回答「回發」問題。但是,謝謝你的努力。 – Serge

+0

@Serge:好的,它的確如此。當從ASPX中刪除它時,將其移到「HttpHandler」將阻止任何回傳。 –

1

開放與IFrame的PDF窗口,你可以做S:

父框架內容

<script> 
window.onload=function() { 
    window.frames["pdf"].focus(); 
    window.frames["pdf"].print(); 
} 
</script> 
<iframe name="pdf" src="url/to/pdf/generation"></iframe> 

從這個https://stackoverflow.com/a/9616706

+0

如何獲得「url/to/pdf/generation」?它需要以aspx頁面的形式發送數據(回傳)。 – Serge

相關問題