Skip to content

Commit

Permalink
Include image in test PDF generation
Browse files Browse the repository at this point in the history
  • Loading branch information
brusch committed Apr 4, 2024
1 parent 958eb0b commit f31f100
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
Binary file added public/img/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 29 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,30 @@ public function getProcessingOptions(): array
*/
public function getPdfFromString(string $html, array $params = [], bool $returnFilePath = false): string
{
$web2printConfig = Config::getWeb2PrintConfig();

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

$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>

0 comments on commit f31f100

Please sign in to comment.