Files
lpj/Helpers/PdfHelper.php
Daeng Deni Mardaeni aceff4f006 sementara
2026-01-30 14:44:14 +07:00

84 lines
2.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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 = [
'<' => '&lt;',
'>' => '&gt;',
'&' => '&amp;',
'"' => '&quot;',
"'" => '&#39;',
'≤' => '&le;',
'≥' => '&ge;',
'≠' => '!=',
'≈' => '~',
'×' => '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);
}
}