Рейтинг:1

Add custom font to mpdf while using PDF using mPDF module

флаг jp

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.

Рейтинг:0
флаг jp

Using the method I tried didn't work because I added the font to the other mPDF object than used when generating the pdf. The trick is to add the fontdata and fontDir to the setting when the new Mpdf() is created in the ConvertToPdf.php file in the PDF using mPDF module. This can simply be done by adding the fontdata and fontDir to the $settings before calling $this->convertToPdf->convert($rendered_view, $settings);

So the final solution looks like this:

<?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)
  {
    $settings = $this->convertToPdf->getConfig();
    $settings["pdf_font_size"] = '10mm';
    $settings["default_font"] = 'aller';
    $settings["orientation"] = 'landscape';
    $settings["pdf_css_file"] = __DIR__ . "/../../css/style.css";
    
    //start adding custom font
    $settings["fontDir"] = __DIR__ . '/../../../../custom/views_pdf/fonts';
    $settings["fontdata"] = ['aller' => [
        'R' => 'aller_rg-webfont.ttf',
        'I' => 'aller_it-webfont.ttf',
    ]];
    //stop adding custom font
    
    $rendered_view = \Drupal::service('renderer')->render(views_embed_view($views_name, $display_id, $arg));
    $this->convertToPdf->convert($rendered_view, $settings);
    return true;
  }
}

Ответить или комментировать

Большинство людей не понимают, что склонность к познанию нового открывает путь к обучению и улучшает межличностные связи. В исследованиях Элисон, например, хотя люди могли точно вспомнить, сколько вопросов было задано в их разговорах, они не чувствовали интуитивно связи между вопросами и симпатиями. В четырех исследованиях, в которых участники сами участвовали в разговорах или читали стенограммы чужих разговоров, люди, как правило, не осознавали, что задаваемый вопрос повлияет — или повлиял — на уровень дружбы между собеседниками.