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

Include image in test PDF generation #68

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added public/img/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 28 additions & 21 deletions src/Processor/Gotenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
use Pimcore\Bundle\WebToPrintBundle\Model\Document\PrintAbstract;
use Pimcore\Bundle\WebToPrintBundle\Processor;
use Pimcore\Logger;
use function array_merge;
use function file_exists;
use function json_decode;
use function key_exists;

class Gotenberg extends Processor
{
Expand All @@ -32,35 +36,15 @@ class Gotenberg extends Processor
*/
protected function buildPdf(PrintAbstract $document, object $config): string
{
$web2printConfig = Config::getWeb2PrintConfig();
$gotenbergSettings = $web2printConfig['gotenbergSettings'];
$gotenbergSettings = json_decode($gotenbergSettings, true);

$params = ['document' => $document];
$this->updateStatus($document->getId(), 10, 'start_html_rendering');
$html = $document->renderDocument($params);

$params['hostUrl'] = 'http://nginx:80';
if (!empty($web2printConfig['gotenbergHostUrl'])) {
$params['hostUrl'] = $web2printConfig['gotenbergHostUrl'];
}

$html = $this->processHtml($html, $params);
$this->updateStatus($document->getId(), 40, 'finished_html_rendering');

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

try {
$this->updateStatus($document->getId(), 50, 'pdf_conversion');
$pdf = $this->getPdfFromString($html, $gotenbergSettings ?? []);
$pdf = $this->getPdfFromString($html);
$this->updateStatus($document->getId(), 100, 'saving_pdf_document');
} catch (\Exception $e) {
Logger::error((string) $e);
Expand Down Expand Up @@ -92,6 +76,29 @@ public function getProcessingOptions(): array
*/
public function getPdfFromString(string $html, array $params = [], bool $returnFilePath = false): string
{
$web2printConfig = Config::getWeb2PrintConfig();

$processParams = [
'hostUrl' => $web2printConfig['gotenbergHostUrl'] ?? 'http://nginx:80',
];

$html = $this->processHtml($html, $processParams);

$gotenbergSettings = $web2printConfig['gotenbergSettings'] ?? '';
$gotenbergSettings = json_decode($gotenbergSettings, true);

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

$params = array_merge($params, $gotenbergSettings);
}

$params = $params ?: $this->getDefaultOptions();

$event = new PrintConfigEvent($this, [
Expand Down
5 changes: 5 additions & 0 deletions templates/settings/test_web2print.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,10 @@
<h4>Background Test</h4>
<p id="with-background">This text could have a background color depending on configured settings.</p>


<h4>Image Test</h4>
<img src="{{ asset('bundles/pimcorewebtoprint/img/logo.png') }}">


</body>
</html>