Question

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.

Was it helpful?

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....

OTHER TIPS

file_get_contents returns the content of a file, without it being parsed as PHP...

Replace

$buffer = file_get_contents($htmlFile); 

With

ob_start();
include($htmlFile);
$buffer = ob_end_clean();

What we are doing: opening an output buffer (this prevents the echo from going to the client as they appear) then we include the PHP file so it gets parsed and all. Remember all the echo's are in the output buffer, so grab it with ob_end_clean() and use the generated HTML to make a PDF.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top