I made a custom module to create a PDF from a views page. This module depends on PDF using mPDF. I'm now trying to add a custom Google font. This should be the default font when generating a pdf. But I can't find out what I should do to have a custom font used in the PDF.
The mPDF documentation gives me an example of what I should do to load the custom font. I tried adding this in a custom function called right before generating the pdf but it doesn't work.
I both tried adding the font as default font to the setting (as shown in the example) or/and via css just using font-family: aller
, none of it worked till now.
So my question is, am I using the code snippet in the right way? Or what can I do to get this to work?
My code:
GenerateViewsPdf.php -> Controller
<?php
namespace Drupal\views_pdf\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\pdf_using_mpdf\Conversion\ConvertToPdf;
use Symfony\Component\DependencyInjection\ContainerInterface;
class GenerateViewsPdf extends ControllerBase
{
/**
* @var ConvertToPdf
*/
protected $convertToPdf;
/**
* Inject ConvertToPdf service
*
* GeneratePdf constructor.
* @param ConvertToPdf $convert
*/
public function __construct(ConvertToPdf $convert)
{
$this->convertToPdf = $convert;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container)
{
return new static(
$container->get('pdf_using_mpdf.conversion')
);
}
public function generateviews($arg = NULL, $views_name = NULL, $display_id = NULL)
{
addFont();
$settings = $this->convertToPdf->getConfig();
$settings["pdf_font_size"] = '10mm';
$settings["default_font"] = 'aller';
$settings["orientation"] = 'landscape';
$settings["pdf_css_file"] = __DIR__ . "/../../css/style.css";
$rendered_view = \Drupal::service('renderer')->render(views_embed_view($views_name, $display_id, $arg));
$this->convertToPdf->convert($rendered_view, $settings);
return true;
}
}
function addFont(){
$defaultConfig = (new \Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
$defaultFontConfig = (new \Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
$mpdf = new \Mpdf\Mpdf(
[
'tempDir' => __DIR__ . '/../../upload',
'fontDir' => array_merge($fontDirs, [
__DIR__ . '/../../fonts'
]),
'fontdata' => $fontData + [
'aller' => [
'R' => 'aller_rg-webfont.ttf',
'I' => 'aller_it-webfont.ttf',
],
],
'default_font' => 'aller'
]
);
}
——UPDATE——
I just tried adding the fontdata directly to the mpdf code in the vendor folder and this works perfectly so the font files work fine. And the idea of adding the fontdata is fine. But I don't know where to inject this code to make it work.