What's new

Help PHP exporting via MPDF

shion_

Eternal Poster
Established
Joined
Apr 28, 2016
Posts
1,171
Reaction
380
Points
462
Help bakit kaya ayaw gumana hindi nag eexport?

PHP:
<?php
require('inc/essentials.php');
require('inc/db_config.php');
require('../vendor/autoload.php'); // Autoload mPDF Composer
adminLogin();
?>


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Panel - Dashboard</title>
    <?php require('inc/links.php') ?>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>

<style>
    #dashboard-menu {
        position: fixed;
        height: 100%;
        z-index: 11;
    }

    @media screen and (max-width: 921px) {
        #dashboard-menu {
            height: auto;
            width: 100%;
        }
    }

    #main-content {
        margin-top: 10px;
    }

    button.excel-button {
        position: relative;
        left: 5px;
        bottom: 3px;
        background-color: #AD9354;
        font-size: 14px;
        border: 0px solid #ffffff;
        border-radius: 12px;
        padding: 5px 10px;
        letter-spacing: 0.5px;
        color: white;
        
    }
    button.excel-button:hover {
        background-color: #DFB35D;
    }
</style>

<body class="bg-light">

    <?php

    require('inc/header.php');

    
    // Increment the user login count when a user logs in
    if (isset($_SESSION['uId'])) {
        if (!isset($_SESSION['login_count'])) {
            $_SESSION['login_count'] = 1;
        } else {
            $_SESSION['login_count']++;
        }
    }

    $is_shutdown = mysqli_fetch_assoc(mysqli_query($connection, "SELECT shutdown FROM tbl_settings"));

    $current_reservation = mysqli_fetch_assoc(mysqli_query($connection, "SELECT
    COUNT(CASE WHEN status = 'booked' and arrival = 0 THEN 1 END) AS 'new_reservations',
    COUNT(CASE WHEN status = 'cancelled' and refund = 0 THEN 1 END) AS 'refund_reservations'
    FROM `tbl_book`"));

    $current_users = mysqli_fetch_assoc(mysqli_query($connection, "SELECT
    COUNT(*) AS 'total',
    COUNT(CASE WHEN status = 1 THEN 1 END) AS 'active',
    COUNT(CASE WHEN status = 0 THEN 1 END) AS 'inactive',
    COUNT(CASE WHEN `is_verified` = 0 THEN 1 END) AS 'unverified'
    FROM `tbl_client`"));

    $unread_inquiries = mysqli_fetch_assoc(mysqli_query($connection, "SELECT COUNT(queryNo) AS 'count'
    FROM tbl_userqueries WHERE seen=0"));

    $totalroomCancelled = mysqli_fetch_assoc(mysqli_query($connection, "SELECT COUNT(*) AS 'count1'
    FROM tbl_book WHERE `status`='cancelled'"));

    $totalpavCancelled = mysqli_fetch_assoc(mysqli_query($connection, "SELECT COUNT(*) AS 'count2'
    FROM pavilion_transactions WHERE `status`='cancelled'"));

    $totalCancelled = $totalroomCancelled['count1'] + $totalpavCancelled['count2'];
    
/////////////////////////////////////////////////////
    $totalroombooked = mysqli_fetch_assoc(mysqli_query($connection, "SELECT COUNT(*) AS 'count3'
    FROM tbl_book WHERE `status`='completed'"));

    $totalpavbooked = mysqli_fetch_assoc(mysqli_query($connection, "SELECT COUNT(*) AS 'count4'
    FROM pavilion_transactions WHERE `status`='completed'"));

    $totalbooked = $totalroombooked['count3'] + $totalpavbooked['count4'];

    ///////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////
    $totalrbooked = mysqli_fetch_assoc(mysqli_query($connection, "SELECT COUNT(*) AS 'count5'
    FROM tbl_book WHERE `status`='booked'"));

    $totalpbooked = mysqli_fetch_assoc(mysqli_query($connection, "SELECT COUNT(*) AS 'count6'
    FROM pavilion_transactions WHERE `status`='booked'"));

    $totalb = $totalrbooked['count5'] + $totalpbooked['count6'];

    ///////////////////////////////////////////////////////////////////////////////
    $total_review = mysqli_fetch_assoc(mysqli_query($connection, "SELECT COUNT(*) AS 'review'
    FROM tbl_roomreview"));



    ?>

    <div class="container-fluid" id="main-content">
        <div class="row">
            <div class="col-lg-10 ms-auto p-4 overflow-hidden">
                <div class="d-flex align-items-center justify-content-between mb-4">
                    <div class="d-flex align-items-center">
                        <h3>DASHBOARD</h3>
                        <form action="generate_excel.php"> <button type="submit" class="excel-button">
                                <i class="fa fa-file-text-o"></i> Export to Excel
                            </button>
                        </form>
                        <form method="post" action="">
        <button type="submit" name="export_pdf" class="excel-button">
            <i class="fa fa-file-pdf-o"></i> Export to PDF
        </button>
    </form>
                    </div>
                    <?php
                    if ($is_shutdown['shutdown']) {
                        echo <<<data
                        <h6 class="badge bg-danger py-2 px-3 rounded">SHUTDOWN MODE IS ACTIVE!</h6>
data;
                    }
                    ?>
                </div>
                

                <div class="row mb-4">
                    <div class="col-md-3 mb-4">
                        <a href="new_reservations.php" class="text-decoration-none">
                            <div class="card text-center text-success p-3">
                                <h6>New Reservations</h6>
                                <h1 class="mt-2 mb-0"><?php echo $current_reservation['new_reservations'] ?></h1>
                            </div>
                        </a>
                    </div>
                    <div class="col-md-3 mb-4">
                        <a href="cancelled_reservations.php" class="text-decoration-none">
                            <div class="card text-center text-warning p-3">
                                <h6>Refund Reservations</h6>
                                <h1 class="mt-2 mb-0"><?php echo $current_reservation['refund_reservations'] ?></h1>
                            </div>
                        </a>
                    </div>
                    <div class="col-md-3 mb-4">
                        <a href="user_queries.php" class="text-decoration-none">
                            <div class="card text-center text-info p-3">
                                <h6>New Inquiries</h6>
                                <h1 class="mt-2 mb-0"><?php echo $unread_inquiries['count'] ?></h1>
                            </div>
                        </a>
                    </div>
                    <div class="col-md-3 mb-4">
                        <a href="rate_review.php" class="text-decoration-none">
                            <div class="card text-center text-info p-3">
                                <h6>Ratings and Review</h6>
                                <h1 class="mt-2 mb-0"><?php echo $total_review['review']?></h1>
                            </div>
                        </a>
                    </div>
                </div>


                <div class="d-flex align-items-center justify-content-between mb-4">
                    <h5>Reservation Analytics</h5>
                    <select class="form-select shadow-none bg-light w-auto" onchange="user_analytics(this.value)">
                        <!-- <option value="">Today</option>
                        <option value="">Yesterday</option>
                        <option value="">Last 7 Days</option> -->
                        <option value="1">Last 30 Days</option>
                        <option value="2">Last 90 Days</option>
                        <!-- <option value="">Last 180 Days</option> -->
                        <option value="3">Last 365 Days</option>
                        <option value="4">All Time</option>
                    </select>
                </div>

                <div class="row mb-3">
                    <div class="col-md-3 mb-4">
                        <div>
                            <div class="card text-center text-success p-3">
                                <h6>Confirmed Reservations</h6>
                                <h1 class="mt-2 mb-0" id="total_reservations"><?php echo $totalb ?></h1>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3 mb-4">
                        <div>
                            <div class="card text-center text-success p-3">
                                <h6>Completed Reservations</h6>
                                <h1 class="mt-2 mb-0" id="active_reservations"><?php echo $totalbooked?></h1>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3 mb-4">
                        <div>
                            <div class="card text-center text-success p-3">
                                <h6>Cancelled Reservations</h6>
                                <h1 class="mt-2 mb-0" id="cancelled_reservations"><?php echo $totalCancelled ?></h1>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="d-flex align-items-center justify-content-between mb-4">
                    <h5>Users, Inqueries, Reviews Analytics</h5>
                    <select class="form-select shadow-none bg-light w-auto" onchange="user_analytics(this.value)">
                        <!-- <option value="">Today</option>
                        <option value="">Yesterday</option>
                        <option value="">Last 7 Days</option> -->
                        <option value="1">Last 30 Days</option>
                        <option value="2">Last 90 Days</option>
                        <!-- <option value="">Last 180 Days</option> -->
                        <option value="3">Last 365 Days</option>
                        <option value="4">All Time</option>
                    </select>
                </div>

                <div class="row mb-3">
                    <div class="col-md-3 mb-4">
                        <div>
                            <div class="card text-center text-success p-3">
                                <h6>New Registered</h6>
                                <h1 class="mt-2 mb-0" id="total_client">5</h1>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3 mb-4">
                        <div>
                            <div class="card text-center text-primary p-3">
                                <h6>Inqueries</h6>
                                <h1 class="mt-2 mb-0" id="total_queries">5</h1>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3 mb-4">
                        <div>
                            <div class="card text-center text-success p-3">
                                <h6>Reviews</h6>
                                <h1 class="mt-2 mb-0" id="total_review">5</h1>
                            </div>
                        </div>
                    </div>
                </div>

                <h5>USERS</h5>
                <div class="row mb-3">
                    <div class="col-md-3 mb-4">
                        <div>
                            <div class="card text-center text-info p-3">
                                <h6>TOTAL USERS</h6>
                                <h1 class="mt-2 mb-0"><?php echo $current_users['total'] ?></h1>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3 mb-4">
                        <div>
                            <div class="card text-center text-success p-3">
                                <h6>ACTIVE</h6>
                                <h1 class="mt-2 mb-0"><?php echo $current_users['active'] ?></h1>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3 mb-4">
                        <div>
                            <div class="card text-center text-warning p-3">
                                <h6>INACTIVE USERS</h6>
                                <h1 class="mt-2 mb-0"><?php echo $current_users['inactive'] ?></h1>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3 mb-4">
                        <div>
                            <div class="card text-center text-danger p-3">
                                <h6>UNVERFIED</h6>
                                <h1 class="mt-2 mb-0"><?php echo $current_users['unverified'] ?></h1>
                            </div>
                        </div>
                    </div>
                </div>


            </div>
        </div>
    </div>


    <?php require('inc/scripts.php') ?>
    <script src="scripts/dashboard.js"></script>
</body>

</html>


<?php
use Mpdf\Mpdf;


// Check if the export button is clicked
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['export_pdf'])) {
    // Create an instance of mPDF
    $mpdf = new Mpdf();

    // Start the output buffer
    ob_start();

    // Include the HTML content
    require('dashboard.php'); // Replace with the actual filename and correct path

    // Get the content from the output buffer
    $html = ob_get_contents();

    // Clear the output buffer
    ob_end_clean();

    // Set mPDF configurations if needed
    // $mpdf->Set...

    // Add the HTML content to the PDF
    $mpdf->WriteHTML($html);

    // Output the PDF to the browser or save it to a file
    $mpdf->Output('report.pdf', 'D'); // 'D' will force a download, you can use 'F' to save to a file

    exit;
}


?>
 
It appears that you are trying to export a PDF using the mPDF library in PHP. From the code you provided, it seems that you have set up the basic structure of your dashboard page and included the necessary mPDF files. However, there are a few issues that could prevent the PDF export from working correctly.

First, make sure that you have properly installed the mPDF library using Composer. You can do this by running the following command in your project directory:
Code:
composer require mpdf/mpdf

Next, check if the generate_excel.php file referenced in the form action exists and is accessible. If not, you may need to create this file and handle the export logic there.

Regarding the PDF export logic, you need to move the code that handles the PDF export outside of the HTML section. Currently, the code for exporting the PDF is placed within the same file as the HTML code, which can cause issues. You can create a separate file specifically for handling the PDF export, or you can move the export logic to the top of the file before any HTML code is output.

Here's an example of how you can rearrange the code to separate the PDF export logic:

PHP:
<?php
require('inc/essentials.php');
require('inc/db_config.php');
require('../vendor/autoload.php'); // Autoload mPDF Composer
adminLogin();

use Mpdf\Mpdf;

// Check if the export button is clicked
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['export_pdf'])) {
    // Create an instance of mPDF
    $mpdf = new Mpdf();

    // Start the output buffer
    ob_start();

    // Include the HTML content
    require('dashboard.php'); // Replace with the actual filename and correct path

    // Get the content from the output buffer
    $html = ob_get_contents();

    // Clear the output buffer
    ob_end_clean();

    // Set mPDF configurations if needed
    // $mpdf->Set...

    // Add the HTML content to the PDF
    $mpdf->WriteHTML($html);

    // Output the PDF to the browser or save it to a file
    $mpdf->Output('report.pdf', 'D'); // 'D' will force a download, you can use 'F' to save to a file

    exit;
}

?>

<!DOCTYPE html>
<html lang="en">

<!-- Rest of your HTML code... -->

<form method="post" action="">
    <button type="submit" name="export_pdf" class="excel-button">
        <i class="fa fa-file-pdf-o"></i> Export to PDF
    </button>
</form>

<!-- Rest of your HTML code... -->

</html>

Make sure to replace 'dashboard.php' in the require statement with the actual filename and correct path to the file containing the HTML content.

By separating the PDF export logic from the HTML code, you should be able to export a PDF successfully. If you encounter any errors, please provide the specific error messages so that I can assist you further.
 

Similar threads

Back
Top