From c8f31ec16a08fdbe3872af7ba0f4d5e30b2e79df Mon Sep 17 00:00:00 2001 From: "m.binerbay" Date: Thu, 28 Mar 2024 13:22:43 +0100 Subject: [PATCH 01/11] Pass metadata to gotenberg-chromium processor --- src/Processor/Gotenberg.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Processor/Gotenberg.php b/src/Processor/Gotenberg.php index 87941cc..78f4251 100644 --- a/src/Processor/Gotenberg.php +++ b/src/Processor/Gotenberg.php @@ -159,6 +159,10 @@ public function getPdfFromString(string $html, array $params = [], bool $returnF $chromium->extraHttpHeaders($params['extraHttpHeaders']); } + if (isset($params['metadata'])) { + $chromium->metadata($params['metadata']); + } + $request = $chromium->outputFilename($tempFileName)->html(Stream::string('processor.html', $html)); if ($returnFilePath) { From 0d5a0b15445dbc512c23f41c954a7041cbdb1626 Mon Sep 17 00:00:00 2001 From: "m.binerbay" Date: Tue, 2 Apr 2024 10:32:29 +0200 Subject: [PATCH 02/11] Add backwards compatibility layer for gotenberg-php v1 --- src/Processor/Gotenberg.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Processor/Gotenberg.php b/src/Processor/Gotenberg.php index 78f4251..fb3de0c 100644 --- a/src/Processor/Gotenberg.php +++ b/src/Processor/Gotenberg.php @@ -159,8 +159,10 @@ public function getPdfFromString(string $html, array $params = [], bool $returnF $chromium->extraHttpHeaders($params['extraHttpHeaders']); } - if (isset($params['metadata'])) { + if (isset($params['metadata']) && method_exists($chromium, 'metadata')) { $chromium->metadata($params['metadata']); + } elseif (isset($params['metadata'])) { + $chromium->formValue('metadata', json_encode($params['metadata'])); } $request = $chromium->outputFilename($tempFileName)->html(Stream::string('processor.html', $html)); From 5e44e852faf3f67347fa7cd0abbd25e4d18c42da Mon Sep 17 00:00:00 2001 From: muratbinerbay Date: Tue, 2 Apr 2024 11:19:24 +0200 Subject: [PATCH 03/11] Check the existence of metadata only once Co-authored-by: Sebastian Blank --- src/Processor/Gotenberg.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Processor/Gotenberg.php b/src/Processor/Gotenberg.php index fb3de0c..27f455c 100644 --- a/src/Processor/Gotenberg.php +++ b/src/Processor/Gotenberg.php @@ -159,10 +159,12 @@ public function getPdfFromString(string $html, array $params = [], bool $returnF $chromium->extraHttpHeaders($params['extraHttpHeaders']); } - if (isset($params['metadata']) && method_exists($chromium, 'metadata')) { - $chromium->metadata($params['metadata']); - } elseif (isset($params['metadata'])) { - $chromium->formValue('metadata', json_encode($params['metadata'])); + if (isset($params['metadata'])) { + if (method_exists($chromium, 'metadata')) { + $chromium->metadata($params['metadata']); + } else { + $chromium->formValue('metadata', json_encode($params['metadata'])); + } } $request = $chromium->outputFilename($tempFileName)->html(Stream::string('processor.html', $html)); From 437b18f2a4d28be00baee95fa6d0e9f648cfb458 Mon Sep 17 00:00:00 2001 From: muratbinerbay Date: Tue, 2 Apr 2024 13:06:03 +0200 Subject: [PATCH 04/11] Just use formValue to support both v1 and v2 Co-authored-by: JiaJia Ji --- src/Processor/Gotenberg.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Processor/Gotenberg.php b/src/Processor/Gotenberg.php index 27f455c..4511bdd 100644 --- a/src/Processor/Gotenberg.php +++ b/src/Processor/Gotenberg.php @@ -160,11 +160,9 @@ public function getPdfFromString(string $html, array $params = [], bool $returnF } if (isset($params['metadata'])) { - if (method_exists($chromium, 'metadata')) { - $chromium->metadata($params['metadata']); - } else { - $chromium->formValue('metadata', json_encode($params['metadata'])); - } + // TODO: Replace with metadata() once dropping support for gotenberg-php < 2.2.0 + $chromium->formValue('metadata', json_encode($params['metadata'])); + } $request = $chromium->outputFilename($tempFileName)->html(Stream::string('processor.html', $html)); From e057251794f765c64f465d8cf61fabdfa1263ab6 Mon Sep 17 00:00:00 2001 From: "m.binerbay" Date: Tue, 2 Apr 2024 13:07:35 +0200 Subject: [PATCH 05/11] Remove empty new line --- src/Processor/Gotenberg.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Processor/Gotenberg.php b/src/Processor/Gotenberg.php index 4511bdd..f765132 100644 --- a/src/Processor/Gotenberg.php +++ b/src/Processor/Gotenberg.php @@ -162,7 +162,6 @@ public function getPdfFromString(string $html, array $params = [], bool $returnF if (isset($params['metadata'])) { // TODO: Replace with metadata() once dropping support for gotenberg-php < 2.2.0 $chromium->formValue('metadata', json_encode($params['metadata'])); - } $request = $chromium->outputFilename($tempFileName)->html(Stream::string('processor.html', $html)); From edbe36a2f7a64d0601cac93f33d7f56bd92066c6 Mon Sep 17 00:00:00 2001 From: "m.binerbay" Date: Tue, 2 Apr 2024 13:25:29 +0200 Subject: [PATCH 06/11] Revert "Remove empty new line" This reverts commit e057251794f765c64f465d8cf61fabdfa1263ab6. --- src/Processor/Gotenberg.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Processor/Gotenberg.php b/src/Processor/Gotenberg.php index f765132..4511bdd 100644 --- a/src/Processor/Gotenberg.php +++ b/src/Processor/Gotenberg.php @@ -162,6 +162,7 @@ public function getPdfFromString(string $html, array $params = [], bool $returnF if (isset($params['metadata'])) { // TODO: Replace with metadata() once dropping support for gotenberg-php < 2.2.0 $chromium->formValue('metadata', json_encode($params['metadata'])); + } $request = $chromium->outputFilename($tempFileName)->html(Stream::string('processor.html', $html)); From 0f07b312992ed98a5a9e84d8a1c2d6f076e921b0 Mon Sep 17 00:00:00 2001 From: "m.binerbay" Date: Tue, 2 Apr 2024 13:25:29 +0200 Subject: [PATCH 07/11] Revert "Just use formValue to support both v1 and v2" This reverts commit 437b18f2a4d28be00baee95fa6d0e9f648cfb458. --- src/Processor/Gotenberg.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Processor/Gotenberg.php b/src/Processor/Gotenberg.php index 4511bdd..27f455c 100644 --- a/src/Processor/Gotenberg.php +++ b/src/Processor/Gotenberg.php @@ -160,9 +160,11 @@ public function getPdfFromString(string $html, array $params = [], bool $returnF } if (isset($params['metadata'])) { - // TODO: Replace with metadata() once dropping support for gotenberg-php < 2.2.0 - $chromium->formValue('metadata', json_encode($params['metadata'])); - + if (method_exists($chromium, 'metadata')) { + $chromium->metadata($params['metadata']); + } else { + $chromium->formValue('metadata', json_encode($params['metadata'])); + } } $request = $chromium->outputFilename($tempFileName)->html(Stream::string('processor.html', $html)); From 6f4218843087abc54f08bc836825231de21c06e3 Mon Sep 17 00:00:00 2001 From: "m.binerbay" Date: Tue, 2 Apr 2024 13:48:52 +0200 Subject: [PATCH 08/11] Update minimum versions for gotenberg/gotenberg-php dependency --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9666e65..e2ab1be 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "require-dev": { "phpstan/phpstan": "^1.10.5", "phpstan/phpstan-symfony": "^1.2.20", - "gotenberg/gotenberg-php": "^1.0 || ^2.0", + "gotenberg/gotenberg-php": "^1.1.8 || ^2.2", "chrome-php/chrome": "^1.8", "codeception/codeception": "^5.0.3", "codeception/module-symfony": "^3.1.0", From cc2800cef3d05581d9f39db8afa6d08ed0932bb3 Mon Sep 17 00:00:00 2001 From: "m.binerbay" Date: Tue, 2 Apr 2024 16:45:29 +0200 Subject: [PATCH 09/11] Ignore phpstan error because code should not be reachable when formValue is protected --- src/Processor/Gotenberg.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Processor/Gotenberg.php b/src/Processor/Gotenberg.php index 27f455c..fa4d2e2 100644 --- a/src/Processor/Gotenberg.php +++ b/src/Processor/Gotenberg.php @@ -163,6 +163,7 @@ public function getPdfFromString(string $html, array $params = [], bool $returnF if (method_exists($chromium, 'metadata')) { $chromium->metadata($params['metadata']); } else { + /* @phpstan-ignore-next-line */ $chromium->formValue('metadata', json_encode($params['metadata'])); } } From 30a08c3d4775ab34c75fb4ab440d9ad3520195e8 Mon Sep 17 00:00:00 2001 From: muratbinerbay Date: Tue, 2 Apr 2024 16:57:26 +0200 Subject: [PATCH 10/11] Try checking formValue with is_callable Co-authored-by: Sebastian Blank --- src/Processor/Gotenberg.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Processor/Gotenberg.php b/src/Processor/Gotenberg.php index fa4d2e2..7cfd1dc 100644 --- a/src/Processor/Gotenberg.php +++ b/src/Processor/Gotenberg.php @@ -162,8 +162,7 @@ public function getPdfFromString(string $html, array $params = [], bool $returnF if (isset($params['metadata'])) { if (method_exists($chromium, 'metadata')) { $chromium->metadata($params['metadata']); - } else { - /* @phpstan-ignore-next-line */ + } elseif (is_callable([$chromium, 'formValue'])) { $chromium->formValue('metadata', json_encode($params['metadata'])); } } From 1c635cd113dd9f9ecd8d6fae382db850d8adddfb Mon Sep 17 00:00:00 2001 From: "m.binerbay" Date: Tue, 28 May 2024 11:22:18 +0200 Subject: [PATCH 11/11] Pass metadata to gotenberg-chromium on version > 2.2 --- src/Processor/Gotenberg.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Processor/Gotenberg.php b/src/Processor/Gotenberg.php index 7cfd1dc..c018323 100644 --- a/src/Processor/Gotenberg.php +++ b/src/Processor/Gotenberg.php @@ -159,12 +159,9 @@ public function getPdfFromString(string $html, array $params = [], bool $returnF $chromium->extraHttpHeaders($params['extraHttpHeaders']); } - if (isset($params['metadata'])) { - if (method_exists($chromium, 'metadata')) { - $chromium->metadata($params['metadata']); - } elseif (is_callable([$chromium, 'formValue'])) { - $chromium->formValue('metadata', json_encode($params['metadata'])); - } + // metadata is only passed on gotenberg-php > 2.2 + if (isset($params['metadata']) && method_exists($chromium, 'metadata')) { + $chromium->metadata($params['metadata']); } $request = $chromium->outputFilename($tempFileName)->html(Stream::string('processor.html', $html));