mpdf 当我将视图渲染为 HTML 时跳过 CSS?

问题描述 投票:0回答:2

我正在使用 codeigniter,我正在处理发票,我想打印我的发票页面的 PDF 文件,这是我的控制器。

$pdfFilePath ='invoice.pdf';
$data=$this->singleinvoice($invoiceId);
ini_set('memory_limit','32M');
$html = $this->load->view('invoice/invoicetopdf', $data, true);
$this->load->library('pdf');
$pdf = $this->pdf->load();
$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822)); 
$pdf->WriteHTML($html);
$pdf->Output($pdfFilePath, 'D');
redirect("invoice");

当我 echo $html veriable 显示正常,但当它输出 pdf 时,它没有 css 样式,我的意思是它转换 HTML 时没有任何我的 css 样式。 我也尝试了以下方法,但没有成功。

 $style=file_get_contents(base_url().'_assets/css/bootstrap.css');
 $pdf->WriteHTML($style,1);
 $pdf->Output($pdfFilePath, 'D');

现在我想将我的 HTML 页面转换为 PDF,因为它以完整的样式和标记向公众显示。 有什么建议或解决方案吗?

php codeigniter mpdf
2个回答
1
投票

您必须将页面结构从 DIV 转换为传统表结构。 将您的元素内容等排列到一个或多个表格中。 然后尝试一些基本的 CSS。 因为 mpdf 或任何其他 pdf 转换器不支持大多数 CSS。

点击这里 了解 mpdf 支持哪些内容(CSS)以及不支持哪些内容。


0
投票

我注意到顺序对于 pdf 类非常重要。

例如当您首先添加 html,然后添加 css 时,此 css 仅适用于在此 css 块之后添加的所有内容。

这意味着您需要更改代码,即您的 css 添加在 html 之前。

例如

$html = $this->load->view('invoice/invoicetopdf', $data, true);
$this->load->library('pdf');
$pdf = $this->pdf->load();
$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822)); 
// add css
$style=file_get_contents(base_url().'_assets/css/bootstrap.css');
$pdf->WriteHTML($style,1);
// add html-content
$pdf->WriteHTML($html);
$pdf->Output($pdfFilePath, 'D');
© www.soinside.com 2019 - 2024. All rights reserved.