Merged in bugfix/PMC-1325 (pull request #7190)
PMC-1325 Approved-by: Paula Quispe <paula.quispe@processmaker.com>
This commit is contained in:
@@ -778,17 +778,38 @@ class OutputDocument extends BaseOutputDocument
|
|||||||
unlink(PATH_OUTPUT_FILE_DIRECTORY . $sFilename . '.html');
|
unlink(PATH_OUTPUT_FILE_DIRECTORY . $sFilename . '.html');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generateTcpdf($sUID, $aFields, $sPath, $sFilename, $sContent, $sLandscape = false, $aProperties = array())
|
/**
|
||||||
|
* Generate a PDF file using the TCPDF library
|
||||||
|
*
|
||||||
|
* @param string $outDocUid
|
||||||
|
* @param array $fields
|
||||||
|
* @param string $path
|
||||||
|
* @param string $filename
|
||||||
|
* @param string $content
|
||||||
|
* @param bool $landscape
|
||||||
|
* @param array $properties
|
||||||
|
*
|
||||||
|
* @see generate()
|
||||||
|
* @see \ProcessMaker\BusinessModel\Cases\OutputDocument::generate()
|
||||||
|
*
|
||||||
|
* @link https://wiki.processmaker.com/3.3/Output_Documents#Creating_Output_Documents_Usign_TCPDF_Generator
|
||||||
|
*/
|
||||||
|
public function generateTcpdf($outDocUid, $fields, $path, $filename, $content, $landscape = false, $properties = [])
|
||||||
{
|
{
|
||||||
require_once(PATH_THIRDPARTY . "tcpdf" . PATH_SEP . "config" . PATH_SEP . "lang" . PATH_SEP . "eng.php");
|
// Including the basic configuration for the TCPDF library
|
||||||
require_once(PATH_THIRDPARTY . "tcpdf" . PATH_SEP . "tcpdf.php");
|
require_once PATH_TRUNK . "vendor" . PATH_SEP . "tecnickcom" . PATH_SEP . "tcpdf" . PATH_SEP . "config" . PATH_SEP . "tcpdf_config.php";
|
||||||
|
|
||||||
$nrt = array("\n", "\r", "\t");
|
// Initialize variables
|
||||||
$nrthtml = array("(n /)", "(r /)", "(t /)");
|
$nrt = ["\n", "\r", "\t"];
|
||||||
|
$nrtHtml = ["(n /)", "(r /)", "(t /)"];
|
||||||
$strContentAux = str_replace($nrt, $nrthtml, $sContent);
|
$outputType = 2;
|
||||||
$sContent = null;
|
$orientation = ($landscape == false) ? PDF_PAGE_ORIENTATION : 'L';
|
||||||
|
$media = (isset($properties['media'])) ? $properties['media'] : PDF_PAGE_FORMAT;
|
||||||
|
$lang = (defined('SYS_LANG')) ? SYS_LANG : 'en';
|
||||||
|
$strContentAux = str_replace($nrt, $nrtHtml, $content);
|
||||||
|
$content = null;
|
||||||
|
|
||||||
|
// Convert the deprecated "font" tags into "style" tags
|
||||||
while (preg_match("/^(.*)<font([^>]*)>(.*)$/i", $strContentAux, $arrayMatch)) {
|
while (preg_match("/^(.*)<font([^>]*)>(.*)$/i", $strContentAux, $arrayMatch)) {
|
||||||
$str = trim($arrayMatch[2]);
|
$str = trim($arrayMatch[2]);
|
||||||
$strAttribute = null;
|
$strAttribute = null;
|
||||||
@@ -804,7 +825,7 @@ class OutputDocument extends BaseOutputDocument
|
|||||||
|
|
||||||
$str = $strAux . $str;
|
$str = $strAux . $str;
|
||||||
|
|
||||||
//Get attributes
|
// Get attributes
|
||||||
$strStyle = null;
|
$strStyle = null;
|
||||||
$array = explode(" ", $str);
|
$array = explode(" ", $str);
|
||||||
|
|
||||||
@@ -813,7 +834,7 @@ class OutputDocument extends BaseOutputDocument
|
|||||||
|
|
||||||
if (isset($arrayAux[1])) {
|
if (isset($arrayAux[1])) {
|
||||||
$a = trim($arrayAux[0]);
|
$a = trim($arrayAux[0]);
|
||||||
$v = trim(str_replace(array("__SPACE__", "\"", "'"), array(" ", null, null), $arrayAux[1]));
|
$v = trim(str_replace(["__SPACE__", "\"", "'"], [" ", null, null], $arrayAux[1]));
|
||||||
|
|
||||||
switch (strtolower($a)) {
|
switch (strtolower($a)) {
|
||||||
case "color":
|
case "color":
|
||||||
@@ -823,7 +844,7 @@ class OutputDocument extends BaseOutputDocument
|
|||||||
$strStyle = $strStyle . "font-family: $v;";
|
$strStyle = $strStyle . "font-family: $v;";
|
||||||
break;
|
break;
|
||||||
case "size":
|
case "size":
|
||||||
$arrayPt = array(0, 8, 10, 12, 14, 18, 24, 36);
|
$arrayPt = [0, 8, 10, 12, 14, 18, 24, 36];
|
||||||
$strStyle = $strStyle . "font-size: " . $arrayPt[intval($v)] . "pt;";
|
$strStyle = $strStyle . "font-size: " . $arrayPt[intval($v)] . "pt;";
|
||||||
break;
|
break;
|
||||||
case "style":
|
case "style":
|
||||||
@@ -842,39 +863,35 @@ class OutputDocument extends BaseOutputDocument
|
|||||||
}
|
}
|
||||||
|
|
||||||
$strContentAux = $arrayMatch[1];
|
$strContentAux = $arrayMatch[1];
|
||||||
$sContent = "<span" . $strAttribute . ">" . $arrayMatch[3] . $sContent;
|
$content = "<span" . $strAttribute . ">" . $arrayMatch[3] . $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
$sContent = $strContentAux . $sContent;
|
// Replenish the content
|
||||||
|
$content = $strContentAux . $content;
|
||||||
|
|
||||||
$sContent = str_ireplace("</font>", "</span>", $sContent);
|
// Replace some remaining incorrect/deprecated HTML tags/properties
|
||||||
|
$content = str_ireplace("</font>", "</span>", $content);
|
||||||
|
$content = str_replace($nrtHtml, $nrt, $content);
|
||||||
|
$content = str_replace("margin-left", "text-indent", $content);
|
||||||
|
|
||||||
$sContent = str_replace($nrthtml, $nrt, $sContent);
|
// Instance the TCPDF library
|
||||||
|
$pdf = new TCPDF($orientation, PDF_UNIT, $media, true, 'UTF-8', false);
|
||||||
|
|
||||||
$sContent = str_replace("margin-left", "text-indent", $sContent);
|
// Set document information
|
||||||
|
|
||||||
// define Save file
|
|
||||||
$sOutput = 2;
|
|
||||||
$sOrientation = ($sLandscape == false) ? PDF_PAGE_ORIENTATION : 'L';
|
|
||||||
$sMedia = (isset($aProperties['media'])) ? $aProperties['media'] : PDF_PAGE_FORMAT;
|
|
||||||
$sLang = (defined('SYS_LANG')) ? SYS_LANG : 'en';
|
|
||||||
|
|
||||||
// create new PDF document
|
|
||||||
$pdf = new TCPDF($sOrientation, PDF_UNIT, $sMedia, true, 'UTF-8', false);
|
|
||||||
|
|
||||||
// set document information
|
|
||||||
$pdf->SetCreator(PDF_CREATOR);
|
$pdf->SetCreator(PDF_CREATOR);
|
||||||
$pdf->SetAuthor($aFields['USR_USERNAME']);
|
$pdf->SetAuthor($fields['USR_USERNAME']);
|
||||||
$pdf->SetTitle('Processmaker');
|
$pdf->SetTitle('ProcessMaker');
|
||||||
$pdf->SetSubject($sFilename);
|
$pdf->SetSubject($filename);
|
||||||
$pdf->SetCompression(true);
|
$pdf->SetCompression(true);
|
||||||
|
|
||||||
$margins = $aProperties['margins'];
|
// Define margins
|
||||||
|
$margins = $properties['margins'];
|
||||||
$margins["left"] = ($margins["left"] >= 0) ? $margins["left"] : PDF_MARGIN_LEFT;
|
$margins["left"] = ($margins["left"] >= 0) ? $margins["left"] : PDF_MARGIN_LEFT;
|
||||||
$margins["top"] = ($margins["top"] >= 0) ? $margins["top"] : PDF_MARGIN_TOP;
|
$margins["top"] = ($margins["top"] >= 0) ? $margins["top"] : PDF_MARGIN_TOP;
|
||||||
$margins["right"] = ($margins["right"] >= 0) ? $margins["right"] : PDF_MARGIN_RIGHT;
|
$margins["right"] = ($margins["right"] >= 0) ? $margins["right"] : PDF_MARGIN_RIGHT;
|
||||||
$margins["bottom"] = ($margins["bottom"] >= 0) ? $margins["bottom"] : PDF_MARGIN_BOTTOM;
|
$margins["bottom"] = ($margins["bottom"] >= 0) ? $margins["bottom"] : PDF_MARGIN_BOTTOM;
|
||||||
|
|
||||||
|
// Set margins configuration
|
||||||
$pdf->setPrintHeader(false);
|
$pdf->setPrintHeader(false);
|
||||||
$pdf->setPrintFooter(false);
|
$pdf->setPrintFooter(false);
|
||||||
$pdf->SetLeftMargin($margins['left']);
|
$pdf->SetLeftMargin($margins['left']);
|
||||||
@@ -882,76 +899,67 @@ class OutputDocument extends BaseOutputDocument
|
|||||||
$pdf->SetRightMargin($margins['right']);
|
$pdf->SetRightMargin($margins['right']);
|
||||||
$pdf->SetAutoPageBreak(true, $margins['bottom']);
|
$pdf->SetAutoPageBreak(true, $margins['bottom']);
|
||||||
|
|
||||||
$oServerConf = ServerConf::getSingleton();
|
// Get ServerConf singleton
|
||||||
|
$serverConf = ServerConf::getSingleton();
|
||||||
|
|
||||||
// set some language dependent data:
|
// Set language configuration
|
||||||
$lg = [];
|
$lg = [];
|
||||||
$lg['a_meta_charset'] = 'UTF-8';
|
$lg['a_meta_charset'] = 'UTF-8';
|
||||||
$lg['a_meta_dir'] = ($oServerConf->isRtl($sLang)) ? 'rtl' : 'ltr';
|
$lg['a_meta_dir'] = ($serverConf->isRtl($lang)) ? 'rtl' : 'ltr';
|
||||||
$lg['a_meta_language'] = $sLang;
|
$lg['a_meta_language'] = $lang;
|
||||||
$lg['w_page'] = 'page';
|
$lg['w_page'] = 'page';
|
||||||
|
|
||||||
//set some language-dependent strings
|
|
||||||
$pdf->setLanguageArray($lg);
|
$pdf->setLanguageArray($lg);
|
||||||
|
|
||||||
if (isset($aProperties['pdfSecurity'])) {
|
// Set security configuration
|
||||||
$tcpdfPermissions = array('print', 'modify', 'copy', 'annot-forms', 'fill-forms', 'extract', 'assemble', 'print-high');
|
if (isset($properties['pdfSecurity'])) {
|
||||||
$pdfSecurity = $aProperties['pdfSecurity'];
|
$tcPdfPermissions = ['print', 'modify', 'copy', 'annot-forms', 'fill-forms', 'extract', 'assemble', 'print-high'];
|
||||||
$userPass = G::decrypt($pdfSecurity['openPassword'], $sUID);
|
$pdfSecurity = $properties['pdfSecurity'];
|
||||||
$ownerPass = ($pdfSecurity['ownerPassword'] != '') ? G::decrypt($pdfSecurity['ownerPassword'], $sUID) : null;
|
$userPass = G::decrypt($pdfSecurity['openPassword'], $outDocUid);
|
||||||
|
$ownerPass = ($pdfSecurity['ownerPassword'] != '') ? G::decrypt($pdfSecurity['ownerPassword'], $outDocUid) : null;
|
||||||
$permissions = explode("|", $pdfSecurity['permissions']);
|
$permissions = explode("|", $pdfSecurity['permissions']);
|
||||||
$permissions = array_diff($tcpdfPermissions, $permissions);
|
$permissions = array_diff($tcPdfPermissions, $permissions);
|
||||||
$pdf->SetProtection($permissions, $userPass, $ownerPass);
|
$pdf->SetProtection($permissions, $userPass, $ownerPass);
|
||||||
}
|
}
|
||||||
// ---------------------------------------------------------
|
|
||||||
// set default font subsetting mode
|
// Enable the font sub-setting option
|
||||||
$pdf->setFontSubsetting(true);
|
$pdf->setFontSubsetting(true);
|
||||||
|
|
||||||
// Set font
|
// Set unicode font if is required, we need to detect if is chinese, japanese, thai, etc.
|
||||||
// dejavusans is a UTF-8 Unicode font, if you only need to
|
if (preg_match('/[\x{30FF}\x{3040}-\x{309F}\x{4E00}-\x{9FFF}\x{0E00}-\x{0E7F}]/u', $content, $matches)) {
|
||||||
// print standard ASCII chars, you can use core fonts like
|
// The additional fonts should be in "shared/fonts" folder
|
||||||
// helvetica or times to reduce file size.
|
$fileArialUniTTF = PATH_DATA . "fonts" . PATH_SEP . "arialuni.ttf";
|
||||||
//$pdf->SetFont('dejavusans', '', 14, '', true);
|
$pdf->SetFont((!file_exists($fileArialUniTTF)) ? "kozminproregular" :
|
||||||
// Detect chinese, japanese, thai
|
$pdf->addTTFfont($fileArialUniTTF, "TrueTypeUnicode", "", 32));
|
||||||
if (preg_match('/[\x{30FF}\x{3040}-\x{309F}\x{4E00}-\x{9FFF}\x{0E00}-\x{0E7F}]/u', $sContent, $matches)) {
|
|
||||||
$fileArialunittf = PATH_THIRDPARTY . "tcpdf" . PATH_SEP . "fonts" . PATH_SEP . "arialuni.ttf";
|
|
||||||
|
|
||||||
$pdf->SetFont((!file_exists($fileArialunittf)) ? "kozminproregular" : $pdf->addTTFfont($fileArialunittf, "TrueTypeUnicode", "", 32));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a page
|
// Convert the encoding of the content if is UTF-8
|
||||||
// This method has several options, check the source code documentation for more information.
|
if (mb_detect_encoding($content) == 'UTF-8') {
|
||||||
$pdf->AddPage();
|
$content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8');
|
||||||
|
|
||||||
// set text shadow effect
|
|
||||||
//$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));
|
|
||||||
// Print text using writeHTMLCell()
|
|
||||||
// $pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
|
|
||||||
if (mb_detect_encoding($sContent) == 'UTF-8') {
|
|
||||||
$sContent = mb_convert_encoding($sContent, 'HTML-ENTITIES', 'UTF-8');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fix the HTML using DOMDocument class
|
||||||
$doc = new DOMDocument('1.0', 'UTF-8');
|
$doc = new DOMDocument('1.0', 'UTF-8');
|
||||||
if ($sContent != '') {
|
if ($content != '') {
|
||||||
$doc->loadHtml($sContent);
|
$doc->loadHtml($content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add a page and put the HTML fixed
|
||||||
|
$pdf->AddPage();
|
||||||
$pdf->writeHTML($doc->saveXML(), false, false, false, false, '');
|
$pdf->writeHTML($doc->saveXML(), false, false, false, false, '');
|
||||||
// ---------------------------------------------------------
|
|
||||||
// Close and output PDF document
|
// Generate the PDF file
|
||||||
// This method has several options, check the source code documentation for more information.
|
switch ($outputType) {
|
||||||
//$pdf->Output('example_00.pdf', 'I');
|
|
||||||
//$pdf->Output('/home/hector/processmaker/example_00.pdf', 'D');
|
|
||||||
switch ($sOutput) {
|
|
||||||
case 0:
|
case 0:
|
||||||
// Vrew browser
|
// Browser
|
||||||
$pdf->Output($sPath . $sFilename . '.pdf', 'I');
|
$pdf->Output($path . $filename . '.pdf', 'I');
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
// Donwnload
|
// Download
|
||||||
$pdf->Output($sPath . $sFilename . '.pdf', 'D');
|
$pdf->Output($path . $filename . '.pdf', 'D');
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
// Save file
|
// Save to file
|
||||||
$pdf->Output($sPath . $sFilename . '.pdf', 'F');
|
$pdf->Output($path . $filename . '.pdf', 'F');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user