A customer wrote with a neato 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 PDF Ink 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 how long the plugin has been around!
Anyway, it was a good request and so we added a filter hook called ‘pdfink_woo_filter_settings‘. (This hook replaces the ‘wwpdf_filter_settings_array’ in WaterWoo PDF Premium, with identical arguments.)
Filtering Settings
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_settings_filter( $settings, $product_id, $file_path, $order, $download ) {
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['placement'][0]['x'] = 125; // 125mm from top of page
$settings['placement'][1]['y'] = 250; // 250 mm from top of page
$settings['placement'][0]['size'] = 24; // 24pt font size for 1st placement
$settings['placement'][1]['size'] = 12; // 12pt font size for 2nd placement
$settings['placement'][0]['content'] = 'Different watermark content in first placement for this specific PDF';
}
return $settings;
}
add_filter( 'pdfink_woo_filter_settings', 'custom_settings_filter', 10, 5 );
In this code we are filtering (adjusting) the settings array for a particular PDF. The settings are used during stamping/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 stamp page position, size, and content. If you want to change other settings, here is (most) the settings array for your convenience:
array(
'setup' => [
'override' => '',
'manipulate_this' => '',
'start_page' => 1,
'end_page' => 'last',
'pages' => 'all',
'custom' => '1',
'rtl' => 'off',
'margin_top_bottom' => 0,
'margin_left_right' => 0,
'tcpdf_font' => 'dejavusans',
'fpdf_font' => 'helvetica',
'setapdf_font' => 'helvetica',
],
'placement' => [
0 => [
'content' => '',
'x' => 0,
'y' => 0,
'opacity' => 1,
'color' => #000000,
'size' => 12,
'rotate' => 0,
'italic' => '', // 'on' - FPDF only
'bold' => '', // 'on' - FPDF only
'spacing' => null, // SetaPDF-Stamper only
'position' => null, // SetaPDF-Stamper only
'alignment' => null, // SetaPDF-Stamper only
'image' => [
'width' => 400,
'height' => 300,
'path' => 'absolute/path/to/image/file.png',
'link' => 'https://a.co.ol/url.com',
],
],
// continue with second index for another placement...
],
'security' => [
'fallback' => 'off',
'encrypt' => 'none',
'permissions' => [],
'pwd' => '',
'pwd_owner' => null,
'pwd_owner_original'=> null,
'unlock' => 'off',
],
)
Different Start Page and Password for Specific PDF
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 stamping and the password for a file called ‘my-file.pdf’. Anytime any file called ‘my-file.pdf’ is downloaded, the new settings will be used:
function my_alter_settings_function( $settings, $product_id, $file_path, $order, $download ) {
if ( false !== stripos( $file_path, 'my-file.pdf' ) ) { // strpos() would work fine too if you are confident of case
$settings['setup']['start_page'] = 2; // We'll start watermarking on page TWO
$settings['security']['pwd'] = '$eJ@4Gg$ApRz6RE!jgQPQwNzs';
}
return $settings;
}
add_filter( 'pdfink_woo_filter_settings', 'my_alter_settings_function', 10, 5 );
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).
Customer Phone Number as Password
Here’s an example for setting the PDF password as the WooCommerce user’s billing phone number. This assumes you are collecting phone numbers for every order, and that you will be telling your customers that their password is their phone number (in this case, digits only).
function custom_phone_password_function( $settings, $product_id, $file_path, $order, $download ) {
if ( isset( $settings['order_id'] ) ) { // Woo order ID - great for getting all sorts of data. Index 'order_id' also works with Download Monitor
// if ( isset( $settings['payment_id'] ) ) { // Use this instead if using Easy Digital Downloads
$order = wc_get_order( $order_id );
if ( ! is_a( $order, 'WC_Order' ) ) {
return $settings;
}
// Now with an order object, we can get a lot of order/customer data:
// https://stackoverflow.com/a/44708344
$order_data = $order->get_data();
if ( isset( $order_data['billing']['phone'] ) ) {
// Remove everything but digits from phone number
$settings['pwd'] = preg_replace('/D+/', '', $order_data['billing']['phone'] );
} else {
// You might want a plan B for if there is no phone number on record
}
}
return $settings;
}
add_filter( 'pdfink_woo_filter_settings', 'custom_phone_password_function', 10, 5 );
Wondering what all this is about? Check out PDF Ink!
Below are old/deprecated filters from WaterWoo for the reference of folks who may be migrating:
function custom_wwpdf_filter_settings_array( $settings, $product_id, $file_path, $order ) {
if ( false !== stripos( $file_path, 'file-we-seek.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 );
The following code and the code directly above will not work with PDF Ink. It is for reference of WaterWoo users. Here is the WaterWoo-style settings array for reference:
$settings = 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,
);
Finally one last WaterWoo example:
function my_wwpdf_filter_settings_array( $settings, $product_id, $file_path, $order ) {
if ( false !== stripos( $file_path, 'specific-file.pdf' ) ) {
$settings['start_page'] = 2; // We'll start watermarking on page TWO
$settings['pwd'] = 'Badpassword123456';
}
return $settings;
}
add_filter( 'wwpdf_filter_settings_array', 'my_wwpdf_filter_settings_array', 10, 4 );