Comments (14)
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;"></span>
<span style="font-family: fontawesome;"></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.
A good use case for packages: #552
from mpdf.
Larger discussion about font handling should go here: https://github.com/mpdf/mpdf/wiki/Font-handling-in-mPDF
from mpdf.
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.
Is this a lot of work?
from mpdf.
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 "></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.
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.
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.
Anyway, @garak, you can set custom font path in <7.0
with _MPDF_TTFONTPATH
constant.
from mpdf.
Also, setting variables with constants is ugly 😢
Hope to see a roadmap for 7.0 soon
from mpdf.
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.
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.
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.
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)
- htmlpagefooter element does not respect z-index HOT 4
- Error in pdf generation - Invalid argument supplied for foreach()
- Document structure is invalid or damaged HOT 6
- Getting Fatal error sometimes with the ColorConverter HOT 2
- Attribute value truncated when containing double quote character and using single quote delimiter
- Warning: Undefined array key "s" HOT 6
- Nesting a table inside a tfoot element results in "Trying to access array offset on null"
- PDF/A : endobj is missing EOL-Marker
- ArrayAccess::offsetSet()" might add "void" as a native return type
- Error while trying to save pdf to a folder. HOT 1
- Write HTML - NOTICE Uninitialized string offset - XXXX on Otl.php HOT 1
- Subsequent calls to Output() of pdf with images are missing images in 2++ data HOT 1
- SELECT don't show selected element HOT 6
- Signed PDF doesn't show signature
- Font size in absolute positioned div depends on position on page
- MPDF "Trying to access array offset on null" error during pdf generation
- Documentation Update Needed for Margin Property Syntax Change in mPDF Version 8 HOT 1
- Bug Report HOT 1
- Set margin-left and margin-right in @page :first { ... }
- text-align: justify add spases between letters not words
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mpdf.