<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: aethans</title>
    <description>The latest articles on DEV Community by aethans (@aethans).</description>
    <link>https://dev.to/aethans</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4166326%2Fee20e6bd-5cd5-426d-8614-a28c7990d4e7.png</url>
      <title>DEV Community: aethans</title>
      <link>https://dev.to/aethans</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aethans"/>
    <language>en</language>
    <item>
      <title>PHP PDF generation becomes slow when creating large documents</title>
      <dc:creator>aethans</dc:creator>
      <pubDate>Tue, 06 Oct 2026 11:58:42 +0000</pubDate>
      <link>https://dev.to/aethans/php-pdf-generation-becomes-slow-when-creating-large-documents-3hc</link>
      <guid>https://dev.to/aethans/php-pdf-generation-becomes-slow-when-creating-large-documents-3hc</guid>
      <description>&lt;p&gt;I am working on a PHP-based worksheet generation system and recently ran into a performance problem.&lt;/p&gt;

&lt;p&gt;The application generates printable PDF files based on user-selected options.&lt;/p&gt;

&lt;p&gt;The workflow is:&lt;/p&gt;

&lt;p&gt;User selects options&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Server generates data&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;PHP creates PDF&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;User downloads the file&lt;/p&gt;

&lt;p&gt;The problem is that the generation time increases significantly when the document contains many items.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;100+ exercises&lt;/li&gt;
&lt;li&gt;multiple pages&lt;/li&gt;
&lt;li&gt;answer pages&lt;/li&gt;
&lt;li&gt;custom layouts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Current implementation&lt;/p&gt;

&lt;p&gt;The frontend sends a POST request to the PDF generation endpoint:&lt;/p&gt;

&lt;p&gt;$result = http_post(&lt;br&gt;
    "&lt;a href="https://www.wativate.com/?api" rel="noopener noreferrer"&gt;https://www.wativate.com/?api&lt;/a&gt;",&lt;br&gt;
    [&lt;br&gt;
        "operation" =&amp;gt; "addition",&lt;br&gt;
        "questions" =&amp;gt; 100,&lt;br&gt;
        "include_answers" =&amp;gt; true&lt;br&gt;
    ]&lt;br&gt;
);&lt;/p&gt;

&lt;p&gt;The server receives the parameters and starts generating the PDF.&lt;/p&gt;

&lt;p&gt;A simplified version:&lt;/p&gt;

&lt;p&gt;function generateWorksheetPDF($options)&lt;br&gt;
{&lt;br&gt;
    $problems = generateProblems(&lt;br&gt;
        $options['questions']&lt;br&gt;
    );&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$html = renderHTML($problems);

return createPDF($html);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;The current approach works well for small files.&lt;/p&gt;

&lt;p&gt;However, when generating larger worksheets, I notice several problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High CPU usage&lt;/li&gt;
&lt;li&gt;Long response time&lt;/li&gt;
&lt;li&gt;Occasional timeout errors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;Small document:&lt;/p&gt;

&lt;p&gt;20 questions → 2 seconds&lt;/p&gt;

&lt;p&gt;Large document:&lt;/p&gt;

&lt;p&gt;200 questions → 20+ seconds&lt;/p&gt;

&lt;p&gt;Things I have tried&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Increasing PHP timeout&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;set_time_limit(120);&lt;/p&gt;

&lt;p&gt;This avoids some timeout problems, but it does not solve the performance issue.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Caching generated files&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I tried saving generated PDFs:&lt;/p&gt;

&lt;p&gt;$filename = md5(&lt;br&gt;
    json_encode($options)&lt;br&gt;
) . ".pdf";&lt;/p&gt;

&lt;p&gt;If the same options are requested again, the existing file can be reused.&lt;/p&gt;

&lt;p&gt;This helps repeated requests, but the first generation is still slow.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Moving PDF generation to background jobs&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I am considering changing the architecture.&lt;/p&gt;

&lt;p&gt;Current:&lt;/p&gt;

&lt;p&gt;HTTP Request&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Generate PDF&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Return File&lt;/p&gt;

&lt;p&gt;Possible:&lt;/p&gt;

&lt;p&gt;HTTP Request&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Create Task&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Queue Worker&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Generate PDF&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Download Later&lt;/p&gt;

&lt;p&gt;My questions&lt;/p&gt;

&lt;p&gt;For a PHP application that generates dynamic PDFs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Should PDF generation always be moved to background jobs?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Is it better to generate PDFs asynchronously even for small documents?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Are there better strategies for handling large HTML-to-PDF conversions?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Would splitting the document generation process into smaller tasks improve performance?&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I would appreciate any suggestions from developers who have built similar document generation systems.&lt;/p&gt;

</description>
      <category>pdf</category>
    </item>
  </channel>
</rss>
