Custom Fonts with WooStamper (SetaStamper)

The SetaPDF library which WooStamper is built on allows quite a bit of font customization to be done. More on SetaPDF and font manipulations. For the meantime, a quick tutorial on how to get other fonts beyond the three packaged with the plugin (Times, Courier, and Helvetica).

Start by finding a TrueType font (.ttf) file which suits your needs. Try to keep the file size small, subsetting if necessary so that your server isn’t bogged down managing a large font file while trying to manipulate your PDF.

Upload the file to your wp-content/uploads directory, somewhere it won’t get overwritten. It makes sense to use the woostamper folder, so we’ve gone ahead and done that below. Arimo is a TrueType font which works for our purposes, so we have uploaded it to a folder “arimo.” Look at the function below and see how we have set up the $path varible. You will need to customize that to suit your file path; otherwise the function should work as-is.

function my_woostamper_custom_font( $font, $document ) {

	// Path to a working TTF font somewhere in your WP directory
        $path = WP_CONTENT_DIR . '/uploads/woostamper/fonts/arimo/Arimo-Regular.ttf';
	
	if ( is_file( $path ) ) { // quick check to make sure path is correct
            try {
                $newfont = SetaPDF_Core_Font_TrueType::create( $document, $path, 'WinAnsiEncoding', 'auto');
	        return $newfont;
            } catch( Exception $e ) {
                woostamper_debug( 'An error occured while trying to add a custom font. ' . $e->getMessage() );
            }
	} else {
            woostamper_debug( 'The path to your font file doesn\'t seem correct. It is: ' . $path );
	}
        return $font;

}
add_filter ( 'woostamper_filter_font', 'my_woostamper_custom_font', 10, 2 );

This piece of code can be added to your child theme functions.php file. If you aren’t comfortable doing that, you can use the Code Snippets Wordpress plugin instead.

We cannot provide support to you for managing font files, sourcing, licensing, etc. Using action and filter hooks WooStamper comes with, you should be able to get what you need while geeking out on fonts. If you see room for improvement in the plugin or its hooks, please get in touch!

The same code will work for the EDDiStamper plugin. Just change ‘woostamper_custom_font’ to ‘eddistamper_custom_font’ and change ‘woostamper_filter_font’ to ‘eddistamper_filter_font’!