Skip to content

[Gotenberg] Add assets functionality #95

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

Merged
merged 2 commits into from
May 26, 2025
Merged
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
27 changes: 21 additions & 6 deletions src/Processor/Gotenberg.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

/**
Expand All @@ -16,12 +17,12 @@

namespace Pimcore\Bundle\WebToPrintBundle\Processor;

use function array_key_exists;
use function array_merge;
use function file_exists;
use Gotenberg\Gotenberg as GotenbergAPI;
use Gotenberg\Stream;
use function json_decode;
use function key_exists;
use Pimcore\Bundle\WebToPrintBundle\Config;
use Pimcore\Bundle\WebToPrintBundle\Event\DocumentEvents;
use Pimcore\Bundle\WebToPrintBundle\Event\Model\PrintConfigEvent;
Expand Down Expand Up @@ -89,10 +90,14 @@ public function getPdfFromString(string $html, array $params = [], bool $returnF

if ($gotenbergSettings) {
foreach (['header', 'footer'] as $item) {
if (key_exists($item, $gotenbergSettings) && $gotenbergSettings[$item] &&
file_exists($gotenbergSettings[$item])) {
if (
array_key_exists($item, $gotenbergSettings) &&
$gotenbergSettings[$item] &&
file_exists($gotenbergSettings[$item])
) {
$gotenbergSettings[$item . 'Template'] = $gotenbergSettings[$item];
}

unset($gotenbergSettings[$item]);
}

Expand All @@ -110,7 +115,7 @@ public function getPdfFromString(string $html, array $params = [], bool $returnF

['html' => $html, 'params' => $params] = $event->getArguments();

$tempFileName = uniqid('web2print_');
$tempFileName = uniqid('web2print_', false);

$chromium = GotenbergAPI::chromium(\Pimcore\Config::getSystemConfiguration('gotenberg')['base_url']);
// To support gotenberg-php v2 and so on
Expand Down Expand Up @@ -138,8 +143,11 @@ public function getPdfFromString(string $html, array $params = [], bool $returnF
}

// generateDocumentOutline is only available for gotenberg >= 8.14.0 and gotenberg-php >= v2.10.0
if (isset($params['generateDocumentOutline']) && $params['generateDocumentOutline']
&& method_exists($chromium, 'generateDocumentOutline')) {
if (
isset($params['generateDocumentOutline']) &&
$params['generateDocumentOutline']
&& method_exists($chromium, 'generateDocumentOutline')
) {
$chromium->generateDocumentOutline();
}

Expand Down Expand Up @@ -177,13 +185,20 @@ public function getPdfFromString(string $html, array $params = [], bool $returnF
$chromium->metadata($params['metadata']);
}

if (isset($params['assets'])) {
foreach ($params['assets'] as $asset) {
$chromium->assets($asset);
}
}

$request = $chromium->outputFilename($tempFileName)->html(Stream::string('processor.html', $html));

if ($returnFilePath) {
$filename = GotenbergAPI::save($request, PIMCORE_SYSTEM_TEMP_DIRECTORY);

return PIMCORE_SYSTEM_TEMP_DIRECTORY . DIRECTORY_SEPARATOR . $filename;
}

$response = GotenbergAPI::send($request);

return $response->getBody()->getContents();
Expand Down