从本地主机导出 pdf/downloading 文件时无法访问页面
can't reach page while export pdf/downloading file from localhost
我想将数据表导出为 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>
我已经检查过服务器连接,一切正常,没有问题。
您没有 return 任何东西,只是退出,所以浏览器将等待响应,直到达到 timeout
的值,然后让您知道它无法达到服务器,这是它得出的结论,因为它从未得到响应。
既然你是直接使用mpdf,你应该可以使用Output
方法将PDF数据直接return到浏览器作为响应。
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 wrapper 而不是直接使用 MPDF,但是你所拥有的应该可以工作 一旦你 return 数据 .
如果这不起作用,您可以更新您的项目以使用包装器,然后像小部件一样构建 PDF,有一个示例 here,在您的情况下它是这样的:
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 有 page with a lot of examples here.
我想将数据表导出为 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>
我已经检查过服务器连接,一切正常,没有问题。
您没有 return 任何东西,只是退出,所以浏览器将等待响应,直到达到 timeout
的值,然后让您知道它无法达到服务器,这是它得出的结论,因为它从未得到响应。
既然你是直接使用mpdf,你应该可以使用Output
方法将PDF数据直接return到浏览器作为响应。
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 wrapper 而不是直接使用 MPDF,但是你所拥有的应该可以工作 一旦你 return 数据 .
如果这不起作用,您可以更新您的项目以使用包装器,然后像小部件一样构建 PDF,有一个示例 here,在您的情况下它是这样的:
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 有 page with a lot of examples here.