-
-
Notifications
You must be signed in to change notification settings - Fork 84
encore_entry_css_files returns nothing if called multiple time #33
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
Comments
Ok I found why: webpack-encore-bundle/src/Asset/EntrypointLookup.php Lines 63 to 66 in 9c89795
There is certainly a reason, maybe for tag rendering? But for that use case, it should have an option to force duplicate returns IMO. |
The workaround to this is to inject the entrypoint lookup and reset it before each twig render call. See my complete service: final class PdfRenderer
{
/**
* @var EngineInterface
*/
private $twig;
/**
* @var Dompdf
*/
private $pdf;
/**
* @var TranslatorInterface&LocaleAwareInterface
*/
private $translator;
/**
* @var EntrypointLookupInterface
*/
private $encoreEntrypointLookup;
public function __construct(EngineInterface $twig, Dompdf $pdf, TranslatorInterface $translator, EntrypointLookupInterface $encoreEntrypointLookup)
{
Assert::isInstanceOf($translator, LocaleAwareInterface::class);
$this->twig = $twig;
$this->pdf = $pdf;
$this->translator = $translator;
$this->encoreEntrypointLookup = $encoreEntrypointLookup;
}
/**
* Generates the HTML code with Twig and pushes it to the PDF generator.
*
* @param mixed[] $parameters
*/
public function renderPdf(string $template, array $parameters, ?string $locale = null): string
{
// We have to get a new Dompdf instance to avoid this issue: https://github.com/dompdf/dompdf/issues/1056
// We will be able to fallback to the properties once resolved and released.
// You can test it again using the nexy:invoice:remind command. ATM, it fails generating multiple reminders.
$pdf = new Dompdf($this->pdf->getOptions());
$fallbackLocale = $this->translator->getLocale();
if (null !== $locale) {
$this->translator->setLocale($locale);
}
$this->encoreEntrypointLookup->reset();
$pdf->loadHtml($this->twig->render($template, $parameters));
$pdf->render();
$this->translator->setLocale($fallbackLocale);
return (string) $pdf->output();
}
} |
@soullivaneuh how did you get the Only adding the parameter to the constructor throws an exception
As per de recommendation in the exception message, I added an alias but that doesn't seem to work, it seems the container just tries to create a new services:
_defaults:
autowire: true
autoconfigure: true
public: false
Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface:
alias: 'webpack_encore.entrypoint_lookup'
How did you get this to work ? |
It seems aliasing So with services:
_defaults:
autowire: true
autoconfigure: true
public: false
Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface:
alias: 'webpack_encore.entrypoint_lookup[_default]' # this is the important line The injection of |
See #45 - I think |
Thanks !! this helped out a lot :) |
Is it supposed to be a wanted behaviour ? |
Yes it is. |
Why ? |
I don't know, sureley to prevent importing the same files multiples times which can lead to undesirable behaviour. Also, it will probably be usefull to have a way to reset the entrypoint lookup directly on Twig. WDYT @weaverryan? |
I'm sorry but I don't understand how you can be sure it's a 100% wanted behaviour without knowing the reason why ? There is probably a better reason (or it's an issue dressed as a feature) |
I'm using this function to render PDF with inline CSS as it was suggested in #16 (comment):
<style>{{ asset_get_content(encore_entry_css_files('pdf')) }}</style>
But if I call the twig
render
method multiple time (to render multiple PDFs on one command), only the first will get CSS.For the other, the css file list is just empty.
So I tried this for test on my homepage:
And it confirms the issue:
To me, it's a bug. Why this function would not return the needed files if call multiple times?
The text was updated successfully, but these errors were encountered: