【问题标题】:create a PDF from a single DIV从单个 DIV 创建 PDF
【发布时间】:2015-05-05 21:38:49
【问题描述】:

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

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

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

我的问题如下:

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

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

});

这是在与正常 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");

?>

【问题讨论】:

  • 你不是真的在做 ajax。
  • 好的,但这不是重点。我正在动态地将一段 HTML 而不是完整的 DOM 加载到另一个页面中
  • 不是。如果您阅读我正在寻找的问题是从单个 div 输出 pdf 的最佳方法。
  • DOMPDF 在服务器端工作,打开新窗口/选项卡/弹出窗口/iframe/向它提供 DOMPDF 输出的任何内容都没有问题。只需照顾它服务器端。也许你应该展示使用 DOMPDF 的代码。

标签: javascript jquery html ajax


【解决方案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>

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

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

几次缩小html。

【讨论】:

  • 这可能是一个有效的解决方案。你对客户端有什么建议? iframe 或新窗口或我喜欢的任何东西,因为没有最佳解决方案:)?
  • 你自己回答了 :) 就我个人而言,我更喜欢一个新标签,因为它让用户(或用户的浏览器)决定打开或保存 pdf。
猜你喜欢
  • 1970-01-01
  • 2022-11-10
  • 2015-05-24
  • 1970-01-01
  • 2015-08-18
  • 2020-10-30
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
相关资源
最近更新 更多