<p>Yes, generate those PDF files ahead of time and then store or cache them. With the right approach, you only ever need to generate each unique combination of PDFs once.</p>
<p>The best approach depends on how the combination of files is determined, your code example doesn't show how the <code>pdfFiles</code> variable is defined.</p>
<h2>If the files are independent of the visitor</h2>
<p>If it's a set of files on an entry, or multiple entries, or any other source that is deterministic (i.e. not the result of a user interaction), you can pre-compute the generated PDF ahead of time. I would probably create a dedicated asset field to store the generated/combined PDF. Then use an <a href="https://craftcms.com/knowledge-base/handling-entry-saves" rel="nofollow noreferrer">event on <code>Entry::EVENT_AFTER_SAVE</code></a> to regenerate the PDF whenever the source entries are saved and store the resulting PDF in that field.</p>
<h2>If the combination is user-defined</h2>
<p>If the user can select which files/URLs are combined into a single PDF, I would implement a caching mechanism. First, store the generated PDF on the server - for example, in the <code>storage</code> folder or as an asset. Then, use the <a href="https://docs.craftcms.com/api/v5/craft-cache-filecache.html#public-methods" rel="nofollow noreferrer">cache service</a> to associate the stored PDF file with the unique combination of source files.</p>

CC BY-SA 4.0
2024-05-25T20:19:11.743Z
2024-05-25T20:19:11.743Z
MoritzLost
<p>I have a page that performs a merge of a series of PDF files through the integration of <a href="https://plugins.craftcms.com/api2pdf" rel="nofollow noreferrer">Api2pdf Craft Plugin</a> in a modified and working version for Craft 4.</p>
<p>The problem is that the file gets generated every time a visitor visits the page.
This rises costs on my part. How can I reduce the number of requests for the merge? Maybe have it generated everytime a user clicks a download link? Or have it generated once and then made available for the next visitors?</p>
<p>Here's the partial code... any idea?</p>
<pre><code>&lt;div class=&quot;w-full border-t-2 border-dashed pt-10&quot;&gt;
    {% set urls = pdfFiles %}
    {% set result = craft.api2pdf.merge(urls) %}
        {% if result and result.success %}
            &lt;h2 class=&quot;text-2xl expand pt-10 first:pt-0 border-b-1 border-black mb-20&quot;&gt;&lt;i class=&quot;fa fa-file-pdf-o text-slate-500&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;
                &lt;a href=&quot;{{ result.pdf }}&quot; class=&quot;text-secondary border-spacing-0 hover:text-primary&quot;&gt;
                        Download whole file&lt;/a&gt;
            &lt;/h2&gt;
        {% else %}
                {{ result.error }}
        {% endif %}
&lt;/div&gt;
</code></pre>

<craft4><pdf><api>
Reduce Api2PDF requests and store the first visit generated file
# Reduce Api2PDF requests and store the first visit generated file

(Asked by: username_0 on 2024-05-25)

I have a page that performs a merge of a series of PDF files through the integration of Api2pdf Craft Plugin in a modified and working version for Craft 4.
The problem is that the file gets generated every time a visitor visits the page.
This rises costs on my part. How can I reduce the number of requests for the merge? Maybe have it generated everytime a user clicks a download link? Or have it generated once and then made available for the next visitors?
Here's the partial code... any idea?

    {% set urls = pdfFiles %}
    {% set result = craft.api2pdf.merge(urls) %}
        {% if result and result.success %}
            
                
                        Download whole file
            
        {% else %}
                {{ result.error }}
        {% endif %}

**Tags:** craft4, pdf, api

---

## 1 answers

### Answer by username_1 on 2024-05-25. Score: 1

Yes, generate those PDF files ahead of time and then store or cache them. With the right approach, you only ever need to generate each unique combination of PDFs once.

The best approach depends on how the combination of files is determined, your code example doesn't show how the `pdfFiles` variable is defined.

## If the files are independent of the visitor

If it's a set of files on an entry, or multiple entries, or any other source that is deterministic (i.e. not the result of a user interaction), you can pre-compute the generated PDF ahead of time. I would probably create a dedicated asset field to store the generated/combined PDF. Then use an [event on `Entry::EVENT_AFTER_SAVE`](https://craftcms.com/knowledge-base/handling-entry-saves) to regenerate the PDF whenever the source entries are saved and store the resulting PDF in that field.

## If the combination is user-defined

If the user can select which files/URLs are combined into a single PDF, I would implement a caching mechanism. First, store the generated PDF on the server - for example, in the `storage` folder or as an asset. Then, use the [cache service](https://docs.craftcms.com/api/v5/craft-cache-filecache.html#public-methods) to associate the stored PDF file with the unique combination of source files.


---

