Topic: Add stamp or watermark to reports on the fly – ad hoc. V2
Another way; but with a lot less changes to core:-
Add a third file to merge in \reporting\includes\pdf_report.inc
--- \reporting\includes\pdf_report ORIGINAL.inc
+++ \reporting\includes\pdf_report.inc
@@ -62,6 +62,7 @@
var $footerEnable; // select whether to print a page footer or not
var $footerText; // store user-generated footer text
var $headerTmpl; // store the name of the currently selected header template
+ var $sheaderTmpl;
var $tmplSize; // pdf header template size in pages
var $rep_id;
@@ -944,8 +945,17 @@
include($tmpl_php);
}
+ // include related php file if any
+ $stmpl_php = find_custom_file("/reporting/forms/".$this->sheaderTmpl."watermark.php");
+ if ($stmpl_php) {
+ include($stmpl_php);
+ }
+
if (method_exists($this, $this->headerTmpl)) // draw predefined page layout if any
$this->{$this->headerTmpl}();
+
+ if (method_exists($this, $this->sheaderTmpl)) // draw predefined page layout if any
+ $this->{$this->sheaderTmpl}();
}
function End($email=0, $subject='')
Add a link for a popup window in \reporting\includes\reports_classes.inc
--- \reporting\includes\reports_classes ORIGINAL.inc
+++ \reporting\includes\reports_classes.inc
@@ -103,7 +103,7 @@
false, '', $SysPrefs->pdf_debug ? false : 'default process') . hidden('REP_ID', $report->id, false).'<br><br>';
$st_params .= $this->getOptions($report->get_controls(), $report->id);
$st_params .= "\n<input type=hidden name='Class' value=".$cur_class.">"
- ."\n</form></td></tr></table>\n";
+ ."\n".label_cell(viewer_link(_('Add Watermark/Stamp'), 'reporting/includes/watermark_form.php?popup=1'))."</form></td></tr></table>\n";
set_focus('Rep'.$report->id);
$Ajax->addUpdate(true, 'rep_form', $st_params);
}
@@ -111,6 +111,7 @@
$st_reports .= "</table>";
}
+ $_SESSION["watermark"][0] = 0;
$st_params = "<div id='rep_form'>".
"$st_params</div>";
No more changes to core files
Create a file called \reporting\forms\watermark.php
<?php
if ($_SESSION["watermark"][0] == 1) {
$wm_text = $_SESSION["watermark"][1];
$wm_color = $_SESSION["watermark"][2];
$wm_font_size = $_SESSION["watermark"][3];
$wm_setx = $_SESSION["watermark"][4];
$wm_sety = $_SESSION["watermark"][5];
$wm_rotate = $_SESSION["watermark"][6];
$wm_alpha = $_SESSION["watermark"][7];
$this->SetX($wm_setx);
$this->SetY($wm_sety);
$this->SetFont('','B', $wm_font_size);
$this->setAlpha($wm_alpha);
$this->SetTextColorArray($this->convertHTMLColorToDec($wm_color));
$this->StartTransform();
$this->Rotate($wm_rotate);
$this->MultiCell(0,10, $wm_text,0,'C',0,1,0,0);
$this->StopTransform();
$this->setAlpha(1);
$this->SetTextColor(0, 0, 0);
$this->SetFont('','B', 9);
}
Create a file called \reporting\includes\watermark_form.php
<?php
$page_security = 'SA_OPEN';
$path_to_root="../..";
include_once($path_to_root . "/includes/session.inc");
$js = "";
page(_($help_context = "Add Watermark or Stamp"), @$_REQUEST['popup'], false, "", $js);
include_once($path_to_root . "/includes/ui.inc");
//--------------------------------------------------------------------------------------------
function html_type_input_cells_ex($label, $input_type, $name, $min, $max, $step, $init=null, $title=null,
$labparams=null, $post_label=null, $submit_on_change=false)
{
global $Ajax;
default_focus($name);
if (!isset($_POST[$name]) || $_POST[$name] == "")
{
if ($init)
$_POST[$name] = $init;
else
$_POST[$name] = "";
}
if ($label != null)
label_cell($label, $labparams);
echo "<td>";
$class = $submit_on_change ? 'class="searchbox"' : '';
echo "<input $class type=\"$input_type\" name=\"$name\" min=\"$min\" max=\"$max\" step=\"$step\" value=\"" . $_POST[$name]. "\""
.($title ? " title='$title'": '')." >";
if ($post_label)
echo " " . $post_label;
echo "</td>\n";
$Ajax->addUpdate($name, $name, $_POST[$name]);
}
function html_type_input_row_ex($label, $input_type, $name, $min, $max, $step, $title=null, $value=null, $params=null, $post_label=null)
{
echo "<tr><td class='label'>$label</td>";
html_type_input_cells_ex(null, $input_type, $name, $min, $max, $step, $value, $title, $params, $post_label);
echo "</tr>\n";
}
//--------------------------------------------------------------------------------------------
function can_process()
{
if (strlen($_POST['wm_text']) == 0)
{
display_error(_("The Watermark/Stamp Text cannot be empty."));
set_focus('wm_text');
return false;
}
if (!preg_match('/#([a-f0-9]{3}){1,2}\b/i', $_POST['wm_color']))
{
display_error(_("The input value - ".$_POST['wm_color']." is not a valid HTML colour. Input required #000 - #fff OR #000000 - #ffffff"));
set_focus('wm_color');
return false;
}
if (!check_num('wm_font_size', 4, 96))
{
display_error(_("The font size must be between 4 and 96."));
set_focus('wm_font_size');
return false;
}
if (!check_num('wm_setx', 1, 200))
{
display_error(_("Start Point from Left must be between 1 and 200."));
set_focus('wm_setx');
return false;
}
if (!check_num('wm_sety', 200, 612))
{
display_error(_("Start Point from Top must be between 200 and 612."));
set_focus('wm_sety');
return false;
}
if (!check_num('wm_rotate', 1, 360))
{
display_error(_("Rotation angle must be between 1 and 360."));
set_focus('wm_rotate');
return false;
}
if (!check_num('wm_alpha', 0.05, 1))
{
display_error(_("Transparency must be between 0.05 and 1 in steps of 0.05."));
set_focus('wm_alpha');
return false;
}
return true;
}
// unset($_SESSION['watermark']);// fixit TESTING ONLY
if(isset($_SESSION["watermark"][1]) && ($_SESSION["watermark"][1]) != '') {
$wm_on = $_SESSION["watermark"][0];
$wm_text = $_SESSION["watermark"][1];
$wm_color = $_SESSION["watermark"][2];
$wm_font_size = $_SESSION["watermark"][3];
$wm_setx = $_SESSION["watermark"][4];
$wm_sety = $_SESSION["watermark"][5];
$wm_rotate = $_SESSION["watermark"][6];
$wm_alpha = $_SESSION["watermark"][7];
} else {
$wm_text = '';
$wm_color = '#dddddd';
$wm_font_size = '48';
$wm_setx = '30';
$wm_sety = '550';
$wm_rotate = '45';
$wm_alpha = '.5';
}
$def = get_post(array('wm_text', 'wm_color', 'wm_font_size', 'wm_setx', 'wm_sety', 'wm_rotate', 'wm_alpha'));
$_POST['wm_text'] = $def["wm_text"];
$_POST['wm_color'] = $def["wm_color"];
$_POST['wm_font_size'] = $def["wm_font_size"];
$_POST['wm_setx'] = $def["wm_setx"];
$_POST['wm_sety'] = $def["wm_sety"];
$_POST['wm_rotate'] = $def["wm_rotate"];
$_POST['wm_alpha'] = $def["wm_alpha"];
start_form();
start_outer_table(TABLESTYLE);
table_section(1);
check_row(_("Turn Off Color Picker"), 'color_input', null, true);
$cp_toggle = isset($_POST['color_input']) ? intval($_POST['color_input']) : 0;
$Ajax->activate('_page_body');
if (!$cp_toggle) {
$typecolor = 'color';
$input_html_text = ' ';
} else {
$typecolor = 'text';
$input_html_text = '#000 - #fff OR #000000 - #ffffff';
}
end_outer_table(1);
start_outer_table(TABLESTYLE);
table_section(1);
table_section_title(_("Quick Settings"));
textarea_row(_("Watermark/Stamp Text:"), 'wm_text', $wm_text, 35, 3);
html_type_input_row_ex(_("Watermark/Stamp Colour:"), $typecolor, 'wm_color', 4, 7, '', '', $wm_color, null, $input_html_text);
table_section_title(_("Position & Visibility Settings"));
html_type_input_row_ex(_("Font Size:"), 'number', 'wm_font_size', 4, 96, '', '', $wm_font_size, null, _("pt (point): 4 - 96"));
html_type_input_row_ex(_("Start Point from Left:"), 'number', 'wm_setx', 1, 400, '', '', $wm_setx, null, _("pt (point): 1 - 400"));
html_type_input_row_ex(_("Start Point from Top:"), 'number', 'wm_sety', 200, 612, '', '', $wm_sety, null, _("pt (point): 200 - 612"));
html_type_input_row_ex(_("Rotation Angle:"), 'number', 'wm_rotate', 1, 360, '', '', $wm_rotate, null, _("Degrees: 1 - 360"));
html_type_input_row_ex(_("Transparency:"), 'number', 'wm_alpha', 0.05, 1, 0.05, '', $wm_alpha, null, _("Transparency: 0.05 - 1"));
if (isset($_POST['submit']) && can_process()) {
$wm_on = 1;
$wm_text = ($_POST['wm_text']);
$wm_color = ($_POST['wm_color']);
$wm_font_size = ($_POST['wm_font_size']);
$wm_setx = ($_POST['wm_setx']);
$wm_sety = ($_POST['wm_sety']);
$wm_rotate = ($_POST['wm_rotate']);
$wm_alpha = ($_POST['wm_alpha']);
$wm_array=array($wm_on, $wm_text, $wm_color, $wm_font_size, $wm_setx, $wm_sety, $wm_rotate, $wm_alpha);
$_SESSION['watermark']=$wm_array;
$Ajax->activate('_page_body');
display_notification(_("Watermark/Stamp has been added, now close window and print report."));
}
end_outer_table(1);
submit_center('submit', _("Add Watermark/Stamp"), true, '', 'default');
// submit_center('submit', _("Add Watermark/Stamp"), true, _("Add Watermark/Stamp"), 'selector'); // fixit does not work
// echo "<pre>";
// print_r ($_SESSION);
// echo "<pre>";
end_form(2);
end_page();
Notes:
1. I cannot get popup window to close by using the submit button; currently you have close window or press ‘Back’ after pressing submit button.
2. Previous settings are retained for session life, so will return to ‘default’ on next login .
3. Will work for ALL reports including extensions.
4. Existing header, header2 .php & .pdf if present still merge.
Demo here
username: demo
Password: password
Screenshots
Interface