I’ve been struggling these past days to export a table to PDF in PHP with mpdf, I finally solved most of the problems, however, I’m only able to create the pdf and store it in the server files automaticlly when opening the page, instead I would like to have the pdf only created after pressing a button. Here is my code:

<?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() . '../avoiding-pdf-creation-on-entering-the-page-in-with-mpdf/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; ?>

Read more here: Avoiding PDF creation on entering the page in PHP with mpdf


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , , , ,

Wordpress related questions and answers: