Skip to content

Commit

Permalink
Merge pull request #2 from horstoeko/Issue_1
Browse files Browse the repository at this point in the history
Added Functionality for adding user-defined fonts
  • Loading branch information
horstoeko authored Jun 17, 2024
2 parents e0ced57 + 3360a18 commit 3a53ebf
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 16 deletions.
13 changes: 10 additions & 3 deletions .github/workflows/build.php73.ant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ on:
- '**.md'
- '**.tmpl'
- '.github/**'
- 'examples/**'
pull_request:
types:
- opened
branches:
- 'master'
workflow_dispatch:

jobs:
build:
permissions: write-all
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup PHP with PECL extension
uses: shivammathur/setup-php@v2
with:
Expand All @@ -27,7 +34,7 @@ jobs:
- name: Build with Ant
run: ant -noinput -buildfile build.ant.xml
- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v1
if: always()
uses: EnricoMi/publish-unit-test-result-action@v2
if: github.event_name != 'pull_request'
with:
files: "./build/logs/junit.xml"
13 changes: 10 additions & 3 deletions .github/workflows/build.php74.ant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ on:
- '**.md'
- '**.tmpl'
- '.github/**'
- 'examples/**'
pull_request:
types:
- opened
branches:
- 'master'
workflow_dispatch:

jobs:
build:
permissions: write-all
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup PHP with PECL extension
uses: shivammathur/setup-php@v2
with:
Expand All @@ -27,7 +34,7 @@ jobs:
- name: Build with Ant
run: ant -noinput -buildfile build.ant.xml
- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v1
if: always()
uses: EnricoMi/publish-unit-test-result-action@v2
if: github.event_name != 'pull_request'
with:
files: "./build/logs/junit.xml"
13 changes: 10 additions & 3 deletions .github/workflows/build.php80.ant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ on:
- '**.md'
- '**.tmpl'
- '.github/**'
- 'examples/**'
pull_request:
types:
- opened
branches:
- 'master'
workflow_dispatch:

jobs:
build:
permissions: write-all
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup PHP with PECL extension
uses: shivammathur/setup-php@v2
with:
Expand All @@ -27,7 +34,7 @@ jobs:
- name: Build with Ant
run: ant -noinput -buildfile build.ant.xml
- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v1
if: always()
uses: EnricoMi/publish-unit-test-result-action@v2
if: github.event_name != 'pull_request'
with:
files: "./build/logs/junit.xml"
13 changes: 10 additions & 3 deletions .github/workflows/build.php81.ant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ on:
- '**.md'
- '**.tmpl'
- '.github/**'
- 'examples/**'
pull_request:
types:
- opened
branches:
- 'master'
workflow_dispatch:

jobs:
build:
permissions: write-all
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup PHP with PECL extension
uses: shivammathur/setup-php@v2
with:
Expand All @@ -27,7 +34,7 @@ jobs:
- name: Build with Ant
run: ant -noinput -buildfile build.ant.xml
- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v1
if: always()
uses: EnricoMi/publish-unit-test-result-action@v2
if: github.event_name != 'pull_request'
with:
files: "./build/logs/junit.xml"
5 changes: 3 additions & 2 deletions .github/workflows/build.php81.release.ant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ on:
- '**.md'
- '**.tmpl'
- '.github/**'
- 'examples/**'

jobs:
build:
permissions: write-all
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup PHP with PECL extension
uses: shivammathur/setup-php@v2
with:
Expand All @@ -25,7 +26,7 @@ jobs:
- name: Build with Ant
run: ant -noinput -buildfile build.ant.xml
- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v1
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
files: "./build/logs/junit.xml"
Expand Down
40 changes: 40 additions & 0 deletions .github/workflows/build.php82.ant.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CI (Ant, PHP 8.2)

on:
push:
tags-ignore:
- '**'
branches:
- '**'
paths-ignore:
- '**.md'
- '**.tmpl'
- '.github/**'
- 'examples/**'
pull_request:
types:
- opened
branches:
- 'master'
workflow_dispatch:

jobs:
build:
permissions: write-all
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Setup PHP with PECL extension
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: imagick, swoole
coverage: xdebug
- name: Build with Ant
run: ant -noinput -buildfile build.ant.xml
- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: github.event_name != 'pull_request'
with:
files: "./build/logs/junit.xml"
40 changes: 40 additions & 0 deletions .github/workflows/build.php83.ant.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CI (Ant, PHP 8.3)

on:
push:
tags-ignore:
- '**'
branches:
- '**'
paths-ignore:
- '**.md'
- '**.tmpl'
- '.github/**'
- 'examples/**'
pull_request:
types:
- opened
branches:
- 'master'
workflow_dispatch:

jobs:
build:
permissions: write-all
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Setup PHP with PECL extension
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: imagick, swoole
coverage: xdebug
- name: Build with Ant
run: ant -noinput -buildfile build.ant.xml
- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: github.event_name != 'pull_request'
with:
files: "./build/logs/junit.xml"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,4 @@ _backup*

/examples/invoice_1.pdf
/examples/invoice_2.pdf
/examples/fonts
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [Set PDF-Options](#set-pdf-options)
- [Set options before instanciating the internal PDF-Engine (```setPdfPreInitCallback```)](#set-options-before-instanciating-the-internal-pdf-engine-setpdfpreinitcallback)
- [Set options after instanciating the internal PDF-Engine (```setPdfRuntimeInitCallback```)](#set-options-after-instanciating-the-internal-pdf-engine-setpdfruntimeinitcallback)
- [Working with custom fonts](#working-with-custom-fonts)

## License

Expand Down Expand Up @@ -249,4 +250,44 @@ $visualizer->setPdfRuntimeInitCallback(function (Mpdf $mpdf, ZugferdVisualizer $
});
```

For configuration options, please consult the documentation of [mPdf](https://mpdf.github.io/configuration/configuration-v7-x.html)
#### Working with custom fonts

If you would like to use your own fonts, that's no problem at all. First you have to specify one or more directories in which your fonts are located:

```php
use horstoeko\zugferdvisualizer\ZugferdVisualizer;
use Mpdf\Mpdf;

$visualizer = new ZugferdVisualizer(static::$document);
$visualizer->addPdfFontDirectory('/var/fonts1/');
$visualizer->addPdfFontDirectory('/var/fonts2/');
```

Next, you need to define the font properties:

* The first parameter sets the name of the font-family
* Thé second parameter sets the type of the font
* R - Regular
* I - Italic
* B - Bold
* BI - Bold & Italic
* The third parameter sets the filename under which the font can be found in the specified font-directories

```php
$visualizer->addPdfFontData('comicsans', 'R', 'comic.ttf');
$visualizer->addPdfFontData('comicsans', 'I', 'comici.ttf');
```

If you want to set a custom font as the default font, you can use the following method:

```php
$visualizer->setPdfFontDefault("comicsans");
```

You can also use the name of the font family in the style attribute of any HTML elements in your template:

```html
<p style="font-family: comicsans">Text in Comic Sans</p>
```

For more configuration options, please consult the documentation of [mPdf](https://mpdf.github.io/configuration/configuration-v7-x.html)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}
},
"require": {
"php": "^7.3|^7.4|^8.0|^8.1|^8.2|^8.3",
"php": "^7.3|^7.4|^8",
"ext-dom": "*",
"ext-mbstring": "*",
"dompdf/dompdf": "^2.0",
Expand Down
3 changes: 3 additions & 0 deletions examples/BuildFromDocumentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@

$visualizer = new ZugferdVisualizer($reader);
$visualizer->setDefaultTemplate();
$visualizer->addPdfFontDirectory(dirname(__FILE__) . '/fonts/');
$visualizer->addPdfFontData('comicsans', 'R', 'comic.ttf');
$visualizer->addPdfFontData('comicsans', 'I', 'comici.ttf');
$visualizer->setPdfFontDefault("courier");
$visualizer->setPdfPaperSize('A4-P');

Expand Down
3 changes: 3 additions & 0 deletions examples/BuildFromDocumentReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

$visualizer = new ZugferdVisualizer($document);
$visualizer->setDefaultTemplate();
$visualizer->addPdfFontDirectory(dirname(__FILE__) . '/fonts/');
$visualizer->addPdfFontData('comicsans', 'R', 'comic.ttf');
$visualizer->addPdfFontData('comicsans', 'I', 'comici.ttf');
$visualizer->setPdfFontDefault("courier");
$visualizer->setPdfPaperSize('A4-P');
$visualizer->renderPdfFile(dirname(__FILE__) . "/invoice_1.pdf");
3 changes: 3 additions & 0 deletions examples/BuildFromDocumentReader2.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

$visualizer = new ZugferdVisualizer($document);
$visualizer->setDefaultTemplate();
$visualizer->addPdfFontDirectory(dirname(__FILE__) . '/fonts/');
$visualizer->addPdfFontData('comicsans', 'R', 'comic.ttf');
$visualizer->addPdfFontData('comicsans', 'I', 'comici.ttf');
$visualizer->setPdfFontDefault("courier");
$visualizer->setPdfPaperSize('A4-P');
$visualizer->renderPdfFile(dirname(__FILE__) . "/invoice_2.pdf");
16 changes: 16 additions & 0 deletions src/ZugferdVisualizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@ public function addPdfFontDirectory(string $directory): void
$this->pdfFontDirectories[] = $directory;
}

/**
* Add a font definition
*
* - Example 1: ``$visualizer->addPdfFont('frutiger', 'R', 'Frutiger-Normal.ttf')``
* - Example 2: ``$visualizer->addPdfFont('frutiger', 'I', 'FrutigerObl-Normal.ttf')``
*
* @param string $name
* @param string $style
* @param string $filename
* @return void
*/
public function addPdfFontData(string $name, string $style, string $filename): void
{
$this->pdfFontData[$name][$style] = $filename;
}

/**
* Sets the default PDF default font
*
Expand Down

0 comments on commit 3a53ebf

Please sign in to comment.