[TikiWiki-commits] [Git][tikiwiki/tiki][27.x] [BP][ENH] PluginPDF: add enhanced coverpage settings as experimental preferences
"ushindi bienvenu \(@usbbush\) via TikiWiki-cvs" <[email protected]>
| Newsgroups | gmane.comp.cms.tiki.cvs |
|---|---|
| Message-ID | <6a1c9e3f416e5_38badac40962b6@gitlab-sidekiq-low-urgency-cpu-bound-v2-985c55b68-crblt.mail> |
ushindi bienvenu pushed to branch 27.x at Tiki Wiki CMS Groupware / Tiki
Commits:
bd52cb27 by Adrien Mbuya Maloba at 2026-05-31T20:40:29+00:00
[BP][ENH] PluginPDF: add enhanced coverpage settings as experimental preferences
---
* [FIX] pdflib: Ensure temp/pdfimg files are deleted even with permission issues
---
* [FIX] pdflib: Ensure temp/pdfimg files are deleted even with permission issues
---
* [FIX] pdflib: Ensure temp/pdfimg files are deleted even with permission issues
See merge request tikiwiki/tiki!9374
(cherry picked from commit bcc4b2157eff2be53ab3aeb01bbf4f4958cf3b6c)
See merge request tikiwiki/tiki!10403
* [FIX] cover page functionality fix in PDF generation with customizable settings
---
* [FIX] cover page functionality fix in PDF generation with customizable settings
This MR refines the behavior of the coverpage_wiki setting in Tiki's PDF export system.
* Ensures only the first page generated from the `coverpage_wiki` content is rendered as the PDF cover page.
* Permission (`tiki_p_view`) check is added when generating cover page from the page set as `coverpage_wiki`.
* The remaining pages contain the content of the current page being exported.
* Supports and preserves `coverpage_image_settings` as a background for the coverpage_wiki-based cover page.
* Ensures that `coverpage_settings` (like background color and border) apply correctly on the cover.
See merge request tikiwiki/tiki!7400
* [ENH] Adds enhanced coverpage settings as experimental preferences;
---
* [ENH] Adds enhanced coverpage settings as experimental preferences;
Ref: https://avan.tech/item102479
See merge request tikiwiki/tiki!5071
See merge request tikiwiki/tiki!10214
- - - - -
5 changed files:
- lib/pdflib.php
- lib/prefs/print.php
- lib/wiki-plugins/wikiplugin_pdf.php
- path_constants.php
- templates/admin/include_print.tpl
Changes:
=====================================
lib/pdflib.php
=====================================
@@ -257,7 +257,7 @@ class PdfGenerator
*/
private function mpdf(string $url, string $parsedData = '', array $params = []): string
{
- global $prefs;
+ global $prefs, $user;
$page = '';
if ($parsedData != '') {
$html = $parsedData;
@@ -433,6 +433,85 @@ class PdfGenerator
$bodycss = '';
}
+ if (! empty($pdfSettings['coverpage_wiki'])) {
+ $coverWikiPage = $pdfSettings['coverpage_wiki'];
+ $tikilib = TikiLib::lib('tiki');
+ $isPermissionToViewPage = $tikilib->user_has_perm_on_object($user, $coverWikiPage, 'wiki page', 'tiki_p_view');
+ if (empty($isPermissionToViewPage)) {
+ $title = tr('PDF Cover Page Error');
+ $mes = tr('PDF generation failed because you do not have permission to view the specified cover page: %0', $coverWikiPage);
+ $this->clearTempImg($tempImgArr);
+ Feedback::error(['mes' => $mes, 'title' => $title]);
+ TikiLib::lib('access')->redirect($params['page'] ?? '');
+ die();
+ }
+ $pageInfo = $tikilib->get_page_info($coverWikiPage);
+ if (! empty($pageInfo)) {
+ $coverHtml = TikiLib::lib('parser')->parse_data(
+ $pageInfo['data'],
+ [
+ 'is_html' => $pageInfo['is_html'],
+ 'print' => 'y',
+ 'namespace' => $pageInfo['namespace']
+ ]
+ );
+
+ $coverStyle = 'min-height:100%;padding:30px;';
+
+ if (! empty($pdfSettings['coverpage_image_settings']) && $pdfSettings['coverpage_image_settings'] != 'off') {
+ $coverStyle .= 'background-image:url(' . $pdfSettings['coverpage_image_settings'] . ');background-size:cover;background-repeat:no-repeat;background-position:center;';
+ $pdfSettings['coverpage_image_settings'] = 'off';
+ }
+
+ if (! empty($pdfSettings['coverpage_settings'])) {
+ list($bgColor, $borderWidth, $borderColor) = explode('|', $pdfSettings['coverpage_settings'] . '||');
+ $pdfSettings['coverpage_settings'] = 'off';
+ if (! empty($bgColor)) {
+ $coverStyle .= 'background-color:' . $bgColor . ';';
+ }
+ if (! empty($borderWidth)) {
+ $coverStyle .= 'border:' . $borderWidth . 'px solid ' . (! empty($borderColor) ? $borderColor : 'black') . ';';
+ }
+ }
+
+ $coverHtml = '<div style="' . $coverStyle . '">' . $coverHtml . '</div>';
+
+ $coverMpdf = new \Mpdf\Mpdf($mpdfConfig);
+ $coverMpdf->SetHTMLHeader();
+ $coverMpdf->SetHTMLFooter();
+ $coverMpdf->AddPage(
+ $pdfSettings['orientation'],
+ '',
+ '',
+ '',
+ '',
+ $pdfSettings['margin_left'],
+ $pdfSettings['margin_right'],
+ $pdfSettings['margin_top'],
+ $pdfSettings['margin_bottom'],
+ $pdfSettings['margin_header'],
+ $pdfSettings['margin_footer']
+ );
+
+ // write HTML
+ $coverMpdf->WriteHTML($coverHtml);
+
+ $coverPdfString = $coverMpdf->Output('', \Mpdf\Output\Destination::STRING_RETURN);
+ $coverTempFile = writeTempFile($coverPdfString, 'pdfcover/', false, 'coverpage_', '.pdf');
+
+ $mpdf->setSourceFile($coverTempFile);
+ $tplId = $mpdf->importPage(1);
+ $tplSize = $mpdf->getTemplateSize($tplId);
+
+ $mpdf->AddPage($tplSize['orientation']);
+ $mpdf->UseTemplate($tplId, 0, 0, $tplSize['width'], $tplSize['height'], true);
+
+ if (file_exists($coverTempFile)) {
+ unlink($coverTempFile);
+ }
+ }
+ }
+
$pdfPages = $this->getPDFPages($html, $pdfSettings);
//adding css styles with first page content
$allCss = '<style>' .
@@ -462,7 +541,11 @@ class PdfGenerator
$cssStyles = str_replace(["background-color: #fff;","background:#fff;"], "background:none", $cssStyles);
}
//cover page checking
- if ($pdfSettings['coverpage_text_settings'] != '' || ! empty($pdfSettings['coverpage_settings']) || ($pdfSettings['coverpage_image_settings'] != '' && $pdfSettings['coverpage_image_settings'] != 'off')) {
+ if (
+ ! empty($pdfSettings['coverpage_text_settings'])
+ || (! empty($pdfSettings['coverpage_settings']) && $pdfSettings['coverpage_settings'] !== 'off')
+ || (! empty($pdfSettings['coverpage_image_settings']) && $pdfSettings['coverpage_image_settings'] !== 'off')
+ ) {
$coverPage = explode("|", $pdfSettings['coverpage_text_settings']);
$coverImage = $pdfSettings['coverpage_image_settings'] != 'off' ? $pdfSettings['coverpage_image_settings'] : '';
$mpdf->SetHTMLHeader(); //resetting header footer for cover page
@@ -472,8 +555,7 @@ class PdfGenerator
$textAlign = 'center';
$textBgColor = '';
$coverPageTextBorder = '';
- $coverPageBgColor = '';
- $coverPageBorder = '';
+ $subHeading = $coverPage[1] ?? '';
if (count($coverPage) === 1 && ! empty($pdfSettings['coverpage_settings'])) {
list($bgColorValue, $borderWidthValue, $borderColorValue) = explode('|', $pdfSettings['coverpage_settings'] . '||');
$coverPageBgColor = ! empty($bgColorValue) ? "background-color:{$bgColorValue};" : '';
@@ -488,34 +570,40 @@ class PdfGenerator
$textBgColor = ! empty($coverPage[3]) ? "background-color:{$coverPage[3]};" : '';
$textColor = ! empty($coverPage[4]) ? "color:{$coverPage[4]};" : '';
}
+ $mpdf->WriteHTML('<body style="' . $coverPageBgColor . 'margin:0px;padding:0px"><div style="height:100%;background-image:url(' . $coverImage . ');padding:20px;background-repeat: no-repeat;background-position: center; "><div style="' . $coverPageBorder . 'height:95%;">
+ <div style="text-align:' . $textAlign . ';margin-top:30%;' . $textColor . '"><div style="' . $textBgColor . $coverPageTextBorder . 'margin-bottom:10px;font-size:50px;">' . $coverPage[0] . '</div>' . $subHeading . '</div></div></body>');
+ $textColor = '';
+ $textAlign = 'center';
+ $textBgColor = '';
+ $coverPageTextBorder = '';
+ $coverPageBgColor = '';
+ $coverPageBorder = '';
+ if (! empty($pdfSettings['coverpage_settings'])) {
+ list($bgColorValue, $borderWidthValue, $borderColorValue) = explode('|', $pdfSettings['coverpage_settings'] . '||');
+ $coverPageBgColor = ! empty($bgColorValue) ? "background-color:{$bgColorValue};" : '';
+ $coverPageBorder = ! empty($borderWidthValue) ? "border:{$borderWidthValue}px solid " . (! empty($borderColorValue) ? "{$borderColorValue};" : 'black;') : '';
+ }
+ if (count($coverPage) > 5) {
+ $borderWidth = empty($coverPage[5]) ? 1 : $coverPage[5];
+ $coverPageTextBorder = "border:{$borderWidth}px solid {$coverPage[6]};";
+ }
+ $textAlign = empty($coverPage[2]) ? 'center' : $coverPage[2];
+ $textBgColor = ! empty($coverPage[3]) ? "background-color:{$coverPage[3]};" : '';
+ $textColor = ! empty($coverPage[4]) ? "color:{$coverPage[4]};" : '';
for ($i = 0; $i <= 1; $i++) {
$coverPage[$i] = str_ireplace(["{PAGETITLE}","{NB}"], [$page,"{nb}"], TikiLib::lib('parser')->parse_data(html_entity_decode($coverPage[$i] ?? ''), ['is_html' => true, 'parse_wiki' => true]));
$coverPage[$i] = preg_replace_callback('/\{DATE\s+(.*?)\}/', function ($matches) {
return date($matches[1]);
}, $coverPage[$i]);
}
- $bodyStyle = "{$coverPageBgColor}margin:0px;padding:0px";
- $containerStyle = "height:100%;background-image:url({$coverImage});padding:20px;background-repeat:no-repeat;background-position:center;";
- $borderStyle = "{$coverPageBorder}height:95%;";
- $contentStyle = "text-align:{$textAlign};margin-top:30%;{$textColor}";
- $titleStyle = "{$textBgColor}{$coverPageTextBorder}margin-bottom:10px;font-size:50px;";
-
- $htmlChunk = <<<HTML
- <body style="$bodyStyle">
- <div style="$containerStyle">
- <div style="$borderStyle">
- <div style="$contentStyle">
- <div style="$titleStyle">{$coverPage[0]}</div>
- {$coverPage[1]}
- </div>
- </div>
- </div>
- </body>
- HTML;
- if ($prefs['print_pdf_mpdf_debug'] === 'y') {
- $debugOutput .= $htmlChunk;
+ $htm = '<body style="' . $coverPageBgColor . 'margin:0px;padding:0px"><div style="height:100%;background-image:url(' . $coverImage . ');background-size:cover;background-repeat: no-repeat;background-position: center;padding:20px;">';
+ if (! empty($coverPage[0]) || ! empty($coverPage[1])) {
+ $htm .= '<div style="' . $coverPageBorder . 'height:95%;">
+ <div style="text-align:' . $textAlign . ';margin-top:30%;' . $textColor . '">
+ <div style="' . $textBgColor . $coverPageTextBorder . 'margin-bottom:10px;font-size:50px;">' . $coverPage[0] . '</div>' . $coverPage[1] . '</div></div>';
}
- $mpdf->WriteHTML($htmlChunk);
+ $htm .= '</div></body>';
+ $mpdf->WriteHTML($htm);
}
//Checking bookmark
if (is_array($pdfSettings['autobookmarks'])) {
@@ -901,8 +989,8 @@ class PdfGenerator
$pdfSettings['pagetitle'] = $prefs['print_pdf_mpdf_pagetitle'];
$pdfSettings['watermark'] = $prefs['print_pdf_mpdf_watermark'];
$pdfSettings['watermark_image'] = $prefs['print_pdf_mpdf_watermark_image'];
- $coverPageContentKey = ! empty($prefs['print_pdf_mpdf_coverpage_wiki']) ? 'coverpage_wiki' : 'coverpage_text_settings';
- $pdfSettings['coverpage_text_settings'] = str_ireplace("{PAGETITLE}", $params['page'] ?? '', $prefs["print_pdf_mpdf_{$coverPageContentKey}"]);
+ $pdfSettings['coverpage_wiki'] = $prefs['print_pdf_mpdf_coverpage_wiki'];
+ $pdfSettings['coverpage_text_settings'] = empty($prefs['print_pdf_mpdf_coverpage_wiki']) ? str_ireplace("{PAGETITLE}", $params['page'] ?? '', $prefs['print_pdf_mpdf_coverpage_text_settings']) : '';
$pdfSettings['coverpage_settings'] = $prefs['print_pdf_mpdf_coverpage_settings'];
$pdfSettings['coverpage_image_settings'] = str_ireplace("{PAGETITLE}", $params['page'] ?? '', $prefs['print_pdf_mpdf_coverpage_image_settings']);
$pdfSettings['hyperlinks'] = $prefs['print_pdf_mpdf_hyperlinks'];
@@ -1051,9 +1139,9 @@ class PdfGenerator
if (! $internalImg) {
$url = $base_url . $url;
}
- if (! file_exists('temp/pdfimg')) {
- mkdir('temp/pdfimg');
- chmod('temp/pdfimg', 0755);
+ if (! file_exists(TEMP_PDFIMG_PATH)) {
+ mkdir(TEMP_PDFIMG_PATH);
+ chmod(TEMP_PDFIMG_PATH, 0755);
}
$cookie = isset($_SERVER['HTTP_COOKIE']) ? $_SERVER['HTTP_COOKIE'] : '';
$opts = [];
@@ -1069,7 +1157,7 @@ class PdfGenerator
if (gettype($data) == 'boolean' && ! $data) {
return '';
}
- $newFile = 'temp/pdfimg/pdfimg' . mt_rand(9999, 999999) . '.png';
+ $newFile = TEMP_PDFIMG_PATH . '/pdfimg' . mt_rand(9999, 999999) . '.png';
file_put_contents($newFile, $data);
chmod($newFile, 0755);
return $newFile;
=====================================
lib/prefs/print.php
=====================================
@@ -295,6 +295,20 @@ function prefs_print_list()
'type' => 'text',
'default' => '',
],
+ 'print_pdf_mpdf_coverpage_wiki' => [
+ 'name' => tra('CoverPage wiki'),
+ 'description' => tra('Name of the wiki page that will be used for the text of the coverpage.'),
+ 'tags' => ['advanced', 'experimental'],
+ 'type' => 'text',
+ 'default' => '',
+ ],
+ 'print_pdf_mpdf_coverpage_settings' => [
+ 'name' => tra('CoverPage settings'),
+ 'description' => tra('Background color|Page border|Border color. Enter settings separated by |. Leave blank for default settings. Sequence is important, for example <code>#fff|1|#000</code>'),
+ 'tags' => ['advanced', 'experimental'],
+ 'type' => 'text',
+ 'default' => '',
+ ],
'print_pdf_mpdf_coverpage_image_settings' => [
'name' => tra('Coverpage image URL'),
'description' => tra('Enter the full URL.'),
=====================================
lib/wiki-plugins/wikiplugin_pdf.php
=====================================
@@ -267,6 +267,20 @@ function wikiplugin_pdf_info()
'type' => 'text',
'default' => '',
],
+ 'coverpage_wiki' => [
+ 'name' => tra('CoverPage wiki'),
+ 'description' => tra('Name of the wiki page that will be used for the text of the coverpage.'),
+ 'required' => false,
+ 'type' => 'text',
+ 'default' => '',
+ ],
+ 'coverpage_settings' => [
+ 'name' => tra('CoverPage settings'),
+ 'description' => tra('<code>Background color|Page border|Border color</code>. Enter settings separated by <code>|</code>,sequence is important,leave blank for default. For example <code>#fff|1|#000</code>'),
+ 'required' => false,
+ 'type' => 'text',
+ 'default' => '',
+ ],
'coverpage_image_settings' => [
'name' => tra('Coverpage image URL'),
'description' => tra('Enter the full URL.'),
=====================================
path_constants.php
=====================================
@@ -96,6 +96,7 @@ const TEMP_HTTP_PUBLIC_PATH = 'temp/public';
const TEMP_CACHE_PATH = 'temp/cache';
const TEMP_PUBLIC_PATH = 'temp/public';
const HTMLPURIFIERCACHE_CACHE_PATH = 'temp/cache/HTMLPurifierCache';
+const TEMP_PDFIMG_PATH = 'temp/pdfimg';
const WIKIPLUGINS_SRC_PATH = 'lib/wiki-plugins';
//Question: where is the string or constant that generates these files?
=====================================
templates/admin/include_print.tpl
=====================================
@@ -42,6 +42,8 @@
{preference name=print_pdf_mpdf_background}
{preference name=print_pdf_mpdf_background_image}
{preference name=print_pdf_mpdf_coverpage_text_settings}
+ {preference name=print_pdf_mpdf_coverpage_wiki}
+ {preference name=print_pdf_mpdf_coverpage_settings}
{preference name=print_pdf_mpdf_coverpage_image_settings}
{preference name=print_pdf_mpdf_allow_unsafe_ssl_requests}
{preference name=print_pdf_mpdf_debug}
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/bd52cb27b65036bfb1379cbb19c51e86a669609a
--
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/bd52cb27b65036bfb1379cbb19c51e86a669609a
You're receiving this email because of your account on gitlab.com. Manage all notifications: https://gitlab.com/-/profile/notifications | Help: https://gitlab.com/help
_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs