PMCORE-2138

This commit is contained in:
Julio Cesar Laura Avendaño
2020-09-08 20:20:41 +00:00
parent 001b04fa00
commit 1b46834ee8
2 changed files with 74 additions and 21 deletions

View File

@@ -387,28 +387,42 @@ EOT
CLI::taskRun("run_artisan"); CLI::taskRun("run_artisan");
/** /**
* Add new font to be used in Documents generation (TinyMCE editor and TCPDF library for now) * Add a font to be used in Documents generation (TinyMCE editor and/or TCPDF library)
*/ */
CLI::taskName('documents-add-font'); CLI::taskName('documents-add-font');
CLI::taskDescription(<<<EOT CLI::taskDescription(<<<EOT
Add new font to be used in Documents generation (TinyMCE editor and TCPDF library for now). Add a font to be used in Documents generation (TinyMCE editor and/or TCPDF library).
EOT EOT
); );
CLI::taskOpt('font_type', <<<EOT CLI::taskOpt('type', <<<EOT
Can be "TrueType" or "TrueTypeUnicode", if the option is not specified the default value is "TrueType" Can be "TrueType" or "TrueTypeUnicode", if the option is not specified the default value is "TrueType"
EOT EOT
,'ft', 'font_type='); ,'t', 'type=');
CLI::taskOpt('tinymce', <<<EOT
Can be "true" or "false", if the option is not specified the default value is "true". If the value is "false" the optional arguments [FRIENDLYNAME] [FONTPROPERTIES] are omitted.
EOT
,'tm', 'tinymce=');
CLI::taskArg('fontFileName', false); CLI::taskArg('fontFileName', false);
CLI::taskArg('friendlyName', true); CLI::taskArg('friendlyName', true);
CLI::taskArg('fontProperties', true); CLI::taskArg('fontProperties', true);
CLI::taskRun('documents_add_font'); CLI::taskRun('documents_add_font');
/** /**
* Remove a font used in Documents generation (TinyMCE editor and TCPDF library for now) * List the registered fonts
*/
CLI::taskName('documents-list-registered-fonts');
CLI::taskDescription(<<<EOT
List the registered fonts.
EOT
);
CLI::taskRun('documents_list_registered_fonts');
/**
* Remove a font used in Documents generation (TinyMCE editor and/or TCPDF library)
*/ */
CLI::taskName('documents-remove-font'); CLI::taskName('documents-remove-font');
CLI::taskDescription(<<<EOT CLI::taskDescription(<<<EOT
Remove a font used in Documents generation (TinyMCE editor and TCPDF library for now). Remove a font used in Documents generation (TinyMCE editor and/or TCPDF library).
EOT EOT
); );
CLI::taskArg('fontFileName', false); CLI::taskArg('fontFileName', false);
@@ -1437,7 +1451,7 @@ function run_artisan($args)
} }
/** /**
* Add new font to be used in Documents generation (TinyMCE editor and TCPDF library for now) * Add a font to be used in Documents generation (TinyMCE editor and/or TCPDF library)
* *
* @param array $args * @param array $args
* @param array $options * @param array $options
@@ -1454,7 +1468,9 @@ function documents_add_font($args, $options)
$fontFileName = $args[0]; $fontFileName = $args[0];
$fontFriendlyName = $args[1] ?? ''; $fontFriendlyName = $args[1] ?? '';
$fontProperties = $args[2] ?? ''; $fontProperties = $args[2] ?? '';
$fontType = $options['font_type'] ?? 'TrueType'; $fontType = $options['type'] ?? 'TrueType';
$inTinyMce = !empty($options['tinymce']) ? $options['tinymce'] === 'true' : true;
$name = '';
// Check fonts path // Check fonts path
OutputDocument::checkTcPdfFontsPath(); OutputDocument::checkTcPdfFontsPath();
@@ -1480,17 +1496,28 @@ 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
$fontFamilyName = TCPDF_FONTS::addTTFfont(PATH_DATA . 'fonts' . PATH_SEP . $fontFileName, $fontType); $tcPdfFileName = TCPDF_FONTS::addTTFfont(PATH_DATA . 'fonts' . PATH_SEP . $fontFileName, $fontType);
// Check if the conversion was successful // Check if the conversion was successful
if ($fontFamilyName === false) { if ($tcPdfFileName === 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 . $tcPdfFileName . '.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,
'tcPdfFileName' => $tcPdfFileName,
'familyName' => $fontFamilyName, 'familyName' => $fontFamilyName,
'inTinyMce' => $inTinyMce,
'friendlyName' => !empty($fontFriendlyName) ? $fontFriendlyName : $fontFamilyName, 'friendlyName' => !empty($fontFriendlyName) ? $fontFriendlyName : $fontFamilyName,
'properties' => $fontProperties 'properties' => $fontProperties
]; ];
@@ -1505,7 +1532,28 @@ function documents_add_font($args, $options)
} }
/** /**
* Remove a font used in Documents generation (TinyMCE editor and TCPDF library for now) * List the registered fonts
*/
function documents_list_registered_fonts()
{
// Check fonts path
OutputDocument::checkTcPdfFontsPath();
// Get registered fonts
$fonts = OutputDocument::loadTcPdfFontsList();
// Display information
CLI::logging(PHP_EOL);
foreach ($fonts as $fileName => $font) {
$inTinyMce = $font['inTinyMce'] ? 'Yes' : 'No';
CLI::logging("TTF Filename: {$fileName}" . PHP_EOL);
CLI::logging("TCPDF Filename: {$font['tcPdfFileName']}" . PHP_EOL);
CLI::logging("Display in TinyMCE: {$inTinyMce}" . PHP_EOL . PHP_EOL . PHP_EOL);
}
}
/**
* Remove a font used in Documents generation (TinyMCE editor and/or TCPDF library)
* *
* @param array $args * @param array $args
*/ */
@@ -1533,14 +1581,14 @@ function documents_remove_font($args)
throw new Exception("Font '{$fontFileName}' was not registered."); throw new Exception("Font '{$fontFileName}' was not registered.");
} }
// This line get the filename of the previous converted font // Get registered font
$tcPdfFont = TCPDF_FONTS::addTTFfont(PATH_DATA . 'fonts' . PATH_SEP . $fontFileName); $font = OutputDocument::loadTcPdfFontsList()[$fontFileName];
// Remove TCPDF font files // Remove TCPDF font files
$extensions = ['ctg.z', 'php', 'z']; $extensions = ['ctg.z', 'php', 'z'];
foreach ($extensions as $extension) { foreach ($extensions as $extension) {
if (file_exists(PATH_DATA . 'fonts' . PATH_SEP . 'tcpdf' . PATH_SEP . $tcPdfFont . '.' . $extension)) { if (file_exists(PATH_DATA . 'fonts' . PATH_SEP . 'tcpdf' . PATH_SEP . $font['tcPdfFileName'] . '.' . $extension)) {
unlink(PATH_DATA . 'fonts' . PATH_SEP . 'tcpdf' . PATH_SEP . $tcPdfFont . '.' . $extension); unlink(PATH_DATA . 'fonts' . PATH_SEP . 'tcpdf' . PATH_SEP . $font['tcPdfFileName'] . '.' . $extension);
} }
} }

