Search code examples
phphtmlwordpresspdfhtml2pdf

How to convert dynamic php file which accepts data from database to pdf?


I am working on WordPress website. And on front-end I am giving the functionality to create a resume online & on submitting the form he will get a option to download & print the resume in PDF format. Till now everything is working fine If I am passing a simple html file to convert into PDF. But I want to create a PDF out of the fields of resume inserted in the database. If I am passing the plain html file for conversion it works fine. But what if I want to create a dynamic PDF file of the PHP file. Here is my tried code: I have used html2pdf conversion library for this.

This is a file from which the call to test.php goes.

require("html2pdf/html2fpdf.php");

$htmlFile = "test.php";
$buffer = file_get_contents($htmlFile);

$pdf = new HTML2FPDF('P', 'mm', 'Letter');
$pdf->AddPage();
$pdf->WriteHTML($buffer);
$pdf->Output('test.pdf', 'F');

And the test.php file is:

<html>
    <body>
        <?php
            $username = "";
            $password = "";
            $hostname = "";
            $dbname = "";

            // Create connection
            $con = mysqli_connect($hostname, $username, $password, $dbname);

            // Check connection
            if (mysqli_connect_errno($con))
            {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }

            echo $result = mysqli_query($con,"SELECT * FROM wp_resume ORDER BY id DESC LIMIT 1");
            echo "hioooooo";
            while($row = mysqli_fetch_array($result))
            {
                ?>
                <div>
                    <label>First Name</label><script type="text/php"><?php echo $row['firstName']; ?></script><br>
                    <label>Last Name</label><script type="text/php"><?php echo $row['lastName']; ?></script><br>
                </div>
                <?php
            }
        ?>
    </body>
</html>

Now after submitting the form I am getting a PDF file which contains only this data:

  • First Name
  • Last Name

And I want all the details of the entry inserted. Clearly this ignores the code inside the <?php ?> tags. So even if I used var_dump(), I didn't get anything.

SO please help me out in this guys. How can I pass the PHP data to PDF file.


Solution

  • Hey I got the solution for this. And the correct code is:

          require("html2pdf/html2fpdf.php"); 
    
          $html = '<h2>Resume</h2>';
          $query = "SELECT * FROM wp_resume order by id desc limit 1";
          $result = mysqli_query($con,$query);
          while($row = mysqli_fetch_array($result))
          {
              $html .= '<div>
                          <label>First Name</label> '. $row['firstName'].'<br>
                          <label>Last Name</label> '.  $row['lastName']. '<br>
                        </div>';
          }
    
          $pdf = new HTML2FPDF('P', 'mm', 'Letter'); 
          $pdf->AddPage(); 
          $pdf->WriteHTML($html); 
          $pdf->Output('test.pdf', 'F'); 
    

    I hope this will help somebody....