0

Before anything else, I must say I am new on building WordPress functions. I have a website with online courses. Students have to sit exams and at the end they get a score, an average of all the exams. I am trying to build a simple dynamic PDF diploma with their name and average score, no more, which I will download by clicking on a button from a table/list. I found the class FPDF, unzipped it and uploaded it through FTP. Then, I made an include( 'fpdf183/fpdf.php'); on my functions.php in order to retrieve the class FPDF. After that, I went to my student-page.php and wrote this code to create the PDF:

 $sql ="SELECT DISTINCT(ID),`user_registered`...()";
    
        $request = $wpdb->get_results($sql); ?>
        <table id="example" class="display cell-border compact stripe" style="width:100%">
          <thead>
            <tr>
                <th>Name</th>
                <th>Average score</th>
                <th>Download Diploma</th>
            </tr>
          </thead>
          <tbody>
    
      foreach ($request as $value) {

        $pdf = new FPDF();
        if( isset($_POST['generate_pdf'])){
    
          global $pdf;
          $title_line_height = 10;
          $content_line_height = 8;        
    
          $pdf->AddPage();
          $pdf->SetFont( 'Arial', '', 42 );
          $pdf->Write(20, 'This is a sample');
          $pdf->Write(20, $value->name);
          $pdf->Write(20, $value->average);
          $pdf->Output('D','sample.pdf');
         }
         <tr>
          <td><?php echo $value->name ?></td>
          <td><?php echo $value->average ?></td>
          <td>
            <form method="post" id="as-fdpf-form">
              <button class="button button-primary" type="submit" name="generate_pdf" value="generate">Download</button>
            </form>
          </td>
         </tr>

        </tbody>
       </table>

I didn't copy the whole code, just the most important so that you get the idea. I do not know what I am doing wrong (maybe everything), but I would appreciate your help. :)

3
  • You can't output the PDF in the middle of a HTML page, you can output the PDF, or you can output a HTML page, but not both at the same time
    – Tom J Nowell
    Commented Apr 26, 2021 at 13:07
  • Thank you for your comment. I tried to do it outside HTML by creating a test.php page and putting inside just the code, it didn't work. But when I do it locally using WAMP it works perfectly. It just doesn't work with WordPress. Any suggestion? :)
    – Andrés
    Commented Apr 26, 2021 at 13:58
  • You shouldn't make direct requests to PHP files in a plugin or theme, that's very bad practice and not the solution.
    – Tom J Nowell
    Commented Apr 26, 2021 at 19:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.