-
Notifications
You must be signed in to change notification settings - Fork 6
Add custom fonts to PDF
David Jensen edited this page May 23, 2024
·
1 revision
Below is a code snippet to add a custom font called jaro. The font files (.ttf) would be stored in the child-theme/assets/fonts
directory, but it can be customized based on your use case.
This code would be placed in your child theme functions.php file or similar.
add_filter(
'wc_cart_pdf_mpdf',
function ( $mpdf ) {
$font_name = 'jaro'; // Name of the font.
$mpdf->AddFontDirectory( get_stylesheet_directory() . '/assets/fonts' ); // Directory where the .ttf files are stored.
$mpdf->fontdata[ $font_name ] = array(
'R' => 'Jaro-Regular.ttf', // Regular.
'I' => 'Jaro-Italic.ttf', // Italic.
'B' => 'Jaro-Bold.ttf', // Bold.
'BI' => 'Jaro-BoldItalic.ttf', // Bold Italic.
);
foreach ( $mpdf->fontdata as $f => $fs ) {
if ( isset( $fs['R'] ) && $fs['R'] ) {
$mpdf->available_unifonts[] = $f;
}
if ( isset( $fs['B'] ) && $fs['B'] ) {
$mpdf->available_unifonts[] = $f . 'B';
}
if ( isset( $fs['I'] ) && $fs['I'] ) {
$mpdf->available_unifonts[] = $f . 'I';
}
if ( isset( $fs['BI'] ) && $fs['BI'] ) {
$mpdf->available_unifonts[] = $f . 'BI';
}
}
$mpdf->SetDefaultFont( $font_name );
$mpdf->AddFont( $font_name );
$mpdf->SetFont( $font_name );
return $mpdf;
}
);