我正在尝试创建包含表情符号字符的文本的 pdf。
使用 PHP > 7 和 mysql
'char_set' => utf8mb4
'dbcollat' => utf8mb4_unicode_ci
我已经从 html 创建了 PDF,回显 html 文本所有表情符号都显示良好。但如果它创建 pdf,则不会转换。
请帮忙解决这个问题
示例:“一\ud83d\ude02”
谢谢
这里有更多解释来简化我的问题。
$fileName = $source_path.'example.pdf';
//using php codeignitor 3.1.2
$htmlContent = $this->load->view('view/view_pdf', $data, TRUE);
$htmlContent = <<<EOD
$htmlContent
EOD;
// call pdf create function
$this->createPDF($fileName, $htmlContent);
// these character working correct as echo output on browser and send in email subject n body.
echo decodeEmoticons("One \ud83d\ude02");
echo "One \u{1F602}";
/*******************************/
// helper functions
function checkJson($title){
if (json_decode($title, true) !== null) {
$title = str_replace('"', '', json_decode($title));
}else{
$title = str_replace('"', '', $title);
}
return $title;
}
function decodeEmoticons($src) {
$replaced = preg_replace("/\\\\u([0-9A-F]{1,4})/i", "&#x$1;", $src);
$result = mb_convert_encoding($replaced, "UTF-16", "HTML-ENTITIES");
$result = mb_convert_encoding($result, 'utf-8', 'utf-16');
$result = checkJson($result);
return $result;
}
/*******************************/
$htmlContent输出html代码如下
<div class="row">
<div class="col-lg-12">
<table class="table dataTable" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td width="60%"><b>Event Name:</b> <?php echo decodeEmoticons("One \ud83d\ude02"); ?> </td>
<td width="60%"><b>Event Name:</b> <?php echo "One \u{1F602}"; ?> </td>
<td><b>Start Time:</b> <?php echo date('F j, Y, g:i A', time()); ?></td>
</tr>
<tr>
<td colspan="3"></td>
</tr>
</tbody>
</table>
</div>
</div>
这是我的 php 函数调用 TCpdf 库
public function createPDF($fileName, $htmlContent) {
/*
http://www.tcpdf.org
// File name : tcpdf.php
// Version : 6.2.13
// Begin : 2002-08-03
// Last Update : 2015-06-18
*/
ob_start();
// Include the main TCPDF library (search for installation path).
$this->load->library(array('Tcpdf'));
// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
//$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('auth');
$pdf->SetTitle('Event ');
$pdf->SetSubject('Event ');
$pdf->SetKeywords('Event ');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
// 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));
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, 10, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(10);
$pdf->SetFooterMargin(30);
// set auto page breaks
//$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->SetAutoPageBreak(TRUE, 30);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->SetFont('dejavusans', '', 10);
//$pdf->SetFont('halvatica', '', 10); // this is also trying but fail
// add a page
$pdf->AddPage();
//$htmlContent = utf8_encode($htmlContent);// this is also trying but fail
// output the HTML content
$pdf->writeHTML($htmlContent, true, false, true, false, '');
// reset pointer to the last pagend output PDF document
$pdf->lastPage();
ob_end_clean();
//Close and output PDF document
$pdf->Output($fileName, 'F');
}
pdf 输出未正确显示表情符号字符。
您可以将表情符号替换为 png 或 svg,然后您可以在 pdf 中使用,因为 png 和 svg 将支持并且您会得到结果。
尝试以下代码
导入 twitter 脚本以获取图像
<script type="module">
import twemoji from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm'
</script>
此函数会将您的表情符号转换为十六进制代码
function emojiToHexForTwemoji($emoji) {
$hex = '';
$emojiLength = mb_strlen($emoji, 'UTF-8');
for ($i = 0; $i < $emojiLength; $i++) {
$char = mb_substr($emoji, $i, 1, 'UTF-8');
$hex .= '-' . strtolower(dechex(mb_ord($char)));
}
return trim($hex, '-');
}
此代码会将您的表情符号从字符串替换为 svg 或 png 图像
$input_string = "Hello 😀! Let's grab some 🍕 and 🥤.";
$output = ''; // Output will store the HTML
// Iterate over each character and convert emojis
$length = mb_strlen($input_string, 'UTF-8');
for ($i = 0; $i < $length; $i++) {
$char = mb_substr($input_string, $i, 1, 'UTF-8');
// If the character is an emoji, convert to Twemoji URL
if (preg_match('/[\x{1F600}-\x{1F64F}|\x{1F300}-\x{1F5FF}|\x{1F680}-\x{1F6FF}|\x{1F1E0}-\x{1F1FF}|\x{2600}-\x{26FF}|\x{2700}-\x{27BF}|\x{1F900}-\x{1F9FF}]/u', $char)) {
$hex = emojiToHexForTwemoji($char);
// $twemojiUrl = "https://twemoji.maxcdn.com/v/latest/72x72/$hex.png"; // Twemoji CDN URL
$twemojiUrl = "https://cdn.jsdelivr.net/gh/twitter/[email protected]/assets/72x72/$hex.png"; // Twemoji CDN URL
$output .= "<img src='$twemojiUrl' alt='$char' class='twemoji' style='width: 1em; height: 1em;' />";
} else {
$output .= htmlspecialchars($char); // Non-emoji characters stay the same
}
}
完整代码如下
<script type="module">
import twemoji from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm'
</script>
$output = ''; // Output will store the HTML
$text = "YOUR TEXT WHICH INCLUDE THE EMOJI";
for ($i = 0; $i < $length; $i++) { // Iterate over each character and convert emojis
$char = mb_substr($text, $i, 1, 'UTF-8');
if (isEmoji($char)) {
$hex = emojiToHexForTwemoji($char);
/* $twemojiUrl = "https://cdn.jsdelivr.net/gh/twitter/[email protected]/assets/72x72/$hex.png"; // PNG Twemoji CDN URL */
$twemojiUrl = "https://cdn.jsdelivr.net/gh/twitter/[email protected]/assets/svg/$hex.svg"; // SVG Twemoji CDN URL
$output .= "<img src='$twemojiUrl' alt='$char' class='twemoji' style='width: 1em; height: 1em;' />";
} else {
$output .= htmlspecialchars($char); // Non-emoji characters stay the same
}
}
$text = $output;
function emojiToHexForTwemoji($emoji) {
$hex = '';
$emojiLength = mb_strlen($emoji, 'UTF-8');
for ($i = 0; $i < $emojiLength; $i++) {
$char = mb_substr($emoji, $i, 1, 'UTF-8');
$hex .= '-' . strtolower(dechex(mb_ord($char)));
}
return trim($hex, '-');
}