我正在 Codeigniter 4 中創建一個 Web 應用程式,用戶可以在其中購買 PDF 書籍。付款后,用戶被重定向到他/她可以直接下載 PDF 的頁面。該頁面沒有任何 UI。PDF 下載是使用headers. PDF 下載功能在 PC 上運行良好,但在移動設備上,檔案下載為 HTML 檔案。
例如:檔案名.pdf.html。
我的功能如下
public function download()
{
$file_url = WRITEPATH . 'uploads/file.pdf';
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=filename.pdf");
readfile($file_url);
}
我已經搜索并嘗試了幾乎所有的解決方案。但是,問題仍然沒有在移動設備中解決。
uj5u.com熱心網友回復:
解決了這個問題。看起來
Codeigniterexit()在readfile()感謝提供提示的@vee之后需要功能。
uj5u.com熱心網友回復:
使用此代碼并使用外部庫,如 dompdf 或 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');
?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/398449.html
上一篇:Django模型欄位值回傳空白?
