title: Avoiding PDF creation on entering the page in PHP with mpdf
body: <p>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 <strong>automaticlly when opening the page</strong>, instead I would like to have the pdf only created after pressing a button. Here is my code:</p>
  
  <pre><code>&lt;?php if ( have_posts() ) : while (have_posts() ) : the_post(); ?&gt;
  
      &lt;h3&gt;&lt;?php the_title() ?&gt;&lt;/h3&gt;
  
      &lt;?php ob_start(); ?&gt;
      &lt;div id="customers"&gt;
      &lt;table border='1'&gt;
          &lt;thead&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;h1 class="table2"  &gt;Periodo de Inversión&lt;/h1&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;h1 class="table2" &gt;Saldo Inicial&lt;/h1&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;h1 class="table2" &gt;Inversión en el Periodo&lt;/h1&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;h1 class="table2" &gt;Interés Causado en el Periodo&lt;/h1&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;h1 class="table2" &gt;Intereses Pagados&lt;/h1&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;h1 class="table2" &gt;Intereses Reinvertidos&lt;/h1&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;h1 class="table2" &gt;Saldo&lt;/h1&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
      &lt;?php
  
      if( have_rows('datos_especificos') ):
       ?&gt;
  
  
          &lt;?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-&gt;ID;
  
              ?&gt; 
  
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;p class="table"&gt; &lt;?php the_sub_field('fecha') ?&gt; &lt;/p&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;p class="table"&gt; &lt;?php the_sub_field('saldo') ?&gt; &lt;/p&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;p class="table"&gt; &lt;?php the_sub_field('inversion_en_el_periodo') ?&gt; &lt;/p&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;p class="table"&gt; &lt;?php the_sub_field('interes_causado_en_el_periodo'); ?&gt; &lt;/p&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;p class="table"&gt; &lt;?php the_sub_field('cantidad_pagada'); ?&gt; &lt;/p&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;p class="table"&gt; &lt;?php echo $crx; ?&gt; &lt;/p&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;p class="table"&gt; &lt;?php echo $sal2x; ?&gt; &lt;/p&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
               &lt;?php
          endwhile;
  
      else :
  
          // no rows found
  
      endif;
  
      ?&gt;
  &lt;/tbody&gt;
      &lt;/table&gt;
  &lt;/div&gt;
  &lt;br&gt;
  &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;p class="table"&gt; Saldo Inicial &lt;?php echo $fech; ?&gt; &lt;/p&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;p class="table"&gt; &lt;?php echo $sal1; ?&gt; &lt;/p&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
              &lt;td&gt;
                  &lt;p class="table"&gt; Nuevas Inversiones &lt;/p&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;p class="table"&gt; &lt;?php echo $ielp; ?&gt; &lt;/p&gt;
              &lt;/td&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;p class="table"&gt; Intereses Pagados &lt;/p&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;p class="table"&gt; &lt;?php echo $cp; ?&gt; &lt;/p&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;p class="table"&gt; Intereses Reinvertidos &lt;/p&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;p class="table"&gt; &lt;?php echo $crt; ?&gt; &lt;/p&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;p class="table"&gt; Total Intereses Generados a la fecha&lt;/p&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;p class="table"&gt; &lt;?php echo $igalf; ?&gt; &lt;/p&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;p class="table"&gt; Saldo Inicial &lt;?php echo $fech2; ?&gt; &lt;/p&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;p class="table"&gt; &lt;?php echo $sal2; ?&gt; &lt;/p&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
  &lt;/table&gt;
  
  &lt;?php $tableVar = ob_get_contents(); 
  
  
  $mpdf=new mPDF();
  $stylesheet = file_get_contents(get_template_directory_uri() . 'style.css');
  $mpdf-&gt;WriteHTML($stylesheet,1);
  $mpdf-&gt;WriteHTML($tableVar,2);
  $mpdf-&gt;Output('test8.pdf','F');
   ?&gt;
      &lt;a href="" class="button"&gt;Generate pdf&lt;/a&gt; //THIS BUTTON DOES NOTHING FOR NOW.
      &lt;hr&gt;
  
  &lt;?php endwhile; else: ?&gt;
  
      &lt;p&gt;There are no posts or pages here&lt;/p&gt;
  
  &lt;?php endif; ?&gt;
  </code></pre>