php - 如何使用 PHP codeigniter 以编程方式生成带有我公司徽标的 PDF
问题描述
我正在尝试使用 dompdf 库创建带有公司徽标的 PDF,但我有一个没有徽标的 PDF。我可以试试下面的代码。
欢迎控制器:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('pdf');
}
public function index()
{
$html = $this->load->view('welcome_message', [], true);
$this->pdf->createPDF($html, 'mypdf', false);
}
}
欢迎观看:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<body>
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<img src="<?php echo base_url() ?>/img/un6.png'">
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/Welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>
</body>
</html>
还包括 dompdf 文件 application/libraries 文件夹:
dompdf 文件的 PDF.php 文件
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once(dirname(__FILE__) . '/dompdf/autoload.inc.php');
class Pdf
{
function createPDF($html, $filename='', $download=TRUE, $paper='A4', $orientation='portrait'){
$dompdf = new Dompdf\DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper($paper, $orientation);
$dompdf->render();
if($download)
$dompdf->stream($filename.'.pdf', array('Attachment' => 1));
else
$dompdf->stream($filename.'.pdf', array('Attachment' => 0));
}
}
?>
我怎样才能做到这一点
解决方案
问题在这里:
<img src="<?php echo base_url() ?>/img/un6.png'">
DomPDF 需要将所有外部资源引用为文件路径,而不是 URL。例如,你可以试试这个:
<img src="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>/img/un6.png'">
或者类似的东西:
<img src="<?php echo FCPATH; ?>/img/un6.png'">
(FCPATH 是 Codeigniter 的前端控制器路径……主index.php
文件的服务器路径,在 CI 中编排所有内容)
推荐阅读
- url - IIS URL 重写模块 - 重定向规则不起作用
- python - 使用 Beautifulsoup 从某个属性中获取文本
- .net-core - Fortify “sourceanalyzer -show-build-ids”现在在成功构建 .NET Core 3.1 x64 后会显示任何构建 ID
- javascript - PHP 回显序列中的 JavaScript 不正确
- loops - 无限循环:保持打印单行
- angular - 在 Ctrl+P 之后,所有 fxHide 指令都会触发,而不管视口大小
- android - Android 11: how to get current location
- reactjs - Ionic React.js 我需要传递一个图像数组,这样我就可以在模态中添加下一个和上一个
- python - 是否可以在 sklearn 中更新经过训练的模型?
- ember.js - ember-composable-helpers 复选框未选中