84 lines
2.2 KiB
PHP
84 lines
2.2 KiB
PHP
<?php
|
||
|
||
namespace Modules\Lpj\Helpers;
|
||
|
||
class PdfHelper
|
||
{
|
||
/**
|
||
* Format text for PDF output to handle special characters
|
||
*
|
||
* @param string $text
|
||
* @return string
|
||
*/
|
||
public static function formatText($text)
|
||
{
|
||
if (empty($text)) {
|
||
return '';
|
||
}
|
||
|
||
// Common problematic characters and their safe replacements
|
||
$replacements = [
|
||
'<' => '<',
|
||
'>' => '>',
|
||
'&' => '&',
|
||
'"' => '"',
|
||
"'" => ''',
|
||
'≤' => '≤',
|
||
'≥' => '≥',
|
||
'≠' => '!=',
|
||
'≈' => '~',
|
||
'×' => 'x',
|
||
'÷' => '/',
|
||
'–' => '-',
|
||
'—' => '-',
|
||
'' => '"',
|
||
'' => '"',
|
||
'' => "'",
|
||
'' => "'",
|
||
];
|
||
|
||
// First pass: replace with HTML entities
|
||
$safeText = str_replace(array_keys($replacements), array_values($replacements), $text);
|
||
|
||
// Ensure UTF-8 encoding
|
||
if (!mb_check_encoding($safeText, 'UTF-8')) {
|
||
$safeText = mb_convert_encoding($safeText, 'UTF-8', 'auto');
|
||
}
|
||
|
||
// Remove any remaining non-ASCII characters that could cause issues
|
||
$safeText = preg_replace('/[^\x20-\x7E\xA0-\xFF]/', '', $safeText);
|
||
|
||
return $safeText;
|
||
}
|
||
|
||
/**
|
||
* Format mathematical symbols to text representation
|
||
*
|
||
* @param string $text
|
||
* @return string
|
||
*/
|
||
public static function formatMathSymbols($text)
|
||
{
|
||
if (empty($text)) {
|
||
return '';
|
||
}
|
||
|
||
$mathReplacements = [
|
||
'<' => 'kurang dari',
|
||
'>' => 'lebih dari',
|
||
'<=' => 'kurang dari sama dengan',
|
||
'>=' => 'lebih dari sama dengan',
|
||
'!=' => 'tidak sama dengan',
|
||
'==' => 'sama dengan',
|
||
'≤' => 'kurang dari sama dengan',
|
||
'≥' => 'lebih dari sama dengan',
|
||
'≠' => 'tidak sama dengan',
|
||
'≈' => 'kira-kira',
|
||
'≡' => 'identik dengan',
|
||
'≅' => 'hampir sama dengan',
|
||
];
|
||
|
||
return str_replace(array_keys($mathReplacements), array_values($mathReplacements), $text);
|
||
}
|
||
}
|