I have written a code which will write the HTML body of a particular webpage into my PDF file. I am using the mPDF library for this purpose.
This is the data that is being sent to my server:
enter image description here
This is my code:
JS:
function download(){
return $.ajax({
url: '/backend_pdfgen/pdfgen.php',
type: 'POST',
data:{
"pos_news_table": JSON.stringify(pos_links_snaps),
},
cache: false
});
}
document.getElementById('download_pdf').addEventListener('click',function(){
(async()=>{
try{
let response = await download();
console.log(response)
}catch(err){
alert(err.status);
}
})();
})
PHP:
(My server script is very big, so I am just pasting a part of the script here)
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
header('Content-Type: text/plain; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new MpdfMpdf(['setAutoTopMargin' => 'stretch','mode' => 'UTF-8']);
$data_1 = json_decode($_POST["pos_news_table"],true);
foreach($data_1 as $k=>$v){
$split_val = explode("**",$v);
$title = $split_val[1];
$image = $split_val[0];
$body = $split_val[2];
$mpdf->WriteHTML('<div style="text-align:center";><table style="background-color:rgb(253, 188, 104);">
$mpdf->WriteHTML('<tr>
<td style="border: 3px solid rgb(87, 59, 22); text-align:center; width:350px; height:25px; font-size: 15px;">'.$name.'</td>
<td style="border: 3px solid rgb(87, 59, 22); text-align:center; width:350px; height:25px; font-size: 15px;">'.$category.'</td>
</tr>');
$mpdf->WriteHTML('</table></div><br>');
$mpdf->WriteHTML('<div style="text-align:center;"><img src="../'.$image.'"></div>');
/* ..................*/
}
$mpdf->Output("reports/report.pdf");
?>
When I download the pdf, I get a 403 forbidden error in my live server. But when I run the same code in my local server, the pdf gets downloaded without any error.
I have spent hours searching for the reason, but I can’t find any. Surprisingly, this error occurs only when I send the data (posted in the screenshot) above. I’ve tried sending other random data to the server, the 403 error doesn’t occur then.
Only in this case, it shows the 403 error. Why is that? Please help me.