Score: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.

Score:0
ธง jp

การใช้วิธีที่ฉันพยายามใช้ไม่ได้ผลเพราะฉันเพิ่มฟอนต์ไปยังออบเจกต์ mPDF อื่นที่ไม่ใช่ที่ใช้เมื่อสร้าง pdf เคล็ดลับคือการเพิ่ม ข้อมูลแบบอักษร และ แบบอักษรDir ไปที่การตั้งค่าเมื่อ ใหม่ Mpdf() ถูกสร้างขึ้นใน ConvertToPdf.php ไฟล์ใน PDF โดยใช้โมดูล mPDF ซึ่งสามารถทำได้โดยการเพิ่ม ข้อมูลแบบอักษร และ แบบอักษรDir ไปที่ การตั้งค่า $ ก่อนโทร $this->convertToPdf->convert($rendered_view, $settings);

ดังนั้นทางออกสุดท้ายจะเป็นดังนี้:

<?php

เนมสเปซ Drupal\views_pdf\Controller;

ใช้ Drupal\Core\Controller\ControllerBase;
ใช้ Drupal\pdf_using_mpdf\Conversion\ConvertToPdf;
ใช้ Symfony\Component\DependencyInjection\ContainerInterface;

คลาส GenerateViewsPdf ขยาย ControllerBase
{
  /**
   * @var ConvertToPdf
   */
  ป้องกัน $convertToPdf;

  /**
   * แทรกบริการ ConvertToPdf
   *
   * ตัวสร้าง GeneratePdf
   * @param ConvertToPdf $แปลง
   */
  ฟังก์ชั่นสาธารณะ __construct (ConvertToPdf $convert)
  {
    $this->convertToPdf = $แปลง;
  }

  /**
   * {@inheritdoc}
   */
  สร้างฟังก์ชันสแตติกสาธารณะ (ContainerInterface $container)
  {
    ส่งคืนค่าคงที่ใหม่ (
      $container->get('pdf_using_mpdf.conversion')
    );
  }

  ฟังก์ชันสาธารณะ createviews($arg = NULL, $views_name = NULL, $display_id = NULL)
  {
    $settings = $this->convertToPdf->getConfig();
    $settings["pdf_font_size"] = '10 มม.';
    $settings["default_font"] = 'ทั้งหมด';
    $settings["แนว"] = 'แนวนอน';
    $settings["pdf_css_file"] = __DIR__ "/../../css/style.css";
    
    // เริ่มเพิ่มแบบอักษรที่กำหนดเอง
    $settings["fontDir"] = __DIR__ '/../../../../custom/views_pdf/fonts';
    $settings["fontdata"] = ['aller' => [
        'R' => 'aller_rg-webfont.ttf',
        'ฉัน' => 'aller_it-webfont.ttf',
    ]];
    // หยุดเพิ่มแบบอักษรที่กำหนดเอง
    
    $rendered_view = \Drupal::service('renderer')->render(views_embed_view($views_name, $display_id, $arg));
    $this->convertToPdf->convert($rendered_view, $settings);
    กลับจริง;
  }
}

โพสต์คำตอบ

คนส่วนใหญ่ไม่เข้าใจว่าการถามคำถามมากมายจะปลดล็อกการเรียนรู้และปรับปรุงความสัมพันธ์ระหว่างบุคคล ตัวอย่างเช่น ในการศึกษาของ Alison แม้ว่าผู้คนจะจำได้อย่างแม่นยำว่ามีคำถามกี่ข้อที่ถูกถามในการสนทนา แต่พวกเขาไม่เข้าใจความเชื่อมโยงระหว่างคำถามและความชอบ จากการศึกษาทั้ง 4 เรื่องที่ผู้เข้าร่วมมีส่วนร่วมในการสนทนาด้วยตนเองหรืออ่านบันทึกการสนทนาของผู้อื่น ผู้คนมักไม่ตระหนักว่าการถามคำถามจะมีอิทธิพลหรือมีอิทธิพลต่อระดับมิตรภาพระหว่างผู้สนทนา