sementara
This commit is contained in:
83
Helpers/PdfHelper.php
Normal file
83
Helpers/PdfHelper.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user