<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jeroen van den Enden on Medium]]></title>
        <description><![CDATA[Stories by Jeroen van den Enden on Medium]]></description>
        <link>https://medium.com/@endroid?source=rss-516f0fec607e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3JN3ZqFiWl_18st7ayfuig.png</url>
            <title>Stories by Jeroen van den Enden on Medium</title>
            <link>https://medium.com/@endroid?source=rss-516f0fec607e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 20:21:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@endroid/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[PDF Generation in Symfony]]></title>
            <link>https://medium.com/@endroid/pdf-generation-in-symfony-3080702353b?source=rss-516f0fec607e------2</link>
            <guid isPermaLink="false">https://medium.com/p/3080702353b</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[pdf]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[symfony]]></category>
            <dc:creator><![CDATA[Jeroen van den Enden]]></dc:creator>
            <pubDate>Wed, 06 Jun 2018 12:08:52 GMT</pubDate>
            <atom:updated>2023-10-10T18:45:57.330Z</atom:updated>
            <content:encoded><![CDATA[<p>During my day to day work as a Symfony developer every now and then I am asked to build a PDF. Not my main specialism (as you all know my real specialisms are cache invalidation and naming things) but having great tools like wkhtmltopdf and libraries like Snappy at your disposal you should only need to focus on the PDF contents, or…</p><p>I found that generating PDFs still involves a lot of boiler plate code.</p><ul><li>More complex PDFs can consist of a cover, a table of contents, a header, a footer and of course the main content, often divided in sections</li><li>Each of these parts can originate from many different sources: some need to be rendered by a controller (as we mainly handle HTML), rendered by a templating engine or loaded from a file or URL</li><li>Contents often need to be cached for performance reasons</li><li>External resources like fonts, stylesheets and scripts can cause issues when generating many pages</li><li>I like coding but I don&#39;t like doing things over and over again</li><li>I have three kids and I want to see them grow up</li></ul><p>These things are not covered by most PDF generation libraries so I decided to add my own abstraction layers on top of wkhtmltopdf and Snappy to make handling the use cases above easier.</p><p>My approach involves three parts: an asset factory, an embed extension and finally a PDF builder. This post will explain what these components do and how you can utilize them to make your work less repetitive and allow you to focus on the real PDF contents.</p><h3>Loading PDF contents</h3><p>One of the first questions you ask when you build a PDF is: where does your content come from? And with content I do not only mean the main contents but also the cover, table of contents, header and footer. For instance: you might want the cover to be a PDF file that is merged, the header and footer to be static html files and the contents to be rendered by a controller. Any of these cases involve extra coding before you can pass the result to snappy, though the end result is clear and the logic for loading file, URL or controller contents is generally pretty straightforward. We don’t want to write code like this over and over again.</p><p>The <a href="https://github.com/endroid/asset">endroid/asset</a> library helps by creating an abstraction layer for retrieving these contents. We just define an asset for any type of resource and pass it to the service needing the output. For instance the FileAsset loads a file, the UrlAsset loads data from a URL and the ControllerAsset renders a controller. Then the receiving service can simply call $asset-&gt;getData() and the data is resolved behind the curtains. You can define a controller asset like this.</p><pre>$asset = new ControllerAsset(<br>    $kernel,<br>    $requestStack,<br>    ContentController::class,<br>    [&#39;locale&#39; =&gt; &#39;en&#39;],<br>);</pre><h4>Asset factory</h4><p>As you can see, creating an asset can still be a tedious process. For instance, each controller asset needs the kernel and request stack to generate the response and each template asset needs a renderer.</p><p>To make asset creation easier an asset factory is introduced that delegates asset creation to so called factory adapters, where each adapter makes sure the necessary services are injected. For instance the ControllerAssetFactory has a reference to the kernel and passes it to all controller assets it generates.</p><p>Now you can create the same controller asset using the factory like this.</p><pre>$controllerAsset = $assetFactory-&gt;create([<br>    &#39;controller&#39; =&gt; ContentController::class,<br>    &#39;parameters&#39; =&gt; [&#39;locale&#39; =&gt; &#39;en&#39;],<br>]);</pre><h4>Guesser</h4><p>Given the configuration above, the asset factory has to find a way to know what type of asset you are building. This is achieved by a so called guesser. When no explicit type is specified, the guesser looks through all registered factory adapters (while adhering to a guessing priority) and the first adapter matching the configuration is used.</p><h4>Caching</h4><p>When you need to cache some data you’d normally write some logic to retrieve the cache item, check for a hit or save the data.</p><pre>$cacheItem = $this-&gt;cache-&gt;getItem($key);</pre><pre><strong>if </strong>(!$cacheItem-&gt;isHit()) {<br>    $cacheItem-&gt;set($data);<br>    $cacheItem-&gt;tag($this-&gt;tags);<br>    $cacheItem-&gt;expiresAfter($this-&gt;expiresAfter);<br>    $this-&gt;cache-&gt;save($cacheItem);<br>}</pre><pre><strong>return </strong>$cacheItem-&gt;get();</pre><p>The endroid/asset library comes with a CacheAsset that can wrap any other asset and cache it according to modern cache standards. You can provide a key, one or more tags (so you can purge) and a lifetime. Most of the time you don’t need more than this for caching.</p><pre>$cacheAsset = $assetFactory-&gt;create([<br>    &#39;controller&#39; =&gt; ContentController::class,<br>    &#39;parameters&#39; =&gt; [&#39;locale&#39; =&gt; &#39;en&#39;],<br>    &#39;cache_key&#39; =&gt; &#39;content&#39;,<br>]);</pre><p>As you can see the logic for obtaining the controller contents and caching are obfuscated and internal dependencies are automatically resolved, leaving you with a simple and clean configuration.</p><p>Here the asset factory is used in the context of generating a PDF but you can use this as a provider for any service that requires content to be loaded from different sources.</p><p><a href="https://github.com/endroid/asset">endroid/asset</a></p><h3>Embedding external resources</h3><p>As mentioned earlier, use of external resources can introduce issues. For instance when you generate a large PDF containing headers and footers, these are generated for every page in your PDF. So if your PDF has 400 pages, the header and footer are processed 400 times each so they are rendered with the correct page number and section name.</p><p>When your header or footer requires external resources like stylesheets or fonts, this can result in a lot of requests. Also, PDF generators are often executed from the command line where loading URLs might be problematic.</p><p>In other words: you want to avoid external request as much as possible during PDF generation. The easiest way to do this is by making sure the resources are already embedded in your contents instead of loading them. Fortunately, for web assets we have the ability to create data URIs for practically any location where you would normally specify a URL. For instance you can put the data URI in an img src attribute, a href attribute or use it instead of an URL in embedded stylesheets.</p><p>However, generating a data URI from a resource requires some coding: we need to load the contents and then pass it as a variable to the template. Here the <a href="https://github.com/endroid/embed">endroid/embed</a> library comes into play: it provides a Twig extension that does this automatically for you. You only need to use the embed method around your external resource URL and it automatically replaces it with the encoded contents.</p><p><a href="https://github.com/endroid/embed">endroid/embed</a></p><h3>Building the PDF</h3><p>The asset factory and embed extension make work a lot easier but when you generate the PDF you still need to create the assets via the asset factory and pass them to the right options in Snappy. To improve this workflow a PDF builder is created around Snappy and the asset factory. This builder accepts option arrays, passes these to the asset factory and sets the correct options in Snappy. This way you can do everything via one single service. In the end building a PDF looks something like this.</p><pre><strong>&lt;?php<br></strong><br><strong>namespace </strong>App\Controller\Pdf;<br><br><strong>use </strong>Endroid\Pdf\Builder\PdfBuilder;<br><strong>use </strong>Endroid\Pdf\Response\InlinePdfResponse;<br><strong>use </strong>Symfony\Component\HttpFoundation\Response;<br><strong>use</strong> Symfony\Component\HttpKernel\Attribute\AsController;<br><strong>use </strong>Symfony\Component\Routing\Annotation\Route;</pre><pre>#[AsController]<br><strong>final readonly class </strong>GenerateController<br>{<br>    <em>#[Route(&#39;/pdf&#39;)]<br>    </em><strong>public function </strong>__invoke(PdfBuilder $builder): Response<br>    {<br>        $builder<br>            -&gt;setCover([<br>                &#39;controller&#39; =&gt; CoverController::<strong>class</strong>,<br>                &#39;cache_key&#39; =&gt; &#39;cover&#39;,<br>            ])<br>            -&gt;setTableOfContents([<br>                &#39;file&#39; =&gt; &#39;table_of_contents.xml&#39;,<br>                &#39;cache_key&#39; =&gt; &#39;toc&#39;,<br>            ])<br>            -&gt;setHeader([<br>                &#39;template&#39; =&gt; &#39;pdf/header.html.twig&#39;,<br>                &#39;cache_key&#39; =&gt; &#39;header&#39;,<br>            ])<br>            -&gt;setFooter([<br>                &#39;template&#39; =&gt; &#39;pdf/footer.html.twig&#39;,<br>                &#39;cache_key&#39; =&gt; &#39;footer&#39;,<br>            ])<br>            -&gt;setContent([<br>                &#39;controller&#39; =&gt; ContentController::<strong>class</strong>,<br>                &#39;cache_key&#39; =&gt; &#39;content&#39;,<br>            ])<br>        ;<br><br>        <strong>return new </strong>InlinePdfResponse($builder-&gt;getPdf());<br>    }<br>}</pre><p>Please note you can override any of the existing Snappy options via $builder-&gt;setOptions([…]) so you still have full control. Also the library provides an InlinePdfResponse for rendering your PDF in a browser and automatically handles scenarios where you want a margin on you content pages but you don&#39;t want that margin on the cover page (a limitation of wkhtmltopdf).</p><p>Thank you for reading :) As I mentioned, this approach helps me build PDFs much quicker and I hope I helped someone by sharing this. If you have any comments or questions let me know.</p><p><a href="https://github.com/endroid/pdf">endroid/pdf</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3080702353b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Auto package configuration for Symfony]]></title>
            <link>https://medium.com/@endroid/auto-package-configuration-for-symfony-e14780e29d81?source=rss-516f0fec607e------2</link>
            <guid isPermaLink="false">https://medium.com/p/e14780e29d81</guid>
            <category><![CDATA[symfony4]]></category>
            <category><![CDATA[composer]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[symfony]]></category>
            <dc:creator><![CDATA[Jeroen van den Enden]]></dc:creator>
            <pubDate>Fri, 20 Apr 2018 06:47:37 GMT</pubDate>
            <atom:updated>2024-02-07T12:28:17.466Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently, I decided to write a custom installer (Composer plugin) for my own packages instead of going with Symfony Flex (which I still use of course). In this article I&#39;ll explain why. For me, it made configuration management a lot easier so I wanted to share my experiences.</p><p>Thanks to Symfony Flex we can spend less time on package configuration and focus more on building applications and libraries. Flex operates as a <strong>Composer plugin</strong> that executes so called <strong>recipes</strong> to copy package configuration and routing, add sections to .env and .gitignore and more.</p><p>As a developer you can contribute to Flex by creating a pull request on the <strong>symfony/recipes </strong>or<strong> symfony/recipes-contrib</strong> repository for any package you like to be auto configured. All you have to do is create a manifest file and provide the configuration files you like to be installed.</p><p>When you create the pull request it is checked by the symfony-flex-server bot and one of the authorized reviewers reviews your changes. Once the pull request is approved by the reviewer, it can be merged.</p><p>Great to have such a tool. However, there are also some downsides to this approach. For instance, even with good average response times it can <strong>take a while</strong> before your recipe or update is merged and publicly available and you have <strong>limited control</strong> over whether the recipe will eventually be merged.</p><p>The reason pull requests can not be merged without a reviewer is that one of the goals of Flex is being a reliable source that offers <strong>high quality </strong>packages, which is a reasonable policy.</p><p>I think open discussions are useful but I personally don’t like the idea that I need to ask and wait for approval for every change in my configuration.</p><p>Other (minor) drawbacks are the fact that versions and configuration files are managed in a separate repository and recipes depend on the recipe server.</p><p>For instance recently when the server was unreachable my packages were installed without the recipes being executed. Not a big issue as you can still copy or create the files yourself, but it made me wonder if I really need my package configuration to be installed from a separate server.</p><p>And regarding version management: when you release a new version requiring a different configuration, make sure you create a new sub folder inside your recipe and have it approved and merged before you bump your package version or users might experience failing installations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba5ebc33741c0e013ece74bc55e79687/href">https://medium.com/media/ba5ebc33741c0e013ece74bc55e79687/href</a></iframe><p>In order to leverage control you have the possibility to use the <strong>copy-from-package</strong> directive. You still need a hosted, versioned and approved recipe, but at least you can keep part of the configuration inside your library.</p><p>You can also set up your <strong>own</strong> <strong>recipe server</strong> to further increase control but this comes with the cost of maintaining a separate hosted environment.</p><p>Because I desire <strong>full control</strong> over the configuration I ship with my packages, like updates to be <strong>immediately available </strong>and want the configuration version to <strong>always be in sync </strong>with the currently installed version and <strong>not depend on a recipe server</strong>, I decided to create a small Composer plugin myself that allows you to put the configuration inside your package next to your code.</p><p>My packages are bundled with an installer that does the following.</p><ol><li>Detect the type of project (i.e. Symfony 6.4 / 7.x)</li><li>Copy files from .install/&lt;projectType&gt;/ to your project</li><li>Allow you to exclude specific packages from installation</li><li>Allow you to disable the installer completely</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*5dd5yPnfThVBQjn5u_XJOw.png" /><figcaption>Example composer install output</figcaption></figure><p>You can find the resulting installer <a href="https://github.com/endroid/installer">here</a>.</p><p><a href="https://github.com/endroid/installer">endroid/installer</a></p><p>Again, I do like Flex and recommend anyone to use it but I just want to point out the fact that Flex is not the only possible solution for auto configuration. Composer allows you to easily write or use any other plugins for configuring your packages. The installer mentioned here is just a basic example.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e14780e29d81" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>