Load custom fonts with InvoicePlane 1.5.2

Hi, I’m using InvoicePlane 1.5.2, that uses mPDF 7 installed via composer, so I would like to add custom fonts outside of the vendor folder. I’ve added custom fonts to the ‘fontdata’ array and changed ‘fontDir’ with a new path where to put all ttfonts, but neither ‘fontDir’ nor ‘fontdata’ work as expected. I inspected ‘$mpdf’ object with var_dump after changes, but both ‘fontdata’ and ‘fontDir’ were correctly changed. Why doesn’t work as expected?

This is the file (mpdf_helper.php, starting from line 39) where I add this snippet:

<?php

$mpdf = new \Mpdf\Mpdf();

$mpdf->fontdata["source-sans-pro-light"] = [
  'R' => "SourceSansPro-ExtraLight.ttf",
  'B' => "SourceSansPro-Light.ttf",
];
$mpdf->fontdata["source-sans-pro-regular"] = [
  'R' => "SourceSansPro-Regular.ttf",
];
$mpdf->fontdata["source-sans-pro-bold"] = [
  'R' => "SourceSansPro-SemiBold.ttf",
  'B' => "SourceSansPro-Bold.ttf",
];

$mpdf->fontDir = FCPATH . 'uploads/temp/mpdf/ttfonts';

var_dump($mpdf);

?>

P.S.
It works fine if I add custom fonts inside the vendor/mpdf folder.

Thank you.

I suppose that you already took a look at the mPDF docs?
If yes, there’s nothing more I could tell you and you may ask the mPDF guys directly. the mPDF helper does nothing special other than loading the library, adding config and returning the result so I would think it’s either a configuration problem or a mPDF bug.

By the way, IP uses mPDF 6, not 7. Maybe woth the mention and is not up-to-date with the main version due to features that we needed but weren’t included in an earlier release.

Hi @Kovah, yes I’ve already taken a look at the mPDF docs and I’ve also already opened an issue in the mPDF GitHub repo.

The documentation only refers to version 5 and 6.

But, are you sure that IP uses mPDF 6 and not 7? When I download IP 1.5.2 from GitHub, in the composer.json the mPDF dependency is "mpdf/mpdf": "dev-development" and the latest entries in the CHANGELOG.md inside the vendor/mpdf/mpdf folder are these:

mPDF 7.0.0
===========================

### 30/06/2016

Backward incompatible changes
-----------------------------

- PHP `^5.6 || ~7.0.0 || ~7.1.0` is required.
- Entire project moved under `Mpdf` namespace
    - Practically all classes renamed to use `PascalCase` and named to be more verbose
    - Changed directory structure to comply to `PSR-4`
- Removed explicit require calls, replaced with Composer autoloading
- Removed configuration files
    - All configuration now done via `__construct` parameter (see below)
- Changed `\Mpdf\Mpdf` constructor signature
    - Class now accepts only single array `$config` parameter
    - Array keys are former `config.php` and `config_fonts.php` properties
    - Additionally, former constructor parameters can be used as keys
- Removed progressbar support
- Removed JpGraph support
- Moved examples to separate repository
- Removed `error_reporting` changes
- Removed timezone changes
- Removed `compress.php` utility
- Removed `_MPDF_PATH` and `_MPDF_URI` constants
- Removed `_MPDF_TEMP_PATH` constant in favor of `tempDir` configuration variable
- Removed `_MPDF_TTFONTDATAPATH` in  favor of `tempDir` configuration variable
- Removed `_MPDFK` constant in favor of `\Mpdf\Mpdf::SCALE` class constant
- tempDir directory now must be writable, otherwise an exception is thrown
- ICC profile is loaded as entire path to file (to prevent a need to write inside vendor directory)
- Removed formerly deprecated methods
- Moved TextVars constants to separate class
- Moved border constants to separate class
- `scriptToLang` and `langToFont` in separate static class methods
- removed `FONT_DESCRIPTOR` constant in favor of `fontDescriptor` configuration variable
- Removed `_MPDF_SYSTEM_TTFONTS` constant in favor of `fontDir` configuration variable with multiple paths
- Removed HTML output of error messages and debugs
- Will now throw an exception when `mbstring.func_overload` is set
- Moved Glyph operator `GF_` constants in separate `\Mpdf\Fonts\GlyphOperator` class
- All methods in Barcode class renamed to camelCase including public `dec_to_hex` and `hex_to_dec`
- Decimal conversion methods (to roman, cjk, etc.) were moved to classes in `\Mpdf\Conversion` namespace
- Images in PHP variables (`<img src="var:smileyface">`) were moved from direct Mpdf properties to `Mpdf::$imageVars` public property array 

Fixes and code enhancements
----------------------------

- Fixed joining arab letters
- Fixed redeclared `unicode_hex` function
- Converted arrays to short syntax


New features
------------

- Refactored caching (custom `Cache` and `FontCache` classes)
- Implemented `Psr\Log\LoggerAware` interface
    - All debug and additional messages are now sent to the logger
    - Messages can be filtered based on `\Mpdf\Log\Context` class constants
- `FontFileFinder` class allowing to specify multiple paths to search for fonts
- `MpdfException` now extends `ErrorException` to allow specifying place in code where error occured
- Generating font metrics moved to separate class
- Added `\Mpdf\Output\Destination` class with verbose output destination constants
- Availability to set custom default CSS file
- Availability to set custom hyphenation dictionary file
- Refactored code portions to new "separate" classes:
    - `Mpdf\Color\ColorConvertor`
    - `Mpdf\SizeConvertor`
    - `Mpdf\Hyphenator`
    - `Mpdf\Image\ImageProcessor`
    - `Mpdf\Image\ImageTypeGuesser`
    - `Mpdf\Conversion\*` classes

Git repository enhancements
---------------------------

- Added contributing guidelines
- Added Issue template

I’ve also tried to use the latest mPDF 6 stable version and when I download it in the vendor folder, IP gives errors about mPDF main class that was reported as a breaking feature in mPDF 7.

Thank you!

We are using the dev branch of the https://github.com/InvoicePlane/mpdf.git repo as specified in the composer.json file.
I know this is quite confusing but Composer loads mPDf from the InvoicePlane fork, not from mPDF itself. :thinking:

Okay, I understand, but if I change this "mpdf/mpdf": "dev-development" into "mpdf/mpdf": "6.1.3", then IP has to work in the same way, right?

What is confusing to me it’s not the way or from where IP loads mPDF, but why if I use mPDF 6.1.3 declaring it in the composer.json (and obviously rewriting composer.lock), IP returns me errors about mPDF main class.

I don’t want to annoy you, I just want to figure out where I’m wrong. :slight_smile:

Thank you in advance.

No, because this would load the original mPDf that is missing features we currently need. This may be the cause for the errors you receive.

Okay, thank you, now it’s all clear.

So it’s the InvoicePlane/mpdf repo that needs to be updated. For me it’s too complex and needs too much time to create a fork of mPDF with InvoicePlane fixes and font loading fixes, so I’ll wait (and hope) for a new version.

Thank you a lot for the help! :slight_smile:

Maybe this fix could help to solve this problem? :slight_smile: