Coder Social home page Coder Social logo

Comments (14)

jamesfairhurst avatar jamesfairhurst commented on September 23, 2024 3

I've managed to get Font Awesome working by using composer to pull in the package "fortawesome/font-awesome": "*", defined a custom path when building the PDF:

define('_MPDF_SYSTEM_TTFONTS_CONFIG', FULL_PATH_TO_FILE);
$mpdf = new Mpdf();
...

Then included the font files in that file:

<?php

$this->fontdata['fontawesome'] = array(
    // relative to the mpdf fonts dir
    'R' => "/../../../fortawesome/font-awesome/fonts/fontawesome-webfont.ttf",
    'I' => "/../../../fortawesome/font-awesome/fonts/fontawesome-webfont.ttf",
);

Then finally in the html I can use:

<span class="fa fa-phone" style="font-family: fontawesome;">&#xf095;</span>
<span style="font-family: fontawesome;">&#xf095;</span>

Note the class isn't actually required, but useful for quickly knowing what icon it is. Hope that helps others whilst 7 is in development.

from mpdf.

marclaporte avatar marclaporte commented on September 23, 2024 1

A good use case for packages: #552

from mpdf.

marclaporte avatar marclaporte commented on September 23, 2024

Larger discussion about font handling should go here: https://github.com/mpdf/mpdf/wiki/Font-handling-in-mPDF

from mpdf.

mquadrat avatar mquadrat commented on September 23, 2024

AFAIK mpdf does not support the :before css selector which is needed to correctly display Bootstrap icons. So some preprocessing might be needed.

from mpdf.

marclaporte avatar marclaporte commented on September 23, 2024

Is this a lot of work?

from mpdf.

jonnybradley avatar jonnybradley commented on September 23, 2024

Looks like that's it, thanks for the clues! I managed to get some FA icons to appear in the mPDF output by using this html in the source page for the flag icon:

<span class="fa fa-fw ">&#xf024</span>

and this in config_fonts.php font data array:

'fontawesome' => array(
    'R' => 'fontawesome-webfont.ttf',
),

So, we (Tiki) could do some preprocessing on our pages to render the icons like this before sending the page to "print", but a solution in mPDF would obviously be preferable so it would work for everyone.

Thanks for getting the ball rolling on this @marclaporte

from mpdf.

garak avatar garak commented on September 23, 2024

The current way of handling font is ugly. This library should avoid shipping fonts and be flexible about font location, since we cant's put stuff in vendor directory by hand

from mpdf.

finwe avatar finwe commented on September 23, 2024

Yes, all this is planned for version 7.0 (currently unfortunately without roadplan). See https://github.com/mpdf/mpdf/blob/namespaces/src/Config/ConfigVariables.php#L410 (plus potential availability to list multiple sources).

from mpdf.

finwe avatar finwe commented on September 23, 2024

Anyway, @garak, you can set custom font path in <7.0 with _MPDF_TTFONTPATH constant.

from mpdf.

garak avatar garak commented on September 23, 2024

Also, setting variables with constants is ugly 😢
Hope to see a roadmap for 7.0 soon

from mpdf.

finwe avatar finwe commented on September 23, 2024

Moved font handling to separate issue #146

Roadmap for 7.0 can be seen at https://github.com/mpdf/mpdf/milestones/7.0 - with no due date, however, as I expect new issues will follow

from mpdf.

dimvic avatar dimvic commented on September 23, 2024

something in the lines of

$data = file_get_contents('path/to/fontawesome/variables.less');
$data = trim(implode("\n", array_map(function($v) { return preg_replace('/^(?:(?!fa-var).)*$/', '', $v); }, explode("\n", $data))));
$data = strtr($data, ['@fa-var'=>'fa', '\\'=>'&#x', '-'=>'_', '"'=>'']);
$data = preg_replace('/\s*:\s*/', ':', $data);
foreach (explode("\n", $data) as $line) {
    list($var, $val) = explode(':', $line);
    ${$var} = $val;
}

can help produce more readable templates

<span style="font-family: fontawesome;"><?php echo $fa_plus; ?></span>

from mpdf.

marclaporte avatar marclaporte commented on September 23, 2024

Since we'll no longer use the Github wiki, I am copying below the content from https://github.com/mpdf/mpdf/wiki/Font-handling-in-mPDF which was written by Daniel James


This is to brainstorm on how to improve font handling in mPDF

Related links

The mPDF font configuration needs to be more flexible so it
can take account of whichever fonts are available locally or via
@font-face. We might start this by creating configuration snippets for
popular fonts, which can be inserted into the mPDF configuration
automatically if the corresponding file names are present in the font path.

In general, mPDF should not bundle any fonts itself, as this makes
redistribution difficult (for example Debian or Ubuntu already ship some
of mPDF's fonts, but not others, and these distros don't want to ship
duplicates). It also means that the bundled fonts can get stale, which
is particularly an issue for icon fonts which are updated regularly with
new glyphs.

My suggestion would be to provide mPDF with scripts that can download
the required fonts at installation time, for example
https://packages.debian.org/jessie/fonts-font-awesome if your server
runs Debian. Most Western users do not need the CJK or Arabic fonts,
which are the biggest downloads.

from mpdf.

minhtkh avatar minhtkh commented on September 23, 2024

In my project, I have to stick with version 6 for now. To work around, I use @shark0der 's answer from here but modify a bit.
Instead of importing all library fonts, I define relative path to customised fonts from vendor/mpdf/mpdf/ttfonts/.

            $mpdf = new \mPDF('UTF-8');
            $fontdata['test'] = [
                // in this case it's resources folder in Laravel project
                'R' => '../../../../resources/assets/fonts/test.ttf',
                'I' => '../../../../resources/assets/fonts/test.ttf',
            ];
            foreach ($fontdata as $f => $fs) {
                $mpdf->fontdata[$f] = $fs;
                foreach (['R', 'B', 'I', 'BI'] as $style) {
                    if (isset($fs[$style]) && $fs[$style]) {
                        $mpdf->available_unifonts[] = $f . trim($style, 'R');
                    }
                }
            }
            $mpdf->default_available_fonts = $mpdf->available_unifonts;

I also don't need to define _MPDF_TTFONTPATH

from mpdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.