Php 如何使用Yii2中的kartik mpdf将HTML页面导出为PDF?
我需要将我的html文件导出为Pdf格式,并将其保存到Pdf文件夹中。 到目前为止,我已经做到了这一点 这是我的html页面,另存为messagesend.php,需要导出 } 它现在显示以下错误。下图中出现错误Php 如何使用Yii2中的kartik mpdf将HTML页面导出为PDF?,php,pdf,yii2,Php,Pdf,Yii2,我需要将我的html文件导出为Pdf格式,并将其保存到Pdf文件夹中。 到目前为止,我已经做到了这一点 这是我的html页面,另存为messagesend.php,需要导出 } 它现在显示以下错误。下图中出现错误 我现在如何生成Pdf?另外,我对这一点还不熟悉,所以你们能再给我解释一下吗?您必须请求与您的操作相对应的url: 如果希望生成PDF并将其存储在服务器中,则必须指定路径/文件名,将destination更改为PDF::DEST_FILE。检查此处的.以使用mPDF创建我的演示文稿的PD
我现在如何生成Pdf?另外,我对这一点还不熟悉,所以你们能再给我解释一下吗?您必须请求与您的操作相对应的url:
如果希望生成PDF并将其存储在服务器中,则必须指定路径/文件名,将
destination
更改为PDF::DEST_FILE
。检查此处的.以使用mPDF创建我的演示文稿的PDF文件
/*
* GENERATE PDF OF SAMPLE
*/
public function actionSamplePdf($id) {
$data = $this->findModel($id);
// get your HTML raw content without any layouts or scripts
$content = $this->renderPartial('_samplePdf', ['model' => $data]);
$destination = Pdf::DEST_BROWSER;
//$destination = Pdf::DEST_DOWNLOAD;
$filename = $data->file_name . ".pdf";
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_UTF8,
// A4 paper format
'format' => Pdf::FORMAT_A4,
// portrait orientation
'orientation' => Pdf::ORIENT_PORTRAIT,
// stream to browser inline
'destination' => $destination,
'filename' => $filename,
// your html content input
'content' => $content,
// format content from your own css file if needed or use the
// enhanced bootstrap css built by Krajee for mPDF formatting
'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
// any css to be embedded if required
'cssInline' => 'p, td,div { font-family: freeserif; }; body, p { font-family: irannastaliq; font-size: 15pt; }; .kv-heading-1{font-size:18px}table{width: 100%;line-height: inherit;text-align: left; border-collapse: collapse;}table, td, th {border: 1px solid black;}',
'marginFooter' => 5,
// call mPDF methods on the fly
'methods' => [
'SetTitle' => ['SAMPLE PDF'],
//'SetHeader' => ['SAMPLE'],
'SetFooter' => ['Page {PAGENO}'],
]
]);
// return the pdf output as per the destination setting
return $pdf->render();
}
我已经检查了文档,但它不工作,所以我请求帮助,但是如果您访问您的操作的url,您的pdf不会生成?那么我能做什么?我也给出了上面的错误图片。你使用的是亚洲/印度字符吗?尝试设置
'mode'=>Pdf::mode_ASIAN
谢谢@gmc它解决了该错误,但现在显示fopen():Filename不能为空错误。尝试将其配置为,将其用作小部件时似乎有问题。
use kartik\mpdf\Pdf;
public function actionReport() {
// get your HTML raw content without any layouts or scripts
$content = $this->renderPartial('messagesend');
// setup kartik\mpdf\Pdf component
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_CORE,
// 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' => $content,
// format content from your own css file if needed or use the
// enhanced bootstrap css built by Krajee for mPDF formatting
'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
// any css to be embedded if required
'cssInline' => '.kv-heading-1{font-size:18px}',
// set mPDF properties on the fly
'options' => ['title' => 'Krajee Report Title'],
// call mPDF methods on the fly
'methods' => [
'SetHeader'=>['Krajee Report Header'],
'SetFooter'=>['{PAGENO}'],
]
]);
// return the pdf output as per the destination setting
return $pdf->render();
/*
* GENERATE PDF OF SAMPLE
*/
public function actionSamplePdf($id) {
$data = $this->findModel($id);
// get your HTML raw content without any layouts or scripts
$content = $this->renderPartial('_samplePdf', ['model' => $data]);
$destination = Pdf::DEST_BROWSER;
//$destination = Pdf::DEST_DOWNLOAD;
$filename = $data->file_name . ".pdf";
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_UTF8,
// A4 paper format
'format' => Pdf::FORMAT_A4,
// portrait orientation
'orientation' => Pdf::ORIENT_PORTRAIT,
// stream to browser inline
'destination' => $destination,
'filename' => $filename,
// your html content input
'content' => $content,
// format content from your own css file if needed or use the
// enhanced bootstrap css built by Krajee for mPDF formatting
'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
// any css to be embedded if required
'cssInline' => 'p, td,div { font-family: freeserif; }; body, p { font-family: irannastaliq; font-size: 15pt; }; .kv-heading-1{font-size:18px}table{width: 100%;line-height: inherit;text-align: left; border-collapse: collapse;}table, td, th {border: 1px solid black;}',
'marginFooter' => 5,
// call mPDF methods on the fly
'methods' => [
'SetTitle' => ['SAMPLE PDF'],
//'SetHeader' => ['SAMPLE'],
'SetFooter' => ['Page {PAGENO}'],
]
]);
// return the pdf output as per the destination setting
return $pdf->render();
}