从单个 DIV 创建 PDF

Lel*_*eta 5 html javascript ajax jquery

编辑:DOMPDF 不是强制性的。欢迎任何其他从 DIV 打印 PDF 的解决方案。

我有一个基于单页 DOM 的 Web 应用程序,该应用程序在登录时加载一次。

然后,页面上的每个动作都会触发一些 div 来加载不同的内容和 php 页面以使用 PDO 和 AJAX 与 mysql 进行交互。

我的问题如下:

每个 php 页面将只回显需要适合该特定 DIV 的 HTML。

用户要求之一是仅考虑特定 DIV(包含内容的主要 DIV)即可打印 PDF。我选择了 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);      
    }

});
Run Code Online (Sandbox Code Playgroud)

这是在与普通 ajax 调用相同的 div 中加载 soff_pdf.php 页面。

“在一个全新的窗口中打开”怎么说?或者有没有更好的方法来获得我正在寻找的东西?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");

?>
Run Code Online (Sandbox Code Playgroud)

Egg*_*ant 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
    ));
}
Run Code Online (Sandbox Code Playgroud)

模板可以很简单:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>PDF Output</title>
        <style>
            /* your style here */
        </style>
    </head>
    <body>
        {contents}
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

编辑: 好的,我现在已经看到了您的代码,那么您可以轻松地将我的示例适应您的情况。您可以将$create_pdf_flag作为 a$_GET$_POST参数传递,并且可以使用测试

<?php if ($create_pdf_flag) { ?>
   some html here
 <?php } ?>
Run Code Online (Sandbox Code Playgroud)

几次缩小html。