title: generate weekly report pdf with mpdf - functions.php
body: <p>I'm using mpdf on wordpress for generating PDF files. I'm working on a functionality which will send weekly report to my users and that report should be send as email and pdf will be attached in email.</p>
  
  <p>My issue is that I'm running code in functions.php file because for running this code every week, I'm going to use server side cron job and my function should be inside functions.php file to execute it. So I added this code in functions.php file:</p>
  
  <pre><code>function weeklyReportFunc(){
      include('mpdf/mpdf.php');
      $mpdf = new mPDF();
      ob_start();
      require get_template_directory() . '/includes/report.php';
      $x = ob_get_contents();
      ob_end_clean();
      $mpdf-&gt;WriteHTML($x);
      $today = date('Y-m-d');
      $pdfName = 'weekly-report-'.$today;
      $mpdf-&gt;Output($pdfName.'.pdf', 'D');
  }
  </code></pre>
  
  <p>And this shows me below error:</p>
  
  <pre><code>Warning: Cannot modify header information - headers already sent by (output started at 
  /home/user/public_html/doms/wp-admin/includes/template.php:1995) in /home/user/public_html/
  doms/wp-content/themes/mytheme/mpdf/mpdf.php on line 8314
  
  Warning: Cannot modify header information - headers already sent by (output started at 
  /home/user/public_html/doms/wp-admin/includes/template.php:1995) in   
  /home/user/public_html/doms/wp-content/themes/mytheme/mpdf/mpdf.php on line 1706
  mPDF error: Some data has already been output to browser, can't send PDF file
  </code></pre>
  
  <p>How can I solve this? Maybe I need to use my function in some action? but which one? Any ideas please?</p>