• Stars
    star
    2
  • Language
    PHP
  • License
    MIT License
  • Created over 1 year ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A symfony bundle to easily load custom fonts for dompdf (on cache warming)

Font loader for dompdf Bundle

This bundle provides a simple way to load/install fonts of your choice for the dompdf library in a symfony project. This bundle offers functionality to load font families programmatically or automatically on cache warmup. You can specify each font family with a name and a path to the font files or you let the bundle autodiscover all fonts in a directory.

Feautures

  • Services for easy installation of font families
  • Configure font families in your config files, which will be loaded automatically on cache warmup
  • Autodiscover all fonts in configured directories

Requirements

  • Symfony 6
  • PHP 8.1 or later

Installation

  1. Install the bundle composer require jbtronics/dompdf-font-loader-bundle
  2. Enable the bundle in your config/bundles.php (normally done by Symfony flex automatically)
  3. Put the font TTF files you want to use in a directory the webserver can access (preferably somewhere in your project folder)
  4. Add a config file config/packages/jbtronics_dompdf_font_loader.yaml, with the content described below (and changed according to your need)

Configuration

dompdf_font_loader:
  
  # Set this to true to enable the automatic font loading on cache warmup, without it you have to load the fonts
  # manually via the ConfiguredFontsInstaller service
  auto_install: true

  # You can specify font families here manually
  fonts:
    my_font: # The name of the font family (used to access it in dompdf later)
      # A font family consists of up to four font files (normal, bold, italic, bold_italic)
      normal: "%kernel.project_dir%/assets/fonts/my_font.ttf"
      bold: "%kernel.project_dir%/assets/fonts/my_font_bold.ttf"
      italic: "%kernel.project_dir%/assets/fonts/my_font_italic.ttf"
      bold_italic: "%kernel.project_dir%/assets/fonts/my_font_bold_italic.ttf"
      
    # But only the normal font file is required, the others can be omitted
    unifont:
      normal: "%kernel.project_dir%/assets/fonts/unifont.ttf"

  # Autodiscover allows you to specify directories, where all fonts will be loaded automatically
  autodiscovery:
    # Each of this directory will be scanned for font files 
    paths:
      - "%kernel.project_dir%/assets/fonts"
      - "%kernel.project_dir%/vendor/fonts/package/ttfs"
    exclude_patterns:
      # You can exclude certain patterns from the autodiscovery if you want
       - "exclude_this_font.ttf"

The fonts and autodiscovery keys are both optional, but at least one of them is required to load fonts.

Usage

When you have enabled the auto_install option, you do not have to do anything else, the fonts will be loaded automatically on cache warmup (when php bin/console cache:clear is run). The bundle will copy the font files to the dompdf font directory, create font metrics and register them in the dompdf library.

Autodiscovery

The autodiscovery mechanism will scan the configured directories for TTF files and register them as font families with the name of the font file. It also tries to detect the type of the font based on a suffix: _bold or _b will be detected as bold fonts, _italic, _i as italic fonts, and _bold_italic or _bi as bold italic. So the my_font_bold.ttf will be registered as bold font of the my_font family and so on, while my_font.ttf will be registered as normal font of the my_font family.

In principle dompdf should be able to use OTF files as well, however in my tests it did not work, so autodiscovery only detects TTF files by default. You can change the detected file types via the autodiscovery.file_pattern option.

Specify DOMPDF font location

Dompdf has its own font directory, where it stores the font files and metrics. This is configured on a per instance basis on a dompdf object with the set_option('fontDir', $path) method. To specify the font directory for the dompdf instance used by this bundle, you must decorate the DompdfFactoryInterface and configure the object in the create() method:

#[AsDecorator(decorates: DompdfFactoryInterface::class)]
class MyDompdfFactory implements DompdfFactoryInterface
{
    public function create(): Dompdf
    {
        return new Dompdf(['fontDir' => '%kernel.project_dir%/var/dompdf/fonts']);
    }
}

Manual font loading/installation

This bundle offers the DompdfFontLoader service, which can be used to install font families manually. You can either install a single font family with the installFontFamily() method or install all found fonts in a folder with the autodiscoverAndInstallFonts() method.

License

This bundle is licensed under the MIT license. See LICENSE for details.

Credits

More Repositories

1

CrookedStyleSheets

Webpage tracking only using CSS (and no JS)
CSS
3,288
star
2

UE4-CheatSheet

An Cheat Sheet for Unreal Engine 4 C++ programming, licensed under CC BY-NC-SA 4.0
TeX
431
star
3

ESP32Console

Extensible UART console library for ESP32 with useful included commands.
C++
152
star
4

settings-bundle

A bundle to easily create typesafe, user-configurable settings for symfony applications
PHP
73
star
5

PCBruler

A PCB Ruler created in KiCAD
66
star
6

DS1054_BodePlotter

A Python program that plots Bode diagrams of a component using a Rigol DS1054Z and a JDS6600
Python
34
star
7

SDBatchToolsGUI

A Gui for the Substance Designer Tools
C#
24
star
8

WireDebugger

Debug AVRs with DebugWire via SerialPort
C#
21
star
9

AVR-ZIF-Programmer

An AVR Programmer based on USBasp which uses a ZIF-Socket for Programming.
KiCad Layout
10
star
10

2fa-webauthn

Webauthn Two-Factor-Authentictication Plugin for scheb 2fa
PHP
8
star
11

bs-treeview

TreeView element for browsers without any dependencies
TypeScript
7
star
12

FM1216duino

An Arduino Library to controlo FM1216-Tuner over I2C (only Soft-I2C)
C
7
star
13

FTController

A Motor controller for Fischertechnik with many cool features.
HTML
5
star
14

Source2Strings

A repository containing extracted strings from existing Source2 games
Shell
5
star
15

Recolldroid

A android app for comfortable use of Recoll-WebUI on android.
Java
4
star
16

ESP07-Breakout

The KiCAD Project files for my ESP07-Breakout
KiCad Layout
4
star
17

Part-DB-android

Part-DB Barcode Scanner for Android
Java
3
star
18

StuRa-Finanzsoftware

A little web project to submit PaymentOrders via a web formula and manage them in a backend. Developed for the student council of the University of Jena.
PHP
3
star
19

GolonkaBBCodeParser

A copy of Golonka\BBCode after original repo got deleted (it's a dependency for Part-DB)
PHP
2
star
20

Portal2HQMaterials

High Quality remake Materials for Portal 2
2
star
21

FRMorp

A tool for dumping firmware from SPMP8000 devices originally written by openschemes.com
C
1
star
22

WiFI-Sinus-Gen

The ESP8266 Code for my WLAN DDS Sinus Generator
Arduino
1
star
23

WiFi-Sinus-Generator-android

The Repository for the WiFi-Sinus-Generator Android App
Java
1
star