From 1591b3bb20676a19c150a0c8573de2aea7c55f04 Mon Sep 17 00:00:00 2001 From: HorstOeko Date: Mon, 17 Jun 2024 17:28:08 +0200 Subject: [PATCH 1/2] Add custom fonts, Updated README.md (Usage of custom fonts) (#1) --- .gitignore | 1 + README.md | 43 ++++++++++++++++++++++++++- examples/BuildFromDocumentBuilder.php | 3 ++ examples/BuildFromDocumentReader.php | 3 ++ examples/BuildFromDocumentReader2.php | 3 ++ src/ZugferdVisualizer.php | 16 ++++++++++ 6 files changed, 68 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ac7cbda..4d52dff 100644 --- a/.gitignore +++ b/.gitignore @@ -84,3 +84,4 @@ _backup* /examples/invoice_1.pdf /examples/invoice_2.pdf +/examples/fonts diff --git a/README.md b/README.md index 33f9082..ff532e4 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) \ No newline at end of file +#### 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 +

Text in Comic Sans

+``` + +For more configuration options, please consult the documentation of [mPdf](https://mpdf.github.io/configuration/configuration-v7-x.html) diff --git a/examples/BuildFromDocumentBuilder.php b/examples/BuildFromDocumentBuilder.php index 2837cfd..fc66da8 100644 --- a/examples/BuildFromDocumentBuilder.php +++ b/examples/BuildFromDocumentBuilder.php @@ -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'); diff --git a/examples/BuildFromDocumentReader.php b/examples/BuildFromDocumentReader.php index 2275e1b..e7f7ccd 100644 --- a/examples/BuildFromDocumentReader.php +++ b/examples/BuildFromDocumentReader.php @@ -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"); diff --git a/examples/BuildFromDocumentReader2.php b/examples/BuildFromDocumentReader2.php index a880c86..833c5a8 100644 --- a/examples/BuildFromDocumentReader2.php +++ b/examples/BuildFromDocumentReader2.php @@ -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"); diff --git a/src/ZugferdVisualizer.php b/src/ZugferdVisualizer.php index 1d4b1e9..293dc46 100644 --- a/src/ZugferdVisualizer.php +++ b/src/ZugferdVisualizer.php @@ -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 * From 3360a18b0c5ac4a4c2a4f43cd4967cfe95715626 Mon Sep 17 00:00:00 2001 From: HorstOeko Date: Mon, 17 Jun 2024 17:33:38 +0200 Subject: [PATCH 2/2] Support for PHP 8.2 and PHP 8.3 --- .github/workflows/build.php73.ant.yml | 13 ++++-- .github/workflows/build.php74.ant.yml | 13 ++++-- .github/workflows/build.php80.ant.yml | 13 ++++-- .github/workflows/build.php81.ant.yml | 13 ++++-- .github/workflows/build.php81.release.ant.yml | 5 ++- .github/workflows/build.php82.ant.yml | 40 +++++++++++++++++++ .github/workflows/build.php83.ant.yml | 40 +++++++++++++++++++ composer.json | 2 +- 8 files changed, 124 insertions(+), 15 deletions(-) create mode 100644 .github/workflows/build.php82.ant.yml create mode 100644 .github/workflows/build.php83.ant.yml diff --git a/.github/workflows/build.php73.ant.yml b/.github/workflows/build.php73.ant.yml index e8783dd..4589386 100644 --- a/.github/workflows/build.php73.ant.yml +++ b/.github/workflows/build.php73.ant.yml @@ -10,6 +10,13 @@ on: - '**.md' - '**.tmpl' - '.github/**' + - 'examples/**' + pull_request: + types: + - opened + branches: + - 'master' + workflow_dispatch: jobs: build: @@ -17,7 +24,7 @@ jobs: 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: @@ -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" diff --git a/.github/workflows/build.php74.ant.yml b/.github/workflows/build.php74.ant.yml index fcb4c72..54bc94f 100644 --- a/.github/workflows/build.php74.ant.yml +++ b/.github/workflows/build.php74.ant.yml @@ -10,6 +10,13 @@ on: - '**.md' - '**.tmpl' - '.github/**' + - 'examples/**' + pull_request: + types: + - opened + branches: + - 'master' + workflow_dispatch: jobs: build: @@ -17,7 +24,7 @@ jobs: 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: @@ -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" diff --git a/.github/workflows/build.php80.ant.yml b/.github/workflows/build.php80.ant.yml index 915661a..2bdbf98 100644 --- a/.github/workflows/build.php80.ant.yml +++ b/.github/workflows/build.php80.ant.yml @@ -10,6 +10,13 @@ on: - '**.md' - '**.tmpl' - '.github/**' + - 'examples/**' + pull_request: + types: + - opened + branches: + - 'master' + workflow_dispatch: jobs: build: @@ -17,7 +24,7 @@ jobs: 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: @@ -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" diff --git a/.github/workflows/build.php81.ant.yml b/.github/workflows/build.php81.ant.yml index da720a2..7cad265 100644 --- a/.github/workflows/build.php81.ant.yml +++ b/.github/workflows/build.php81.ant.yml @@ -10,6 +10,13 @@ on: - '**.md' - '**.tmpl' - '.github/**' + - 'examples/**' + pull_request: + types: + - opened + branches: + - 'master' + workflow_dispatch: jobs: build: @@ -17,7 +24,7 @@ jobs: 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: @@ -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" diff --git a/.github/workflows/build.php81.release.ant.yml b/.github/workflows/build.php81.release.ant.yml index 5f06afe..bb3046c 100644 --- a/.github/workflows/build.php81.release.ant.yml +++ b/.github/workflows/build.php81.release.ant.yml @@ -8,6 +8,7 @@ on: - '**.md' - '**.tmpl' - '.github/**' + - 'examples/**' jobs: build: @@ -15,7 +16,7 @@ jobs: 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: @@ -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" diff --git a/.github/workflows/build.php82.ant.yml b/.github/workflows/build.php82.ant.yml new file mode 100644 index 0000000..2f11161 --- /dev/null +++ b/.github/workflows/build.php82.ant.yml @@ -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" diff --git a/.github/workflows/build.php83.ant.yml b/.github/workflows/build.php83.ant.yml new file mode 100644 index 0000000..98224cd --- /dev/null +++ b/.github/workflows/build.php83.ant.yml @@ -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" diff --git a/composer.json b/composer.json index 069bb2a..da5ab3e 100644 --- a/composer.json +++ b/composer.json @@ -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",