Adding Custom Fonts to Stamper

All three new Stamper plugins (EDDiStamper, WooStamper, and Download Monitor Stamper) come with the three standard PDF fonts: Helvetica, Times New Roman, and Courier. Not much to get excited about, I know. Whereas EDDiMark and WaterWoo (plugins based on TCPDF, not SetaPDF-Stamper) have built-in font uploaders with more instant gratification, the integration with Seta is a bit more complicated, and so you’ll need to use a little code. Whether you add the code into a custom plugin, your child theme functions.php file, or by using the more plug-n-play Code Snippets plugin doesn’t matter.

Before using the following example code, we have started by creating a “fonts” folder inside the wp-content/uploads/eddistamper/ folder. We put our Comic Sans (yes!) TTF file inside the fonts folder. You can see that path reflected in line 6 of the code below.

So, put the TTF file of your choosing in a folder and put the path to that TTF in line 6. Great. But the font must have TrueType outlines and it really should be licensed for use. (You’ll see below in the create() method, line 6, the final TRUE argument says you have permission to use the font. Do you really have permission?)

If this hooked function fails, it will send a report to your Easy Digital Downloads debug log, so you can see what went wrong.

function custom_add_my_own_font( $font, $document ) {

    $existing_font = $font;
    if ( function_exists( 'setapdf_autoload' ) ) {
        try {
            $font = SetaPDF_Core_Font_TrueType::create($document, WP_CONTENT_DIR . '/uploads/eddistamper/fonts/comicsans.ttf', 'WinAnsiEncoding', 'auto', false, true );
        } catch( Exception $e ) {
            edd_debug_log( 'An error occured while trying to add a custom font. ' . $e->getMessage() );
            return $existing_font;
        }
    }
    return $font;

}

add_filter( 'eddistamper_filter_font', 'custom_add_my_own_font', 10, 2 ); // use for EDDiStamper
// add_filter( 'woostamper_filter_font', 'custom_add_my_own_font', 10, 2 ); // use for WooStamper
// add_filter( 'dlmstamper_filter_font', 'custom_add_my_own_font', 10, 2 ); // use for Download Monitor Stamper 

This example is written for EDDistamper. To use with WooStamper or Download Monitor Stamper, you will want to use the correct line starting with “add_filter” (lines 16-18). You’ll know which one after reading them, just delete the other two.

We cannot provide support to you for managing font files, sourcing, licensing, etc. The debug log is there to help you sort out issues you may encounter. However, if you have a quick question about this, please get in touch.

If you want to go deeper, learn more about the SetaPDF_Core_Font_TrueType class here.

via GIPHY