-1

Here is the generatepdf.php:

    <?php
include('db.php');
require_once 'vendor/autoload.php';
ob_start();

?>

<?php
$student_id = 0;

if (isset($_GET['id'])) {
    $student_id = $_GET['id'];
}

// var_dump($student_id);

$fetchUser = $conn->query("SELECT * from student where id = $student_id")->fetchAll(PDO::FETCH_ASSOC);
$subjects = $conn->query("SELECT * FROM subjects WHERE student_id = $student_id")->fetchAll(PDO::FETCH_ASSOC);

// 
// var_dump($fetchUser);

// Student Info

$name = $fetchUser[0]['name'];


$rollno = $fetchUser[0]['rollno'];

$image = $fetchUser[0]['image'];
$center = $fetchUser[0]['center'];
$division = $fetchUser[0]['division'];
$academic_year = $fetchUser[0]['academic_year'];

$class = $fetchUser[0]['class'];
$session = $fetchUser[0]['session1'] . ' - ' . $fetchUser[0]['session2'];
$father = $fetchUser[0]['father'];
$stream = $fetchUser[0]['stream'];
$totalmarks = $fetchUser[0]['totalmarks'];;
$obtmarks = $fetchUser[0]['obtmarks'];
$marksinwords = $fetchUser[0]['marksinwords'];

$percentage = ($fetchUser[0]['obtmarks'] / $fetchUser[0]['totalmarks']) * 100;
$percentage = number_format($percentage, 2);

$todaysDate = gmdate("M d, Y", strtotime('now'));


$html = '
<html>
<head>
<style>
body {font-family: sans-serif;
    font-size: 10pt;
}
p { margin: 0pt; }
table.items {
    border: 0.1mm solid #000000;
}
td { vertical-align: top; }
.items td {
    border-left: 0.1mm solid #000000;
    border-right: 0.1mm solid #000000;
}
table thead td { background-color: #EEEEEE;
    text-align: center;
    border: 0.1mm solid #000000;
    font-variant: small-caps;
}
.items td.blanktotal {
    background-color: #EEEEEE;
    border: 0.1mm solid #000000;
    background-color: #FFFFFF;
    border: 0mm none #000000;
    border-top: 0.1mm solid #000000;
    border-right: 0.1mm solid #000000;
}
.items td.totals {
    text-align: right;
    border: 0.1mm solid #000000;
}
.items td.cost {
    text-align: "." center;
}
</style>
</head>
<body>

<!--mpdf
<htmlpageheader name="myheader">
<table width="100%"><tr>
<td width="80%" style="color:#000; "><span style="font-weight: bold; font-size: 14pt;">Online Marksheet For TELEGANA UNIVERSITY</span></td>
// <td width="20%" style="text-align: right;">Date: ' . $todaysDate . '<br />
</tr></table>
</htmlpageheader>

<htmlpagefooter name="myfooter">
<div style="border-top: 1px solid #000000; font-size: 9pt; text-align: center; padding-top: 3mm; ">
Page {PAGENO} of {nb}
</div>
</htmlpagefooter>

<sethtmlpageheader name="myheader" value="on" show-this-page="1" />
<sethtmlpagefooter name="myfooter" value="on" />
mpdf-->
<img style="margin-top: -50px;padding: 15px; border: 2px solid #000; height: 100px;margin: auto !important;width: 100px;background-size: contain;background-repeat: no-repeat;background-position: center;margin-left: 80px !important;" src="images/' . $image . '" class="img-thumbnail">
<table width="100%" style="font-family: serif;" cellpadding="10"><tr>
<td width="55%"><br /><br /><span style="font-weight: bold;">Name:</span> ' . $name . '<br /><span style="font-weight: bold;">Father:</span> ' . $father . '<br /><span style="font-weight: bold;">Center:</span> ' . $center . '<br /><span style="font-weight: bold;">Division:</span> ' . $division . '<br /><span style="font-weight: bold;">Class:</span> ' . $class . '</td>
<td width="5%">&nbsp;</td>
<td width="40%"><br /><br /><span style="font-weight: bold;">Hall Ticket / Roll No:</span> ' . $rollno . '<br /><span style="font-weight: bold;">Session:</span> ' . $session . '<br /><span style="font-weight: bold;">Stream:</span> ' . $stream . '<br /><span style="font-weight: bold;">Year of Passing:</span> ' . $academic_year . '</td>
</tr></table>

