php - 将图像放入 pdf TCPDF
问题描述
我正在尝试生成一个也显示图像的pdf。所以我的图像存储在数据库中,我有一个工作代码,可以将数据库中的数据生成为 PDF 文件。
这是从我的数据库查看我的图像到数据表的代码
<td ><img onclick="onClick(this)" class="w3-hover-opacity" src="<?php echo base_url('uploads/'.$report["attachment"].'.png'); ?>" style="width:100%;max-width:200px;border-radius: 5px" ></td>
这就是我所做的,但它只打印 img 文件名
if(isset($_POST["generate_pdf"]))
{
require_once('tcpdf/tcpdf.php');
$obj_pdf = new TCPDF('L', 'mm', 'LETTER', true, 'UTF-8', false);
$obj_pdf->SetCreator(PDF_CREATOR);
$obj_pdf->SetAuthor('Nicola Asuni');
$obj_pdf->SetSubject('TCPDF Tutorial');
$obj_pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$obj_pdf->SetTitle("Incident Report");
$obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);
$obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$obj_pdf->SetDefaultMonospacedFont('helvetica');
$obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$obj_pdf->SetMargins(2, '7', PDF_MARGIN_RIGHT, '10');
$obj_pdf->setPrintHeader(false);
$obj_pdf->setPrintFooter(false);
$obj_pdf->setPrintFooter(false);
$obj_pdf->SetFont('helvetica', '', 5);
$obj_pdf->AddPage();
date_default_timezone_set("Asia/Manila");
$content = '';
$content2 = date("F d, Y h:i:s A",mktime());
$content .= '
<h4 align="center">Incident Report</h4> <br />
<table align="center" border="1" cellspacing="0" cellpadding="3">
<tr>
<th width="8%">School</th>
<th width="5%">Grade/Level</th>
<th width="6%">Section</th>
<th width="7%">Date and Time</th>
<th width="6%">Incident Type</th>
<th width="7%">Incident Description</th>
<th width="6%">Sender</th>
<th width="10%">Person Involved</th>
<th width="14%">Students that tagged safe</th>
<th width="14%">Students that tagged unsafe</th>
<th width="4%">Male Count</th>
<th width="5%">Female Count</th>
<th width="5%">Number of involved people</th>
if(isset($_POST["generate_pdf"]))
{
require_once('tcpdf/tcpdf.php');
$obj_pdf = new TCPDF('L', 'mm', 'LETTER', true, 'UTF-8', false);
$obj_pdf->SetCreator(PDF_CREATOR);
$obj_pdf->SetAuthor('Nicola Asuni');
$obj_pdf->SetSubject('TCPDF Tutorial');
$obj_pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$obj_pdf->SetTitle("Incident Report");
$obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);
$obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$obj_pdf->SetDefaultMonospacedFont('helvetica');
$obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$obj_pdf->SetMargins(2, '7', PDF_MARGIN_RIGHT, '10');
$obj_pdf->setPrintHeader(false);
$obj_pdf->setPrintFooter(false);
$obj_pdf->setPrintFooter(false);
$obj_pdf->SetFont('helvetica', '', 5);
$obj_pdf->AddPage();
date_default_timezone_set("Asia/Manila");
$content = '';
$content2 = date("F d, Y h:i:s A",mktime());
$content .= '
<h4 align="center">Incident Report</h4> <br />
<table align="center" border="1" cellspacing="0" cellpadding="3">
<tr>
<th width="8%">School</th>
<th width="5%">Grade/Level</th>
<th width="6%">Section</th>
<th width="7%">Date and Time</th>
<th width="6%">Incident Type</th>
<th width="7%">Incident Description</th>
<th width="6%">Sender</th>
<th width="10%">Person Involved</th>
<th width="14%">Students that tagged safe</th>
<th width="14%">Students that tagged unsafe</th>
<th width="4%">Male Count</th>
<th width="5%">Female Count</th>
<th width="5%">Number of involved people</th>
<th width="5%">Attachment</th>
</tr>';
$content .= fetch_data();
$content .= '</table>';
$obj_pdf->writeHTML($content);
$obj_pdf->writeHTML($content2);
$obj_pdf->Output('report.pdf', 'I');
}
?>
</tr>';
$content .= fetch_data();
$content .= '</table>';
$obj_pdf->writeHTML($content);
$obj_pdf->writeHTML($content2);
$obj_pdf->Output('report.pdf', 'I');
}
?>
我需要添加另一个 $obj_pdf 吗?我知道该行<th width="5%">Attachment</th>
只打印文件名。我只是试水
解决方案
您可以通过将图像的 base64 编码字符串提供给标签来设置标签src
中的属性来做到这一点。<img>
像这样:
只需将此脚本放在您希望显示图像的 pdf html 中。
HTML 格式:
<img class="w3-hover-opacity" style="width:100%;max-width:200px;border-radius: 5px" src="data:image/png;base64,'<?php echo base64_encode(file_get_contents('uploads/'.$report["attachment"] . '.png')); ?>">
或 PHP 连接字符串:
echo
'<img class="w3-hover-opacity" style="width:100%;max-width:200px;border-radius: 5px" src="data:image/png;base64,' . base64_encode(file_get_contents('uploads/'.$report["attachment"] . '.png')) . '">;
对于file_get_contents()
尝试:
file_get_contents(base_url('uploads/'.$report["attachment"] . '.png'))
然后将其包装为base64_encode()
:
base64_encode(file_get_contents(base_url('uploads/'.$report["attachment"] . '.png')))
那应该对你有用。
推荐阅读
- php - 试图在视图刀片中获取非对象的属性“id”
- php - 在 Laravel 中搜索导出为 PDF 后?
- sql - Missing Comma in SQL Command Oracle VM
- julia - DataFrame.jl 缺少插补(Julia 编程语言)
- c++ - 测试无意中从 gtest 中的不同测试套件合并
- java - Function.apply-Method 在自定义界面中没有被覆盖时会做什么?
- python - 使用 class 和 raise() 时的 Tkinter 帧大小
- python - ValueError:标签不在输入中。对于没有标签的预测示例,请改用基本模型
- node.js - Mongoose 模式类型二进制数据 - 类型转换
- proxy - Envoy 和 GRPC 不工作(上游没有健康)