【发布时间】:2021-01-15 07:51:09
【问题描述】:
我正在使用 codeigniter 3 和 dompdf 将 html 视图转换为 pdf。我可以成功地将 html 转换为 pdf,但没有应用适当的样式。 所有必需的 css 文件都已作为自定义设计包含在视图文件中。
我还需要做什么才能正确包含 css 样式和图像?
这里是控制器:
public function get_pdf_test($id)
{
// print_r($_REQUEST);
// die;
$data['incidents'] = $this->incidents_model->getById($id);
$data['incidents_history'] = $this->incidents_model->getById_history($id);
$data['company_name'] = $this->incidents_model->getAllCompanyName();
generate_pdf("admin/incidents/incidentsView.pdf", "admin/incidents/incidentsView.php", $data);
}
incident_model.php
function getById($id)
{
return $this->db->get_where('incidents',array('id'=>$id))->row();
}
function getById_history($id)
{
$query=$this->db->query("SELECT incidents_id FROM incidents WHERE id = $id");
$get_row = $query->row();
$incident_id = $get_row->incidents_id;
$this->db->select('*');
$this->db->where(array('incidents.incidents_id'=>$incident_id));
$this->db->join('incident_status', 'incidents.id = incident_status.incident_id');
return $this->db->get('incidents')->result();
}
function getAllCompanyName()
{
$query = $this->db->get('company_details');
$query = $this->db->query('SELECT company_name FROM company_details where delete_flag =0');
return $query->result();
}
库中的 Pdf.php 文件:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
*
* Convert HTML to PDF in CodeIgniter applications.
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author Hostmystory
* @link https://www.hostmystory.com
*/
// Dompdf namespace
use Dompdf\Dompdf;
class Pdf
{
public function __construct(){
// require_once autoloader
require_once dirname(__FILE__).'/dompdf/autoload.inc.php';
$pdf = new DOMPDF();
$CI =& get_instance();
$CI->dompdf = $pdf;
}
}
?>
helper 中的new_helper.php:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
// generate pdf
function generate_pdf($name, $tpl, $data)
{
$ci = &get_instance();
$data['data'] = $data;
$ci->load->view($tpl, $data);
// Get output html
$html = $ci->output->get_output();
// add external css library
$html .= '<link href="' . base_url() . 'assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">';
// Load pdf library
$ci->load->library('pdf');
$ci->dompdf->loadHtml($html);
// setup size
$ci->dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$ci->dompdf->render();
// Output PDF (1 = download and 0 = preview)
$ci->dompdf->stream($name, array("Attachment" => 0));
}
?>
这是 dompdf 的输出:
【问题讨论】:
-
我已经用一个新的解决方案更新了我的答案,如果它对你有用,请告诉我。
标签: css image codeigniter codeigniter-3 dompdf