简体   繁体   English

使用 mPDF 将 HTML 表导出到 PDF

[英]Export HTML Table to PDF using mPDF

I have am using mPDF to convert and export the data from my html table to PDF.我正在使用 mPDF 将 html 表中的数据转换并导出到 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:这是 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):这是我处理 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.谢谢你。

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.您已将按钮添加到表格的每一行,当单击它时,您会传递该行具有的数据的 ID,因此您只导出单行。

You suppose to do add button on Top of HTML table.您应该在 HTML 表的顶部添加按钮。 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.通过单击它,您应该将所有变量值传递给 makepdf2.php 并编写相同的数据库查询以获取所有数据。

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.如果您希望以表格格式显示数据,您可以使用相同的 HTML table/tr/td 结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM