【问题标题】:Avoiding PDF creation on entering the page in PHP with mpdf使用 mpdf 在 PHP 中输​​入页面时避免创建 PDF
【发布时间】:2014-08-25 02:12:25
【问题描述】:

过去几天我一直在努力使用 mpdf 在 PHP 中将表格导出为 PDF,我终于解决了大部分问题,但是,我只能创建 pdf 并将其存储在服务器文件中 打开页面时自动,相反,我希望仅在按下按钮后创建pdf。这是我的代码:

<?php if ( have_posts() ) : while (have_posts() ) : the_post(); ?>

    <h3><?php the_title() ?></h3>

    <?php ob_start(); ?>
    <div id="customers">
    <table border='1'>
        <thead>
        <tr>
            <td>
                <h1 class="table2"  >Periodo de Inversión</h1>
            </td>
            <td>
                <h1 class="table2" >Saldo Inicial</h1>
            </td>
            <td>
                <h1 class="table2" >Inversión en el Periodo</h1>
            </td>
            <td>
                <h1 class="table2" >Interés Causado en el Periodo</h1>
            </td>
            <td>
                <h1 class="table2" >Intereses Pagados</h1>
            </td>
            <td>
                <h1 class="table2" >Intereses Reinvertidos</h1>
            </td>
            <td>
                <h1 class="table2" >Saldo</h1>
            </td>
        </tr>
    </thead>
    <tbody>
    <?php

    if( have_rows('datos_especificos') ):
     ?>


        <?php  
        while ( have_rows('datos_especificos') ) : the_row();

        $icelpx = get_sub_field('interes_causado_en_el_periodo');
        $cpx = get_sub_field('cantidad_pagada');

        $crx = $icelpx-$cpx;

        $sal1x = get_sub_field('saldo');
        $ipx = get_sub_field('inversion_en_el_periodo');
        $sal2x = $sal1x+$ipx+$crx;

        $fech = get_sub_field('fecha');
        $sal1 = get_sub_field('saldo');
        $ielp = get_sub_field('inversion_en_el_periodo');
        $icelp = get_sub_field('interes_causado_en_el_periodo');
        $cp = get_sub_field('cantidad_pagada'); 
        $cr = $icelp-$cp;
        $crt = $crt+$cr;
        $sal2 = $sal1+$ip+$cr;
        $igalf = $igalf+$icelp;
        $fech2 = $fech+100;
        $ID= $the_query->ID;

            ?> 

            <tr>
                <td>
                    <p class="table"> <?php the_sub_field('fecha') ?> </p>
                </td>
                <td>
                    <p class="table"> <?php the_sub_field('saldo') ?> </p>
                </td>
                <td>
                    <p class="table"> <?php the_sub_field('inversion_en_el_periodo') ?> </p>
                </td>
                <td>
                    <p class="table"> <?php the_sub_field('interes_causado_en_el_periodo'); ?> </p>
                </td>
                <td>
                    <p class="table"> <?php the_sub_field('cantidad_pagada'); ?> </p>
                </td>
                <td>
                    <p class="table"> <?php echo $crx; ?> </p>
                </td>
                <td>
                    <p class="table"> <?php echo $sal2x; ?> </p>
                </td>
            </tr>
             <?php
        endwhile;

    else :

        // no rows found

    endif;

    ?>
</tbody>
    </table>
</div>
<br>
<table>
        <tr>
            <td>
                <p class="table"> Saldo Inicial <?php echo $fech; ?> </p>
            </td>
            <td>
                <p class="table"> <?php echo $sal1; ?> </p>
            </td>
        </tr>
            <td>
                <p class="table"> Nuevas Inversiones </p>
            </td>
            <td>
                <p class="table"> <?php echo $ielp; ?> </p>
            </td>
        <tr>
            <td>
                <p class="table"> Intereses Pagados </p>
            </td>
            <td>
                <p class="table"> <?php echo $cp; ?> </p>
            </td>
        </tr>
        <tr>
            <td>
                <p class="table"> Intereses Reinvertidos </p>
            </td>
            <td>
                <p class="table"> <?php echo $crt; ?> </p>
            </td>
        </tr>
        <tr>
            <td>
                <p class="table"> Total Intereses Generados a la fecha</p>
            </td>
            <td>
                <p class="table"> <?php echo $igalf; ?> </p>
            </td>
        </tr>
        <tr>
            <td>
                <p class="table"> Saldo Inicial <?php echo $fech2; ?> </p>
            </td>
            <td>
                <p class="table"> <?php echo $sal2; ?> </p>
            </td>
        </tr>
</table>

<?php $tableVar = ob_get_contents(); 


$mpdf=new mPDF();
$stylesheet = file_get_contents(get_template_directory_uri() . 'style.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($tableVar,2);
$mpdf->Output('test8.pdf','F');
 ?>
    <a href="" class="button">Generate pdf</a> //THIS BUTTON DOES NOTHING FOR NOW.
    <hr>

<?php endwhile; else: ?>

    <p>There are no posts or pages here</p>

<?php endif; ?>

【问题讨论】:

  • 如果您想简单一点,只需将链接更改为 POST 表单并在生成之前检查该服务器端。
  • 你能再解释一下吗?谢谢:D

标签: php wordpress pdf mpdf


【解决方案1】:

要进一步解释我的评论(这不一定是最好的方法),您可以更改链接:

<a href="" class="button">Generate pdf</a>

变成这样的表格:

<form method="post"><input type="submit" value="Generate pdf"/></form>

然后将您的 PDF 内容包装成这样:

if( 'POST' === $_SERVER['REQUEST_METHOD'] )
{
    $mpdf=new mPDF();
    $stylesheet = file_get_contents(get_template_directory_uri() . 'style.css');
    $mpdf->WriteHTML($stylesheet,1);
    $mpdf->WriteHTML($tableVar,2);
    $mpdf->Output('test8.pdf','F');
}

您可能需要考虑一些安全问题和边缘情况,但这是一个很好的基本开始

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-05
  • 1970-01-01
相关资源
最近更新 更多