From 528b3a5cb405b4f59151f525562c90461d876413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Sat, 2 Apr 2022 11:20:07 +0200 Subject: [PATCH] Issue #281 Implement `replacementFont($font)` (requires pdftk-java >= 3.3.0) --- README.md | 13 +++++++++++++ src/Pdf.php | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/README.md b/README.md index 6776b8c..6daa9ce 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,19 @@ This will make sure, that the PDF reader takes care of using the right fonts for something that pdftk can't do for you. Also note that `flatten()` doesn't really work well if you have special characters in your data. +If you use `pdftk-java` >= 3.3.0 and the embedded font does not support UTF-8 +characters you can also replace it with a local font: + +```php +use mikehaertl\pdftk\Pdf; + +// Fill form with data array +$pdf = new Pdf('/full/path/to/form.pdf'); +$result = $pdf->fillForm($data) + ->replacementFont('/usr/share/fonts/dejavu/DejaVuSans.ttf') + ->saveAs('filled.pdf'); +``` + #### Create a XFDF/FDF file from a PHP array This is a bonus feature that is not available from `pdftk`. diff --git a/src/Pdf.php b/src/Pdf.php index 31270b6..5a83fc8 100644 --- a/src/Pdf.php +++ b/src/Pdf.php @@ -588,6 +588,23 @@ public function passwordEncryption($strength = 128) return $this; } + /** + * Replace embedded font with a local font when filling a form. + * + * This option is only available for pdftk-java >= 3.3.0. It is useful when + * filling a form with non-ASCII text that is not supported by the fonts + * included in the input PDF. + * + * @param string $fontName the path to the font or the name of a font family. + * @return Pdf the pdf instance for method chaining + */ + public function replacementFont($path) + { + $this->getCommand() + ->addOption('replacement_font', $path); + return $this; + } + /** * Execute the operation and save the output file *