Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PDF watermark feature with customizable text, display, and position settings #35

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion app/Entities/Tools/PdfGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use BookStack\Exceptions\PdfExportException;
use Knp\Snappy\Pdf as SnappyPdf;
use Dompdf\Dompdf;
use Dompdf\Options;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -56,13 +57,36 @@ protected function getWkhtmlBinaryPath(): string

protected function renderUsingDomPdf(string $html): string
{
$options = config('exports.dompdf');
$options = new Options(config('exports.dompdf'));
$domPdf = new Dompdf($options);
$domPdf->setBasePath(base_path('public'));

$domPdf->loadHTML($this->convertEntities($html));
$domPdf->render();

// Check watermark display is enable or not in settings
if (setting('watermark-display')) {
//Add WaterMark in pdf;
$domPdf->getCanvas()->page_script(function ($pageNumber, $pageCount, $canvas, $fontMetrics) {
$font = $fontMetrics->getFont('times');
$text = setting('watermark-text');

$fontSize = 30;
$canvas->set_opacity(0.15);

// Get the width and height of the page
$w = $canvas->get_width();
$h = $canvas->get_height();

// set position of watermark based on watermark position in settings
$x = \Str::contains(setting('watermark-position'), 'Right') ? $w - $fontMetrics->getTextWidth($text, $font, $fontSize) - 20 : 20;
$y = \Str::contains(setting('watermark-position'), 'Bottom') ? $h - $fontMetrics->getFontHeight($font, $fontSize) - 10 : 10;

// Add the watermark text
$canvas->text($x, $y, $text, $font, $fontSize, array(0, 0, 0), '', '', 0);
});
}

return (string) $domPdf->output();
}

