Unique Watermark/Password For Each WooCommerce Product PDF
A PDF Watermark for WooCommerce (WaterWoo Premium) customer wrote with a request last month. If one WooCommerce product had several PDFs, and wasn’t a variable product, how could each PDF in the list be watermarked differently from the others? There is no setting for that inside WaterWoo because WooCommerce hasn’t exactly made that function easily possible, and also because it somehow hadn’t been requested until last month. That’s sort of amazing considering the plugin has been out for about 9 years!
Anyway, it was a good request and so I added a filter hook to WaterWoo version 2.6.2 called ‘wwpdf_filter_settings_array‘. Because WooCommerce — unlike Easy Digital Downloads — doesn’t give a way to tell between each of the uploaded PDFs in a simple product, we’ll have to use the PDF file name to distinguish between them. That shouldn’t be a problem, right? Here’s some example code:
function custom_wwpdf_filter_settings_array( $settings, $product_id, $file_path, $order ) {
if ( false !== stripos( $file_path, 'myUniqueFilename.pdf' ) ) { // case-insensitive match
// Set different watermark position for this specific PDF (in a multi-file WooCommerce simple product)
$settings['o_finetune_Y'] = 125; // 125mm from top of page
$settings['f_finetune_Y'] = 250; // 250 mm from top of page
$settings['o_size'] = 24; // 24pt font size for overlay
$settings['f_size'] = 12; // 12pt font size for footer
$settings['o_input'] = 'Different watermark content for this specific PDF';
}
return $settings;
}
add_filter( 'wwpdf_filter_settings_array', 'custom_wwpdf_filter_settings_array', 10, 4 );
In this code we are filtering (adjusting) the settings array for a particular PDF. The settings are used during watermarking/passwording to achieve your desired result, and so if your desired result changes, your settings need to change, too. In the code we are looking for a particular PDF, and adjusting the watermark page position, size, and content. If you want to change other settings, here is the settings array for your convenience:
array(
'rtl' => $rtl,
'start_page' => (int) $start,
'end_page' => $end,
'watermark_pages' => $wm_pgs,
'margin_t_b' => (int) $marg_t_b,
'margin_l_r' => (int) $marg_l_r,
'font' => $font,
'o_input' => $o,
'o_size' => (int) $o_size,
'o_color' => $o_color,
'o_rotate' => (int) $o_rotate,
'o_finetune_X' => (int) $o_tune_X,
'o_finetune_Y' => (int) $o_tune_Y,
'f_input' => $f,
'f_size' => (int) $f_size,
'f_color' => $f_color,
'f_finetune_X' => (int) $f_tune_X,
'f_finetune_Y' => (int) $f_tune_Y,
'encryption' => intval( $encrypt ),
'pwd' => $pwd,
'pwd_owner' => $pwd_owner,
'unlock' => $unlock,
'disable_print' => $dis_print,
'disable_print_high' => $dis_print_high,
'disable_mods' => $dis_mod,
'disable_ass' => $dis_ass,
'disable_copy' => $dis_copy,
'disable_annot' => $dis_annot,
'disable_fill_forms' => $dis_forms,
'disable_extract' => $dis_extract,
)
OK, so taking it a bit further, and hopefully to help you understand, we can create a another different PHP function which will adjust the start page for watermarking and the password for a file called ‘UpCycling.PDF’. Anytime any file called ‘UpCycling.PDF’ is downloaded, the new settings will be used:
function my_wwpdf_filter_settings_array( $settings, $product_id, $file_path, $order ) {
if ( false !== stripos( $file_path, 'UpCycling.PDF' ) ) {
$settings['start_page'] = 2; // We'll start watermarking on page TWO
$settings['pwd'] = '$eJ@4tg$Aphtz6RE!jgQPQzNjs';
}
return $settings;
}
add_filter( 'wwpdf_filter_settings_array', 'my_wwpdf_filter_settings_array', 10, 4 );
Can you see how we reset the values in the $settings array for ‘start_page’ and ‘pwd’? That’s how we make the settings changes programmatically, and they only apply to the ‘UpCycling.PDF’ file! Hopefully that helps this make sense for you. When you think you have your PHP function code ready, it would be cut and pasted into a child theme functions.php file (or inserted using the Code Snippets plugin).
Wondering what all this is about? Check out the WaterWoo plugin
(advertisement)