編輯: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");
?>
你並不是真的在做阿賈克斯。 –
可能的重複[使用JavaScript在新標籤中打開URL](http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-using-javascript) –
好的,但這不是重點。我正在將一段HTML而不是完整的DOM加載到另一頁 –