php - 从 localhost 导出 pdf/下载文件时无法访问页面
问题描述
我想将数据表导出为 PDF。
我正在使用 Yii2 框架,PHP 7.3 版并使用 Mpdf 插件。
我认为代码是正确的,因为当我点击按钮导出为PDF时它没有显示任何错误信息,页面加载时间很长,最后只是显示“无法访问页面” .
当我尝试从本地目录下载文件时,也会发生此问题。
这是代码:
控制器:
public function actionExportPdf()
{
$searchModel = new BelanjaSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$html = $this->renderPartial('_pdf', ['dataProvider' => $dataProvider]);
$mpdf = new \mpdf('c', 'A4', '', '', 0, 0, 0, 0, 0, 0);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
}
pdf.php
<!DOCTYPE html>
<html>
<head>
<title>Print</title>
<style>
.page {
padding: 2cm;
}
table {
border-spacing: 0;
border-collapse: collapse;
width: 100%;
}
table td,
table th {
border: 1px solid #ccc;
}
table th {
background-color: red;
}
</style>
</head>
<body>
<div class="page">
<h1>Belanja</h1>
<table border="0">
<tr>
<th>No</th>
<th>Jenis Belanja</th>
<th>Subjenis Belanja</th>
<th>Barang</th>
<th>Jumlah</th>
<th>Satuan</th>
<th>Harga</th>
<th>Jumlah Harga</th>
</tr>
<?php
$no = 1;
foreach ($dataProvider->getModels() as $bar) {
?>
<tr>
<th><?= $no++ ?></th>
<th><?= $bar->jenis_belanja ?></th>
<th><?= $bar->subjenis_belanja ?></th>
<th><?= $bar->barang_belanja ?></th>
<th><?= $bar->jumlah_barang ?></th>
<th><?= $bar->satuan_barang ?></th>
<th><?= $bar->harga_satuan ?></th>
<th><?= $bar->jumlah_harga ?></th>
</tr>
<?php } ?>
</table>
</div>
</body>
</html>
我已经检查了服务器连接,一切都很好,没有问题。
解决方案
您没有返回任何内容,只是退出,因此浏览器将等待响应,直到timeout
达到 的值,然后让您知道它无法到达服务器,这是它到达的结论,因为它从未得到响应.
由于您直接使用mpdf,因此您应该可以使用该Output
方法将 PDF 数据作为响应直接返回给浏览器。
public function actionExportPdf()
{
$searchModel = new BelanjaSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$html = $this->renderPartial('_pdf', ['dataProvider' => $dataProvider]);
$mpdf = new \mpdf('c', 'A4', '', '', 0, 0, 0, 0, 0, 0);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list
$mpdf->WriteHTML($html);
// Change the next two lines
return $mpdf->Output();
}
请注意,我没有尝试过这个,我使用Kartik Yii2 MPDF 包装器而不是直接使用 MPDF,但是一旦你返回 data ,你所拥有的应该可以工作。
如果这不起作用,您可以更新您的项目以使用包装器,然后像小部件一样构建 PDF,这里有一个示例,在您的情况下,它将是这样的:
use kartik\mpdf\Pdf;
...
public function actionExportPdf()
{
$searchModel = new BelanjaSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$html = $this->renderPartial('_pdf', ['dataProvider' => $dataProvider]);
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_CORE, // Or Pdf::MODE_UTF8
//Name for the file
'filename' => Yii::t('app', 'document.pdf'),
// A4 paper format
'format' => Pdf::FORMAT_A4,
// portrait orientation
'orientation' => Pdf::ORIENT_PORTRAIT,
// stream to browser inline
'destination' => Pdf::DEST_BROWSER,
// your html content input
'content' => $html,
// format content from your own css file if needed or use the
// enhanced bootstrap css built by Krajee for mPDF formatting
'cssFile' => '@cssPath/pdf-document.css',
// any css to be embedded if required
'cssInline' => '',
// set mPDF properties on the fly
'options' => [
'title' => Yii::t('app', 'document.pdf'),
'autoScriptToLang' => true,
'autoLangToFont' => true
],
// call mPDF methods on the fly
'methods' => [
'SetHeader'=>[$document->getNamei18n()],
'SetFooter'=>['{PAGENO}'],
],
]);
return $pdf->render();
}
Kartik 有一个页面,这里有很多示例。
推荐阅读
- haskell - How to count list occurrences with a Data.Map?
- linux - Kubernetes emptyDir 和符号链接
- micronaut - 如何在 micronaut 声明式客户端中收到空响应?
- python - 如何在 SQLAlchemy 中查询热切连接的表?
- php - 如何动态编号(重力)表单的字段?
- django - 具有相同字段的动态循环表单
- android - 如何在android中存储硬编码的加密主密钥?
- pyspark - 在 Databricks 中哪里可以找到火花日志
- python - 如何加快 LSTM DQN 训练时间?
- javascript - 随机登陆页面(Vue / Javascript)