Fixing some issues found in tests before the first demo

This commit is contained in:
Julio Cesar Laura Avendaño
2020-09-02 22:16:47 +00:00
parent 50d8013786
commit 001b04fa00
2 changed files with 27 additions and 12 deletions

View File

@@ -1455,7 +1455,6 @@ function documents_add_font($args, $options)
$fontFriendlyName = $args[1] ?? ''; $fontFriendlyName = $args[1] ?? '';
$fontProperties = $args[2] ?? ''; $fontProperties = $args[2] ?? '';
$fontType = $options['font_type'] ?? 'TrueType'; $fontType = $options['font_type'] ?? 'TrueType';
$name = '';
// Check fonts path // Check fonts path
OutputDocument::checkTcPdfFontsPath(); OutputDocument::checkTcPdfFontsPath();
@@ -1481,22 +1480,13 @@ function documents_add_font($args, $options)
} }
// Convert TTF file to the format required by TCPDF library // Convert TTF file to the format required by TCPDF library
$tcPdfFont = TCPDF_FONTS::addTTFfont(PATH_DATA . 'fonts' . PATH_SEP . $fontFileName, $fontType); $fontFamilyName = TCPDF_FONTS::addTTFfont(PATH_DATA . 'fonts' . PATH_SEP . $fontFileName, $fontType);
// Check if the conversion was successful // Check if the conversion was successful
if ($tcPdfFont === false) { if ($fontFamilyName === false) {
throw new Exception("The font file '{$fontFileName}' cannot be converted."); throw new Exception("The font file '{$fontFileName}' cannot be converted.");
} }
// Include font definition, in order to use the variable $name
require_once K_PATH_FONTS . $tcPdfFont . '.php';
// Build the font family name to be used in the styles
$fontFamilyName = strtolower($name);
$fontFamilyName = str_replace('-', ' ', $fontFamilyName);
$fontFamilyName = str_replace(['bold', 'oblique', 'italic', 'regular'], '', $fontFamilyName);
$fontFamilyName = trim($fontFamilyName);
// Add new font // Add new font
$font = [ $font = [
'fileName' => $fontFileName, 'fileName' => $fontFileName,

View File

@@ -928,6 +928,31 @@ class OutputDocument extends BaseOutputDocument
// Enable the font sub-setting option // Enable the font sub-setting option
$pdf->setFontSubsetting(true); $pdf->setFontSubsetting(true);
// Set default unicode font if is required, we need to detect if is chinese, japanese, thai, etc.
if (preg_match('/[\x{30FF}\x{3040}-\x{309F}\x{4E00}-\x{9FFF}\x{0E00}-\x{0E7F}]/u', $content, $matches)) {
// The additional fonts should be in "shared/fonts" folder
$fileArialUniTTF = PATH_DATA . 'fonts' . PATH_SEP . 'arialuni.ttf';
if (file_exists($fileArialUniTTF)) {
// Convert TTF file to the format required by TCPDF library
$fontFamilyName = TCPDF_FONTS::addTTFfont($fileArialUniTTF, 'TrueTypeUnicode');
// Set the default unicode font for the document
$pdf->SetFont($fontFamilyName);
// Register the font file if is not present in the JSON file
if (!self::existTcpdfFont('arialuni.ttf')) {
// Add "arialuni.ttf" font
$font = [
'fileName' => 'arialuni.ttf',
'familyName' => $fontFamilyName,
'friendlyName' => $fontFamilyName,
'properties' => ''
];
self::addTcPdfFont($font);
}
}
}
// Convert the encoding of the content if is UTF-8 // Convert the encoding of the content if is UTF-8
if (mb_detect_encoding($content) == 'UTF-8') { if (mb_detect_encoding($content) == 'UTF-8') {
$content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'); $content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8');