Skip to content

Commit

Permalink
Add assets DIR with Package
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenRenaux committed Feb 22, 2024
1 parent ecb9226 commit 28c32ad
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
6 changes: 6 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Sensiolabs\GotenbergBundle\Asset\GotenbergPackage;
use Sensiolabs\GotenbergBundle\Client\GotenbergClient;
use Sensiolabs\GotenbergBundle\Pdf\Gotenberg;
use Sensiolabs\GotenbergBundle\Twig\GotenbergAssetExtension;
Expand Down Expand Up @@ -31,7 +32,12 @@
$services->alias(GotenbergClient::class, 'sensiolabs_gotenberg.client')
->private();

$services->set('sensiolabs_gotenberg.asset.package', GotenbergPackage::class)
->args([param('kernel.project_dir')])
->alias(GotenbergPackage::class, 'sensiolabs_gotenberg.asset.package');

$services->set('sensiolabs_gotenberg.twig.asset_extension', GotenbergAssetExtension::class)
->args([service('sensiolabs_gotenberg.asset.package')])
->tag('twig.extension')
;
};
19 changes: 6 additions & 13 deletions docs/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ already set in your ``sensiolabs_gotenberg.yml``.
Additional Assets
-----------------

You can reference any file (like images, fonts, stylesheets, and so on...)
from within a template.
If a template needs to link to a static asset (e.g. an image), this bundle provides an gotenberg_asset()
Twig function to help generate that URL.

.. warning::
The only requirement is that their paths in the template file are on the root level.
This function work as `asset() Twig function`_.

.. code-block:: html

Expand All @@ -32,28 +31,21 @@ from within a template.
<title>PDF body</title>
</head>
<body>
<img src="ceo.jpeg" />
<img src="admin.jpeg" />
<img src="{{ gotenberg_asset('ceo.jpeg') }}" alt="CEO"/>
<img src="{{ gotenberg_asset('img/admin.jpeg') }}" alt="Admin"/>
<main>
<h1>Hello world!</h1>
</main>
</body>
</html>

When building the PDF, you just have to use the ``assets`` method to add any
asset referenced in the template to the request.

.. code-block:: php
use Sensiolabs\GotenbergBundle\Pdf\Gotenberg;
$twigPdfBuilder = $gotenberg->twig();
$twigPdfBuilder
->content('path/to/template.html.twig')
->assets(
'assets/images/profiles/ceo.jpeg',
'assets/images/profiles/admin.jpeg',
)
->generate()
.. tip::
Expand Down Expand Up @@ -339,6 +331,7 @@ Enable PDF for Universal Access for optimal accessibility.

For more information about `pdf formats`_.

.. _asset() Twig function: https://symfony.com/doc/current/templates.html#linking-to-css-javascript-and-image-assets
.. _assets: https://gotenberg.dev/docs/routes#html-file-into-pdf-route
.. _defaults properties: https://gotenberg.dev/docs/routes#page-properties-chromium
.. _Header and footer: https://gotenberg.dev/docs/routes#header--footer
Expand Down
15 changes: 15 additions & 0 deletions src/Asset/GotenbergPackage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Sensiolabs\GotenbergBundle\Asset;

readonly class GotenbergPackage
{
public function __construct(private string $projectDir)
{
}

public function getUrl(string $path): string
{
return $this->projectDir.'/public/'.$path;
}
}
2 changes: 1 addition & 1 deletion src/Builder/BuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function footer(string $path, array $context = []): self
public function assets(string ...$pathToAssets): static
{
foreach ($pathToAssets as $filePath) {
$file = new DataPartFile($this->resolveFilePath($filePath));
$file = new DataPartFile($filePath);
$dataPart = new DataPart($file);

$this->multipartFormData[] = [
Expand Down
12 changes: 10 additions & 2 deletions src/Twig/GotenbergAssetExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

namespace Sensiolabs\GotenbergBundle\Twig;

use Sensiolabs\GotenbergBundle\Asset\GotenbergPackage;
use Sensiolabs\GotenbergBundle\Builder\AssetAwareBuilderInterface;
use Symfony\Component\Mime\Part\File;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class GotenbergAssetExtension extends AbstractExtension
final class GotenbergAssetExtension extends AbstractExtension
{
private GotenbergPackage $packages;

public function __construct(GotenbergPackage $packages)
{
$this->packages = $packages;
}

public function getFunctions(): array
{
return [
Expand All @@ -27,7 +35,7 @@ public function getAssetUrl(array $context, string $path): string
throw new \LogicException();
}

$builder->assets($path);
$builder->assets($this->packages->getUrl($path));

return (new File($path))->getFilename();
}
Expand Down

0 comments on commit 28c32ad

Please sign in to comment.