【发布时间】:2021-06-25 00:40:24
【问题描述】:
我已阅读有关此主题的几篇文章,但不想在其中任何一篇文章中提出其他问题。
具体这篇帖子:TCPDF and insert an image base64 encoded
我正在从 Wordpress 的自定义主题中生成 PDF。我正在使用 TCPDF 6.2.3(我相信是最新的稳定版本)。
我正在使用用于在页面上显示的相同 HTML 构建此 PDF。如果我嵌入完整的 base64 编码字符串,它可以在浏览器中正常工作,但 PDF 中缺少图像。
如果我使用链接帖子中描述的“@”方法,我会在浏览器中看到一个损坏的图像(预期),但在 PDF 中仍然没有。
我所有的 HTML 标记都在 PDF 中呈现,只是没有显示图像。
我是否需要设置其他设置或选项才能使图像出现在 PDF 中,和/或您能发现我在这里做错了什么吗?没有错误,图像在 PDF 中不可见。
这是我设置图像的方式:
$imageLocation = $img_root.$imgsrc;
$ext = end(explode(".", $imageLocation));
$image = base64_encode(file_get_contents($imageLocation));
//$response .= "<img src='data:image/$ext;base64,$image'>"; //works in browser but not in PDF
$response .= "<img src='@$image' class='socf_image'>"; //does not work in browser or PDF
这里是创建 PDF 的方法:
function createPDF($response)
{
// Include the main TCPDF library (search for installation path).
require_once('tcpdf_6_3_2/tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('test');
$pdf->SetTitle('test');
$pdf->SetSubject('test');
$pdf->SetKeywords('test');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set default font subsetting mode
$pdf->setFontSubsetting(true);
// Set font
$pdf->SetFont('helvetica', '', 14, '', true);
// Add a page
$pdf->AddPage();
$html = $response;
$pdf->writeHTML($response, true, false, true, false, '');
return $pdf;
}
【问题讨论】:
标签: tcpdf