我需要将我的html文件导出为Pdf格式并将其保存到pdf文件夹中。 到目前为止,我已经做到了这一点。
这是我保存为messagesend.php的html页面,需要导出。
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();
现在下面是我用来生成pdf的控制器。
List<Item> items = new List<Item>();
if(!string.IsNullOrEmpty(_txtboxId_)
{
items = dbContext.Items.Where(m => m.Id.Contains(_txtboxId_).ToList();
}
else
items = dbContext.Items.ToList();
if(!string.IsNullOrEmpty(_txtboxBuyer_)
{
items = dbContext.Items.Where(m => m.Id.Contains(_txtboxBuyer_).ToList();
}
else
items = dbContext.Items.ToList();
if(!string.IsNullOrEmpty(_txtboxPrice_)
{
items = dbContext.Items.Where(m => m.Id.Contains(_txtboxPrice_).ToList();
}
else
items = dbContext.Items.ToList();
}
我现在如何生成Pdf?我也是新手,所以你们可以向我解释一下吗?
答案 0 :(得分:1)
您必须请求与您的操作相对应的网址:http://example.com/yourcontroller/report
如果要生成PDF并将其存储在服务器中,则必须将destination
更改为Pdf::DEST_FILE
,指定路径/文件名。查看docs。
答案 1 :(得分:0)
在这里,要使用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();
}