<br />

<table class="items" width="100%" style="font-size: 9pt; border-collapse: collapse; " cellpadding="8">
<thead>
<tr>
<td width="15%">Sub Code</td>
<td width="40%">Subject Name</td>
<td width="10%">Theory</td>
<td width="15%">Practicals</td>
<td width="20%">Marks Secured</td>
</tr>
</thead>
<tbody>
<!-- ITEMS HERE -->
';

foreach ($subjects as $subject) {

    $html .= '<tr>';
    $html .= '<td align="center">' . $subject['subcode'] . '</td>';
    $html .= '<td align="center">' . $subject['subject_name'] . '</td>';
    $html .= '<td>' . $subject['total_marks'] . '</td>';
    $html .= '<td class="cost">' . $subject['obtained_marks'] . '</td>';
    $html .= '<td class="cost">' . $subject['marksinwords'] . '</td>';
    $html .= '</tr>';
}
$html .= '
<tr style="border-top: 2px solid #000;">
<td style="border-top: 2px solid #000;" align="center" >Percentage: ' . $percentage . '</td>
<td style="border-top: 2px solid #000;" align="center"></td>
<td style="border-top: 2px solid #000;">' . $totalmarks . '</td>
<td style="border-top: 2px solid #000;" class="cost">' . $obtmarks . '</td>
<td style="border-top: 2px solid #000;" class="cost">' . $marksinwords . '</td>
</tr>
</tbody>
</table>

</body>
</html>
';

require_once 'bootstrap.php';

$mpdf = new \Mpdf\Mpdf([
    'margin_left' => 10,
    'margin_right' => 10,
    'margin_top' => 30,
    'margin_bottom' => 25,
    'margin_header' => 10,
    'margin_footer' => 10
]);

$mpdf->SetTitle("Online Marksheet");
$mpdf->SetDisplayMode('fullpage');


$mpdf->WriteHTML($html);

$mpdf->Output();

This is working perfectly fine in XAMPP for me, and is downloading the PDF every time I load the page.

But when I hosted these exact same files on cPanel's File Manager - it's only showing me a blank page. No errors. What can I do?

I've checked the PHP versions of both my XAMPP (v8.0) and PHP(v7.3) but that doesn't seem to be the issue (unless one of you think it is?)

Dharman
  • 30,962
  • 25
  • 85
  • 135
VenoM
  • 453
  • 3
  • 9
  • 17
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Jul 16 '22 at 11:18

1 Answers1

0

To Troubleshoot:

Comment out

$mpdf->WriteHTML($html);
$mpdf->Output();

instead, just echo $html; first, just to see if all is correct.

Why? Make sure that your html is not malformed.

If there's error messages or any php headers are sent already PDF will be corrupted and will show error on render.

Things to check before output();

  1. All paths include/require paths are still correct or you uploaded them differently than your XAMPP?
  2. Why ob_start() but I do not see where you ob_get_contents() and ob_end_clean() in the code? Effectively it would keep buffering without outputting. Server might be more strict than your XAMPP and gives you warning.
  3. After all errors checked, paths corrected if were incorrect - your user input is not santised thats a big NO NO.

regarding your it's only showing me a blank page. No errors. What can I do? statement. There's either critical error and you need to check logs.

In this case, likely "require" path to bootstrap is incorrect.

If it's no errors in error logs, then likely your ob_start() would forbid you from outputting anything.

Maximus Light
  • 411
  • 3
  • 9
  • Thank you for the reply. When I echo "$html" - everything is working fine.. As for ob_start() - it's something I added later on to the code, to see if that'd help it start. I've removed ob_start() and ob_end_clean() - still no luck. – VenoM Jul 16 '22 at 10:59
  • have you checking it on your destination server or xampp? – Maximus Light Jul 16 '22 at 11:02
  • On server (cPanel) – VenoM Jul 16 '22 at 11:03
  • ok, so if echo $html is successful, no errors in error_log file not just errors shown in browsers as production servers hide errors from frontend and show all of them in logs. (that is often why you see blank page instead of errors). Then next thing to check if your PDF have enough memory to render, if too much data or high quality images might use up your allowed allocated memory and render will be terminated early. – Maximus Light Jul 16 '22 at 11:10
  • for error logs in cpanel go to "Metrics" > "Errors". – Maximus Light Jul 16 '22 at 11:13