View File

@@ -934,18 +934,20 @@ class OutputDocument extends BaseOutputDocument
$fileArialUniTTF = PATH_DATA . 'fonts' . PATH_SEP . 'arialuni.ttf'; $fileArialUniTTF = PATH_DATA . 'fonts' . PATH_SEP . 'arialuni.ttf';
if (file_exists($fileArialUniTTF)) { if (file_exists($fileArialUniTTF)) {
// Convert TTF file to the format required by TCPDF library // Convert TTF file to the format required by TCPDF library
$fontFamilyName = TCPDF_FONTS::addTTFfont($fileArialUniTTF, 'TrueTypeUnicode'); $tcPdfFileName = TCPDF_FONTS::addTTFfont($fileArialUniTTF, 'TrueTypeUnicode');
// Set the default unicode font for the document // Set the default unicode font for the document
$pdf->SetFont($fontFamilyName); $pdf->SetFont($tcPdfFileName);
// Register the font file if is not present in the JSON file // Register the font file if is not present in the JSON file
if (!self::existTcpdfFont('arialuni.ttf')) { if (!self::existTcpdfFont('arialuni.ttf')) {
// Add "arialuni.ttf" font // Add font "arialuni.ttf"
$font = [ $font = [
'fileName' => 'arialuni.ttf', 'fileName' => 'arialuni.ttf',
'familyName' => $fontFamilyName, 'tcPdfFileName' => $tcPdfFileName,
'friendlyName' => $fontFamilyName, 'familyName' => $tcPdfFileName,
'inTinyMce' => true,
'friendlyName' => $tcPdfFileName,
'properties' => '' 'properties' => ''
]; ];
self::addTcPdfFont($font); self::addTcPdfFont($font);
@@ -1344,7 +1346,10 @@ class OutputDocument extends BaseOutputDocument
// Build the CSS content // Build the CSS content
foreach ($fonts as $font) { foreach ($fonts as $font) {
$css .= str_replace(['@familyName', '@fileName', '@properties'], [$font['familyName'], $font['fileName'], $font['properties']], $template); if ($font['inTinyMce']) {
$css .= str_replace(['@familyName', '@fileName', '@properties'],
[$font['familyName'], $font['fileName'], $font['properties']], $template);
}
} }
// Save the CSS file // Save the CSS file