Skip to content

Commit

Permalink
feat: add custom font support
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasravnsborg committed Apr 23, 2016
1 parent d4205ce commit b28395a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"php": ">=5.4.0",
"mpdf/mpdf": "^6.0"
"mpdf/mpdf": "^6.1.0-beta"
},
"autoload": {
"psr-4": {"niklasravnsborg\\LaravelPdf\\": "src/LaravelPdf"}
Expand Down
27 changes: 27 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,33 @@ Now you just need to define them with the name attribute in your CSS:

Inside of headers and footers `{PAGENO}` can be used to display the page number.

## Included Fonts

By default you can use all the fonts [shipped with mPDF](https://mpdf.github.io/fonts-languages/available-fonts-v6.html).

## Custom Fonts

With `custom_font_data` in your configuration file (`/config/pdf.php`) you can use your own fonts in the generated PDFs. The TTF files have to be located in one folder, e.g. `/resources/fonts/`.

```
'custom_font_path' => base_path('/resources/fonts/'), // don't forget the trailing slash!
'custom_font_data' => [
'examplefont' => [
'R' => 'ExampleFont-Regular.ttf', // regular font
'B' => 'ExampleFont-Bold.ttf', // optional: bold font
'I' => 'ExampleFont-Italic.ttf', // optional: italic font
'BI' => 'ExampleFont-Bold-Italic.ttf' // optional: bold-italic font
]
// ...add as many as you want.
]
```

Now you can use the font in CSS:

```
font-family: 'examplefont', sans-serif;
```

## License

Laravel PDF is open-sourced software licensed under the [MIT license](http:https://opensource.org/licenses/MIT)
4 changes: 4 additions & 0 deletions src/LaravelPdf/PdfServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function register() {

$this->app->bind('mpdf.wrapper', function($app, $cfg) {

if (Config::has('pdf.custom_font_path') && Config::has('pdf.custom_font_data')) {
define(_MPDF_SYSTEM_TTFONTS_CONFIG, __DIR__ . '/../mpdf_ttfonts_config.php');
}

$mpdf = new \mPDF(
Config::get('pdf.mode'), // mode - default ''
Config::get('pdf.format'), // format - A4, for example, default ''
Expand Down
5 changes: 5 additions & 0 deletions src/mpdf_ttfonts_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use Illuminate\Support\Facades\Config;
define('_MPDF_SYSTEM_TTFONTS', Config::get('pdf.custom_font_path'));
$this->fontdata = array_merge($this->fontdata, Config::get('pdf.custom_font_data'));

0 comments on commit b28395a

Please sign in to comment.