tipah
tipah

Reputation: 37

Export HTML Table to PDF using mPDF

I have am using mPDF to convert and export the data from my html table to PDF. The button on my table works and the data can be exported. However the code only passes one variable. I want to pass all the variables. I cannot pinpoint what else is missing in my code. Can anyone help me?

Here is the link button from index.php:

while($row = mysqli_fetch_array($result)){
         echo "<tr>";
         echo "<td>" .$row['eventvolunteerID'] . "</td>";
         echo "<td>" .$row['eventTitle'] . "</td>";
         echo "<td>" .$row['rollno'] . "</td>";
         echo "<td>" .$row['eventDate'] . "</td>";
         echo "<td>" .$row['eventTime'] . "</td>";
         echo "<td>" .$row['eventLimit'] .  "</td>";
         echo "<td><a href='makepdf2.php?eventvolunteerID=".$row['eventvolunteerID']."'>Create PDF</a></td>";
         echo "</tr>";

Here is my code for processing the PDF (makepdf2.php):

<?php
require_once __DIR__ . '/vendor/autoload.php';
if(isset($_GET['eventvolunteerID'])) {

$eventvolunteerID = $_GET['eventvolunteerID'];
$eventTitle = $_GET['eventTitle'];
$rollno = $_GET['rollno'];
$eventDate = $_GET['eventDate'];
$eventTime = $_GET['eventTime'];
$eventLimit = $_GET['eventLimit'];

$mpdf = new \Mpdf\Mpdf();

$data = '';
$data .= '<strong>Event Volunteer ID:</strong> ' . $eventvolunteerID . '<br/>';
$data .= '<strong>Event Title:</strong> ' . $eventTitle . '<br/>';
$data .= '<strong>Roll no.:</strong> ' . $rollno . '<br/>';
$data .= '<strong>Event Date:</strong> ' . $eventDate . '<br/>';
$data .= '<strong>Event Time:</strong> ' . $eventTime . '<br/>';
$data .= '<strong>Event Limit:</strong> ' . $eventLimit . '<br/>';
$mpdf->WriteHTML($data);
$mpdf->Output('myfile.pdf', 'D');
}
?>

Please do help. Thank you.

Upvotes: 2

Views: 1929

Answers (1)

Vantiya
Vantiya

Reputation: 627

You have added button to each row of table and when it's clicked you pass the id of data that row has and hence you export only single row.

You suppose to do add button on Top of HTML table. By clicking it you supposed to pass all the variable values to makepdf2.php and write the same Database query to fetch all the data.

require_once __DIR__ . '/vendor/autoload.php';

if(isset($_GET['checkyouraction'])) {

    // Write your Database query here to get $result

    $data = '';
    while($row = mysqli_fetch_array($result)){
        $data .= '<strong>Event Volunteer ID:</strong> ' . $row['eventvolunteerID'] . '<br/>';
        $data .= '<strong>Event Title:</strong> ' . $row['eventTitle'] . '<br/>';
        $data .= '<strong>Roll no.:</strong> ' . $row['rollno'] . '<br/>';
        $data .= '<strong>Event Date:</strong> ' . $row['eventDate'] . '<br/>';
        $data .= '<strong>Event Time:</strong> ' . $row['eventTime'] . '<br/>';
        $data .= '<strong>Event Limit:</strong> ' . $row['eventLimit'] . '<br/>';
    }

    $mpdf = new \Mpdf\Mpdf();

    $mpdf->WriteHTML($data);
    $mpdf->Output('myfile.pdf', 'D');
}

Above code generate you all the records. If you wish to display data in tabular format you can use same HTML table/tr/td structure.

Upvotes: 2

Related Questions