Expand Down
3 changes: 3 additions & 0 deletions app/Settings/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public function update(Request $request, AppSettingsStore $store, string $catego
$this->validate($request, [
'app_logo' => ['nullable', ...$this->getImageValidationRules()],
'app_icon' => ['nullable', ...$this->getImageValidationRules()],
'setting-watermark-display' => ['nullable','string'],
'setting-watermark-text' => ['nullable','string'],
'setting-watermark-position' => ['nullable','string'],
]);

$store->storeFromUpdateRequest($request, $category);
Expand Down
6 changes: 6 additions & 0 deletions lang/bg/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Other Licenses',
'license_details' => 'License Details',

'watermark_text' => 'Текст на водния знак',
'watermark_desc' => 'Задайте текста, който искате да отпечатате като воден знак в PDF',
'watermark_position_text' => 'Позиция на водния знак',
'watermark_position_desc' => 'Изберете позицията за водния знак в PDF',
'watermark_display' => 'Покажи водния знак в PDF',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/ca/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Other Licenses',
'license_details' => 'License Details',

'watermark_text' => 'Text de marca d\'aigua',
'watermark_desc' => 'Estableix el text que vols imprimir com a marca d’aigua al PDF',
'watermark_position_text' => 'Posició de la marca d\'aigua',
'watermark_position_desc' => 'Selecciona la posició de la marca d\'aigua al PDF',
'watermark_display' => 'Mostrar marca d\'aigua al PDF',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/cs/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Ostatní licence',
'license_details' => 'Podrobnosti o licenci',

'watermark_text' => 'Text vodoznaku',
'watermark_desc' => 'Nastavte text, který chcete vytisknout jako vodoznak v PDF',
'watermark_position_text' => 'Pozice vodoznaku',
'watermark_position_desc' => 'Vyberte pozici pro vodoznak umístěný v PDF',
'watermark_display' => 'Zobrazit vodoznak v PDF',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/cy/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Trwyddedau Eraill',
'license_details' => 'Manylion y Drwydded',

'watermark_text' => 'Testun Dyfrnod',
'watermark_desc' => 'Gosod Testun rydych chi am ei argraffu fel dyfrnod mewn PDF',
'watermark_position_text' => 'Safle Dyfrnod',
'watermark_position_desc' => 'Dewiswch y safle ar gyfer y nodyn dŵr yn y PDF',
'watermark_display' => 'Dangos Dyfrnod mewn PDF',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/da/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Other Licenses',
'license_details' => 'License Details',

'watermark_text' => 'Vandmærkets tekst',
'watermark_desc' => 'Indstil tekst, som du vil have trykt som vandmærke i PDF',
'watermark_position_text' => 'Vandmærkets position',
'watermark_position_desc' => 'Vælg positionen for vandmærket i PDF',
'watermark_display' => 'Vis vandmærke i PDF',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/de/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@
'licenses_other' => 'Andere Lizenzen',
'license_details' => 'Lizenzdetails',

'watermark_text' => 'Wasserzeichen-Text',
'watermark_desc' => 'Legen Sie den Text fest, den Sie als Wasserzeichen in PDF drucken möchten',
'watermark_position_text' => 'Wasserzeichen-Position',
'watermark_position_desc' => 'Wählen Sie die Position für das Wasserzeichen in PDF',
'watermark_display' => 'Wasserzeichen in PDF anzeigen',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/el/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Other Licenses',
'license_details' => 'License Details',

'watermark_text' => 'Κείμενο υδατογραφήματος',
'watermark_desc' => 'Ορίστε το κείμενο που θέλετε να εκτυπώσετε ως υδατογράφημα στο PDF',
'watermark_position_text' => 'Θέση υδατογραφήματος',
'watermark_position_desc' => 'Επιλέξτε τη θέση για υδατογράφημα που τοποθετείται στο PDF',
'watermark_display' => 'Εμφάνιση υδατογράφημα στο PDF',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/en/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Other Licenses',
'license_details' => 'License Details',

'watermark_text' => 'Watermark Text',
'watermark_desc' => 'Set Text that you want to print as watermark in PDF',
'watermark_position_text' => 'Watermark Position',
'watermark_position_desc' => 'Select the position for watermark placed in PDF',
'watermark_display' => 'Show Watermark in PDF',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/es/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Otras Licencias',
'license_details' => 'Datos de la licencia',

'watermark_text' => 'Texto de marca de agua',
'watermark_desc' => 'Establezca el texto que desea imprimir como marca de agua en PDF',
'watermark_position_text' => 'Posición de marca de agua',
'watermark_position_desc' => 'Seleccione la posición para la marca de agua colocada en PDF',
'watermark_display' => 'Mostrar marca de agua en PDF',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/es_AR/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@
'licenses_other' => 'Otras Licencias',
'license_details' => 'Datos de la licencia',

'watermark_text' => 'Texto de marca de agua',
'watermark_desc' => 'Establezca el texto que desea imprimir como marca de agua en PDF',
'watermark_position_text' => 'Posición de marca de agua',
'watermark_position_desc' => 'Seleccione la posición para la marca de agua colocada en PDF',
'watermark_display' => 'Mostrar marca de agua en PDF',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/et/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Muud litsentsid',
'license_details' => 'Litsentsi detailid',

'watermark_text' => 'Vesimärgi tekst',
'watermark_desc' => 'Määrake tekst, mille soovite PDF -is vesimärgina printida',
'watermark_position_text' => 'Vesimärgi asukoht',
'watermark_position_desc' => 'Valige PDF -is asetatud vesimärgi positsioon',
'watermark_display' => 'Näidake vesimärki PDF -is',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/eu/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Other Licenses',
'license_details' => 'License Details',

'watermark_text' => 'Watermark testua',
'watermark_desc' => 'Ezarri PDFan ur-marka gisa inprimatu nahi duzun testua',
'watermark_position_text' => 'Watermark posizioa',
'watermark_position_desc' => 'Hautatu PDFan jarritako ur-marka',
'watermark_display' => 'Erakutsi ur-marka PDFn',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/fa/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'سایر مجوز‌ها',
'license_details' => 'جزئیات مجوز',

'watermark_text' => 'متن واترمارک',
'watermark_desc' => 'متنی را که می خواهید به عنوان واترمارک در PDF چاپ کنید تنظیم کنید',
'watermark_position_text' => 'موقعیت واترمارک',
'watermark_position_desc' => 'موقعیت واترمارک قرار داده شده در PS را انتخاب کنید. موقعیت واترمارک قرار داده شده در PDFDF را انتخاب کنید',
'watermark_display' => 'نمایش واترمارک در PDF',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/fi/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Other Licenses',
'license_details' => 'License Details',

'watermark_text' => 'Vesileiman teksti',
'watermark_desc' => 'Aseta teksti, jonka haluat tulostaa vesileimaksi PDF:ssä',
'watermark_position_text' => 'Vesileiman sijainti',
'watermark_position_desc' => 'Valitse PDF-tiedoston vesileiman sijainti',
'watermark_display' => 'Näytä vesileima PDF-muodossa',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/fr/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Autres Licences',
'license_details' => 'Détails de la licence',

'watermark_text' => 'Texte de filigrane',
'watermark_desc' => 'Définissez le texte que vous souhaitez imprimer en filigrane dans PDF',
'watermark_position_text' => 'Position de filigrane',
'watermark_position_desc' => 'Sélectionnez la position pour le filigrane placé dans PDF',
'watermark_display' => 'Afficher le filigrane en PDF',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/he/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Other Licenses',
'license_details' => 'License Details',

'watermark_text' => 'טקסט סימן מים',
'watermark_desc' => 'הגדר טקסט שברצונך להדפיס כסימן מים ב- PDF',
'watermark_position_text' => 'מיקום סימן מים',
'watermark_position_desc' => 'בחר את המיקום של סימן מים המונח ב- PDF',
'watermark_display' => 'הצג סימן מים ב- PDF',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/hr/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Other Licenses',
'license_details' => 'License Details',

'watermark_text' => 'Tekst vodenog žiga',
'watermark_desc' => 'Postavite tekst koji želite ispisati kao vodeni žig u PDF -u',
'watermark_position_text' => 'Položaj vodenog žiga',
'watermark_position_desc' => 'Odaberite položaj za vodeni žig smješten u PDF',
'watermark_display' => 'Pokažite vodeni žig u PDF -u',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/hu/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Egyéb licencek',
'license_details' => 'Licenc részletek',

'watermark_text' => 'Vízjel -szöveg',
'watermark_desc' => 'Állítson be szöveget, amelyet vízjelként nyomtatni szeretne PDF -ben',
'watermark_position_text' => 'Vízjel helyzete',
'watermark_position_desc' => 'Válassza ki a PDF -be elhelyezett vízjel helyzetét',
'watermark_display' => 'Mutasd meg a vízjelet a PDF -ben',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/id/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Other Licenses',
'license_details' => 'License Details',

'watermark_text' => 'Teks tanda air',
'watermark_desc' => 'Atur teks yang ingin Anda cetak sebagai tanda air di PDF',
'watermark_position_text' => 'Posisi tanda air',
'watermark_position_desc' => 'Pilih posisi untuk tanda air yang ditempatkan di PDF',
'watermark_display' => 'Tunjukkan tanda air di PDF',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/it/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Altre Licenze',
'license_details' => 'Dettagli Licenza',

'watermark_text' => 'Testo di filigrana',
'watermark_desc' => 'Imposta il testo che si desidera stampare come filigrana in PDF',
'watermark_position_text' => 'Posizione di filigrana',
'watermark_position_desc' => 'Seleziona la posizione per la filigrana inserita in PDF',
'watermark_display' => 'Mostra filigrana in PDF',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/ja/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'その他のライセンス',
'license_details' => 'ライセンス詳細',

'watermark_text' => '透かしテキスト',
'watermark_desc' => 'PDFの透かしとして印刷したいテキストを設定します',
'watermark_position_text' => '透かしの位置',
'watermark_position_desc' => 'PDFに配置された透かしの位置を選択します',
'watermark_display' => 'PDFで透かしを表示します',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/ko/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => '기타 라이선스',
'license_details' => '라이선스 세부 사항',

'watermark_text' => '워터 마크 텍스트',
'watermark_desc' => 'PDF에서 Watermark로 인쇄하려는 텍스트 설정',
'watermark_position_text' => '워터 마크 위치',
'watermark_position_desc' => 'PDF에 배치 된 워터 마크 위치를 선택하십시오',
'watermark_display' => 'PDF로 워터 마크를 보여줍니다',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
7 changes: 7 additions & 0 deletions lang/lt/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@
'licenses_other' => 'Other Licenses',
'license_details' => 'License Details',

'watermark_text' => 'Vandens ženklo tekstas',
'watermark_desc' => 'Nustatykite tekstą, kurį norite atspausdinti kaip vandens ženklą PDF',
'watermark_position_text' => 'Vandens ženklo padėtis',
'watermark_position_desc' => 'Pasirinkite „PDF“ įdėto vandens ženklo poziciją',
'watermark_display' => 'Rodyti vandens ženklą PDF',


//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/lv/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Citas licences',
'license_details' => 'Licences informācija',

'watermark_text' => 'Ūdenszīmes teksts',
'watermark_desc' => 'Iestatiet tekstu, kuru vēlaties izdrukāt kā ūdenszīmi PDF',
'watermark_position_text' => 'Ūdenszīmes stāvoklis',
'watermark_position_desc' => 'Atlasiet PDF ievietoto ūdenszīmju pozīciju',
'watermark_display' => 'Parādīt ūdenszīmi PDF',

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/nb/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
'licenses_other' => 'Andre lisenser',
'license_details' => 'Lisens detaljer',

'watermark_text' => "Vannmerke tekst",
'watermark_desc' => "Angi teksten du ønsker å skrive ut som vannmerke i PDF",
'watermark_position_text' => "Vannmerke posisjon",
'watermark_position_desc' => "Velg posisjonen for vannmerket i PDF",
'watermark_display' => "Vis vannmerke i PDF",

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
//!////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions lang/nl/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@
'licenses_js' => 'JavaScript Bibliotheek Licenties',
'licenses_other' => 'Andere Licenties',
'license_details' => 'Licentie Details',

'watermark_text' => "Watermerk tekst",
'watermark_desc' => "Stel de tekst in die je wilt afdrukken als watermerk in PDF",
'watermark_position_text' => "Watermerk positie",
'watermark_position_desc' => "Selecteer de positie voor het watermerk in PDF",
'watermark_display' => "Toon watermerk in PDF",

//! If editing translations files directly please ignore this in all
//! languages apart from en. Content will be auto-copied from en.
Expand Down
Loading