Allow custom PDF watermark text

December 11, 2018
Joost de Keijzer

I’d like to add a “Draft” watermark to the pdf invoices.

The html variant uses the `sliced_get_invoice_watermark` function which has a filter to add extra watermark text.

The PDF does not use this function and has no option to add a custom watermark text.

Please also use `sliced_get_invoice_watermark` for the PDF watermark text, eg:

“`
public static function set_watermark( $id = 0, $mpdf ) {

if( $text = sliced_get_invoice_watermark( $id ) ) {
//watermarks
$mpdf->SetWatermarkText( $text );
$mpdf->showWatermarkText = true;
$mpdf->watermark_font = ‘Helvetica’;
$mpdf->watermarkTextAlpha = 0.07;
$mpdf->SetDisplayMode(‘fullpage’);
}

return $mpdf;

}
“`

2 Comments. Leave new

Sliced Invoices
December 11, 2018 8:07 pm

This is not necessary. You can use the hook ‘sliced_pdf_init’ which passes the $mpdf object as it’s only argument. Example:

add_action( 'sliced_pdf_init', 'sliced_pdf_html_custom_params', 10, 1 );
function sliced_pdf_html_custom_params( $mpdf ) {
	$mpdf->SetWatermarkText( 'draft' );
}
Joost de Keijzer
May 5, 2023 11:56 pm

Hi,

Thanks for the suggestion, it does become a bit more complex. My solution now is:

function sliced_pdf_html_custom_params( $mpdf ) {
$id = $_GET[‘id’] ?? 0;

if ( ! $id ) {
return;
}

if ( ! has_term( ‘paid’, ‘invoice_status’, $id ) && ! has_term( ‘cancelled’, ‘invoice_status’, $id ) && ( $text = sliced_get_invoice_watermark( $id ) ) ) {
$mpdf->SetWatermarkText( $text );
$mpdf->showWatermarkText = true;
$mpdf->watermark_font = ‘Helvetica’;
$mpdf->watermarkTextAlpha = 0.07;
$mpdf->SetDisplayMode(‘fullpage’);
}
}

Leave a Reply

Your email address will not be published.