Home > Mobile >  PHP PDF download is not working properly in mobile
PHP PDF download is not working properly in mobile

Time:12-30

I am creating a web application in Codeigniter 4 in which users can purchase a PDF book. After payment, the user is redirected to a page where he/she can download PDF directly. The page doesn't have any UI. The PDF download is implemented using headers. The PDF download function is working perfectly on PC, but on mobile, the file is downloading as an HTML file.

eg : filename.pdf.html.

My function is as below

public function download()
{
    $file_url = WRITEPATH . 'uploads/file.pdf';
    header("Content-type:application/pdf");
    header("Content-Disposition:attachment;filename=filename.pdf");
    readfile($file_url);
}

I have searched and tried almost all the solutions. But still, the issue is not resolved in mobile.

CodePudding user response:

Solved the issue. Looks like Codeigniter needed exit() function after readfile() Thanks to @vee who gave the tip.

CodePudding user response:

use this code and use external libary like dompdf or mpdf

    <?php

require ('vendor/autoload.php');
require 'db.php';

$query ="SELECT * FROM student";

$res=mysqli_query($conn,$query);

$html='<style>
table,th,td{
    border:2px solid black;
}
table{
    border-collapse: collapse;
    width:100%;
}
th,td{
    padding:15px;
}
.img{
    margin-left:600px;
    margin-top:-80px;
}
</style>';
$html.='<h1 style="text-align: center;">Student List</h1>';

$html.='<table>';
$html.='<tr><th>Name</th><th>Roll Number</th><th>Standard</th><th>Section</th></tr>';

while($row=mysqli_fetch_assoc($res)){
    $html.='<tr><td>'.$row['name'].'</td><td>'.$row['roll'].'</td><td>'.$row['std'].'</td><td>'.$row['sec'].'</td></tr>';
}
$html.='</table>';

$mpdf=new \Mpdf\Mpdf();
$mpdf->WriteHTML($html);
$file='Student List'.time().'.pdf';
$mpdf->output($file,'I');

?>
  • Related