PMCORE-2097
This commit is contained in:
@@ -386,6 +386,23 @@ EOT
|
||||
);
|
||||
CLI::taskRun("run_artisan");
|
||||
|
||||
/**
|
||||
* Add new font to be used in Output Documents generation (TinyMCE editor and TCPDF library)
|
||||
*/
|
||||
CLI::taskName('output-documents-add-font');
|
||||
CLI::taskDescription(<<<EOT
|
||||
Add new font to be used in Output Documents generation (TinyMCE editor and TCPDF library).
|
||||
EOT
|
||||
);
|
||||
CLI::taskOpt('font_type', <<<EOT
|
||||
Can be "TrueType" or "TrueTypeUnicode", if the option is not specified the default value is "TrueType"
|
||||
EOT
|
||||
,'ft', 'font_type=');
|
||||
CLI::taskArg('fontFileName', false);
|
||||
CLI::taskArg('friendlyName', true);
|
||||
CLI::taskArg('fontProperties', true);
|
||||
CLI::taskRun('output_documents_add_font');
|
||||
|
||||
/**
|
||||
* Function run_info
|
||||
*
|
||||
@@ -1407,3 +1424,81 @@ function run_artisan($args)
|
||||
CLI::logging("> The --workspace option is undefined.\n");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new font to be used in Output Documents generation (TinyMCE editor and TCPDF library)
|
||||
*
|
||||
* @param array $args
|
||||
* @param array $options
|
||||
*/
|
||||
function output_documents_add_font($args, $options)
|
||||
{
|
||||
try {
|
||||
// Validate the main required argument
|
||||
if (empty($args)) {
|
||||
throw new Exception('Please send the font filename.');
|
||||
}
|
||||
|
||||
// Load and initialize optional arguments and options
|
||||
$fontFileName = $args[0];
|
||||
$fontFriendlyName = $args[1] ?? '';
|
||||
$fontProperties = $args[2] ?? '';
|
||||
$fontType = $options['font_type'] ?? 'TrueType';
|
||||
$name = '';
|
||||
|
||||
// Check fonts path
|
||||
OutputDocument::checkTcPdfFontsPath();
|
||||
|
||||
// Check if the font file exist
|
||||
if (!file_exists(PATH_DATA . 'fonts' . PATH_SEP . $fontFileName)) {
|
||||
throw new Exception("Font '{$fontFileName}' not exists.");
|
||||
}
|
||||
|
||||
// Check if the font file was already added
|
||||
if (OutputDocument::existTcpdfFont($fontFileName)) {
|
||||
throw new Exception("Font '{$fontFileName}' already added.");
|
||||
}
|
||||
|
||||
// Check if the friendly font name is valid
|
||||
if (preg_match('/[^0-9A-Za-z ]/', $fontFriendlyName)) {
|
||||
throw new Exception('The friendly font name is using an incorrect format please use only letters, numbers and spaces.');
|
||||
}
|
||||
|
||||
// Check if the font type is valid
|
||||
if (!in_array($fontType, ['TrueType', 'TrueTypeUnicode'])) {
|
||||
throw new Exception("Font type '{$fontType}' is invalid.");
|
||||
}
|
||||
|
||||
// Convert TTF file to the format required by TCPDF library
|
||||
$tcPdfFont = TCPDF_FONTS::addTTFfont(PATH_DATA . 'fonts' . PATH_SEP . $fontFileName, $fontType);
|
||||
|
||||
// Check if the conversion was successful
|
||||
if ($tcPdfFont === false) {
|
||||
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
|
||||
$font = [
|
||||
'fileName' => $fontFileName,
|
||||
'familyName' => $fontFamilyName,
|
||||
'friendlyName' => !empty($fontFriendlyName) ? $fontFriendlyName : $fontFamilyName,
|
||||
'properties' => $fontProperties
|
||||
];
|
||||
OutputDocument::addTcPdfFont($font);
|
||||
|
||||
// Print finalization message
|
||||
CLI::logging("Font '{$fontFileName}' added successfully." . PHP_EOL . PHP_EOL);
|
||||
} catch (Exception $e) {
|
||||
// Display the error message
|
||||
CLI::logging($e->getMessage() . PHP_EOL . PHP_EOL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -798,7 +798,7 @@ class OutputDocument extends BaseOutputDocument
|
||||
public function generateTcpdf($outDocUid, $fields, $path, $filename, $content, $landscape = false, $properties = [])
|
||||
{
|
||||
// Check and prepare the fonts path used by TCPDF library
|
||||
self::checkTcpdfFontsPath();
|
||||
self::checkTcPdfFontsPath();
|
||||
|
||||
// Including the basic configuration for the TCPDF library
|
||||
require_once PATH_TRUNK . "vendor" . PATH_SEP . "tecnickcom" . PATH_SEP . "tcpdf" . PATH_SEP . "config" . PATH_SEP . "tcpdf_config.php";
|
||||
@@ -1198,7 +1198,7 @@ class OutputDocument extends BaseOutputDocument
|
||||
/**
|
||||
* Check and prepare the fonts path used by TCPDF library
|
||||
*/
|
||||
public static function checkTcpdfFontsPath()
|
||||
public static function checkTcPdfFontsPath()
|
||||
{
|
||||
// Define the path of the fonts, "K_PATH_FONTS" is a constant used by "TCPDF" library
|
||||
define('K_PATH_FONTS', PATH_DATA . 'fonts' . PATH_SEP . 'tcpdf' . PATH_SEP);
|
||||
@@ -1215,4 +1215,94 @@ class OutputDocument extends BaseOutputDocument
|
||||
$filesystem->copyDirectory(PATH_TRUNK . 'vendor' . PATH_SEP . 'tecnickcom' . PATH_SEP . 'tcpdf' . PATH_SEP . 'fonts' . PATH_SEP, K_PATH_FONTS);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the custom fonts list
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function loadTcPdfFontsList()
|
||||
{
|
||||
// Initialize variables
|
||||
$jsonFilePath = K_PATH_FONTS . 'fonts.json';
|
||||
|
||||
// Load the custom fonts list
|
||||
if (file_exists($jsonFilePath)) {
|
||||
$fonts = json_decode(file_get_contents($jsonFilePath), true);
|
||||
} else {
|
||||
$fonts = [];
|
||||
}
|
||||
|
||||
return $fonts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the custom fonts list
|
||||
*
|
||||
* @param $fonts
|
||||
*/
|
||||
public static function saveTcPdfFontsList($fonts)
|
||||
{
|
||||
// Initialize variables
|
||||
$jsonFilePath = K_PATH_FONTS . 'fonts.json';
|
||||
|
||||
// Save the JSON file
|
||||
file_put_contents($jsonFilePath, json_encode($fonts));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a font file name exist in the fonts list
|
||||
*
|
||||
* @param string $fontFileName
|
||||
* @return bool
|
||||
*/
|
||||
public static function existTcpdfFont($fontFileName)
|
||||
{
|
||||
// Load the custom fonts list
|
||||
$fonts = self::loadTcPdfFontsList();
|
||||
|
||||
// Exist?
|
||||
return isset($fonts[$fontFileName]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a custom font to be used by TCPDF library
|
||||
*
|
||||
* @param array $font
|
||||
*/
|
||||
public static function addTcPdfFont($font)
|
||||
{
|
||||
// Load the custom fonts list
|
||||
$fonts = self::loadTcPdfFontsList();
|
||||
|
||||
// Add the font
|
||||
$fonts[$font['fileName']] = $font;
|
||||
|
||||
// Save the fonts list
|
||||
self::saveTcPdfFontsList($fonts);
|
||||
|
||||
// Re-generate CSS file
|
||||
self::generateCssFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate CSS with the fonts definition to be used by TinyMCE editor
|
||||
*/
|
||||
private static function generateCssFile()
|
||||
{
|
||||
// Initialize variables
|
||||
$template = "@font-face {font-family: @familyName;src: url(@fileName) format('truetype');@properties}\n";
|
||||
$css = '';
|
||||
|
||||
// Load the custom fonts list
|
||||
$fonts = self::loadTcPdfFontsList();
|
||||
|
||||
// Build the CSS content
|
||||
foreach ($fonts as $font) {
|
||||
$css .= str_replace(['@familyName', '@fileName', '@properties'], [$font['familyName'], $font['fileName'], $font['properties']], $template);
|
||||
}
|
||||
|
||||
// Save the CSS file
|
||||
file_put_contents(K_PATH_FONTS . 'fonts.css', $css);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user