2015-05-05 129 views
4

編輯:DOMPDF不是強制性的。歡迎任何其他從DIV打印PDF的解決方案。從單個DIV創建PDF

我有一個基於單頁面DOM的web應用程序,它在登錄時加載一次。

然後,頁面上的每個動作都會觸發一些div加載不同的內容和php頁面,以使用PDO和AJAX與mysql進行交互。

我的問題是這樣的:

每個PHP頁面會響應只是需要適合特定DIV的HTML。

用戶需求之一是打印PDF的可能性,只考慮特定的DIV(主要內容)。我選擇了DOMPDF來完成這項工作,但它需要一個有效的html標記才能運行,而不僅僅是一個部分。

我的想法是:添加一個複選框來選擇PDF格式我使用相同的ajax調用,但將它重定向到一個新的PHP頁面,它將回顯完整的html標記。

問題是,結果應該在新頁面(或iframe中)打開,以便被DOM PDF正確解析。

如何得到這一點?

我實際的AJAX調用:

$('#livesearch').on("click",'a', function(event){ 
    event.preventDefault(); 
    var tip = $(this).attr("href"); 
    var descr = $(this).text(); 
    $('#anag_search').val(descr); 
    $('#livesearch').hide(); 
    if ($('#pdfprint').prop('checked')) { 
     $('#upper').load('sofferenze/soff_pdf.php?soff='+tip);  
    }else{ 
     $('#upper').load('sofferenze/soff.php?soff='+tip);  
    } 

}); 

這是加載soff_pdf.php頁面在同一div作爲正常Ajax調用。

如何說「打開一個全新的窗口」?還是有更好的方法來獲得我正在尋找的東西? 的soff_pdf.php(相關部分):

<?php 
require '../../session_handler.inc.php'; 
require_once '../../global_functions.php'; 
require_once '../../dompdf/dompdf_config.inc.php'; 
session_start(); 
$actusr = $_SESSION['id']; 
$id_soff = $_GET['soff']; 
$soff_name = soff2name($id_soff,$pdo); 
//query to fetch data are here 
?> 
<?php ob_start(); ?> 
<!DOCTYPE html> 

<html> 
    <head> 
<meta content="charset=utf-8" /> 
<title>DEVELOPMENT SYSTEM</title> 
<link rel="stylesheet" type="text/css" href="../../css/style.css" media="screen" /> 
<link rel="stylesheet" type="text/css" href="../../css/style_print.css" media="print"/> 
<script type="text/javascript" src="../../js/jquery-1.9.1.min.js"></script> 
</head> 
<body> 
<br><br> 
<div id="accordion"> 
    <h3>Crediti inclusi nella sofferenza</h3> 
    <div> 
    <table class="report"> 
     <caption class="report">Crediti Chirografari</caption> 
     <thead> 
     <tr> 
      <!--<th class="report">Codice</th>--> 
      <th class="report">Sofferenza</th> 
      <th class="report">GBV</th> 
      <th class="report">Data GBV</th> 
      <th class="report">Titolare</th> 
      <th class="report">Serie</th> 
      <th class="report">Data Acq.</th> 
      <th class="report">Cedente</th> 
      <th class="report">Tipo</th> 
      <th class="report">Originator</th> 
       <th class="report">Stato</th> 
     </tr> 
     </thead> 
     <tbody> 
     <?php if(count($crediti_chiro) >0){ foreach($crediti_chiro as $credito_chiro){ ?> 
     <tr> 
      <!--<td class="report"><?php //echo $credito_chiro['id_cre']; ?></td>--> 
      <td class="report"><?php echo soff2name($credito_chiro['cod_soff'],$pdo); ?></td> 
      <td class="report"><?php echo num2cur($credito_chiro['gbv_tot']); ?></td> 
      <td class="report"><?php echo mysql2table($credito_chiro['gbv_data']); ?></td> 
      <td class="report"><?php echo get_name('veicoli',$credito_chiro['titolare'],'nome',$pdo); ?></td> 
      <td class="report"><?php echo get_name('serie',$credito_chiro['cod_serie'],'serie',$pdo); ?></td> 
      <td class="report"><?php echo mysql2table($credito_chiro['data_acq']); ?></td> 
      <td class="report"><?php echo prot2name($credito_chiro['cedente'],$pdo); ?></td> 
      <td class="report"><?php echo get_name('diz_cred',$credito_chiro['tipo'],'descrizione',$pdo); ?></td> 
      <td class="report"><?php echo prot2name($credito_chiro['originator'],$pdo); ?></td> 
      <td class="report"><?php echo $credito_chiro['stato']; ?></td> 
     </tr> 
     <?php }}else{ ?> 
      <td class="report" colspan="10">Nessun credito chirografario caricato</td> 
     <?php }?> 
     </tbody> 
    </table> 
<br><br> 

</div> 
<!-- more html here --> 
</body></html> 
<?php 

$html = ob_get_clean(); 
$dompdf = new DOMPDF(); 
$dompdf->load_html($html); 
$dompdf->render(); 
$dompdf->stream("sample.pdf"); 

?> 
+0

你並不是真的在做阿賈克斯。 –

+0

可能的重複[使用JavaScript在新標籤中打開URL](http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-using-javascript) –

+0

好的,但這不是重點。我正在將一段HTML而不是完整的DOM加載到另一頁 –

回答

1

假設你打開一個彈出窗口或新標籤頁或任何你喜歡的客戶端,你想重新使用已經呼應你的HTML同一個腳本:

if ($create_pdf_flag) ob_start(); 

echo 'some html...'; 
echo 'some more html...'; 
echo 'some other html...'; 
echo 'yet some more html...'; 

if ($create_pdf_flag) { 
    $contents = ob_get_contents(); 
    ob_end_clean(); 

    $html = file_get_contents('/path/to/your/template.html'); 
    $html = str_replace('{contents}', $contents, $html); 

    $DOMPDF = new DOMPDF(); 
    $DOMPDF->load_html($html); 
    $DOMPDF->render(); 
    $DOMPDF->stream('Filename.pdf', array(
     'compress'  => 1, 
     'Attachment' => 0 
    )); 
} 

模板可以像這樣簡單的事情:

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="utf-8"> 
     <title>PDF Output</title> 
     <style> 
      /* your style here */ 
     </style> 
    </head> 
    <body> 
     {contents} 
    </body> 
</html> 

編輯:好我看過你的代碼中沒有w,那麼你可以很容易地調整我的例子到你的案例。您可以通過$create_pdf_flag作爲$_GET$_POST參數,你可以使用測試

<?php if ($create_pdf_flag) { ?> 
    some html here 
<?php } ?> 

幾次收縮的HTML。

+0

這可能是一個有效的解決方案。你在客戶端有什麼建議? Iframe或新窗口或任何我喜歡的原因沒有一個最佳的解決方案:)? –

+1

你自己回答:)我個人更喜歡新標籤,因爲它留給用戶(或用戶的瀏覽器)打開或保存pdf的決定。 – Eggplant