title: High CPU Usage when generating PDFs in Rails with Wicked_PDF gem
body: <p>I'm trying to generate a PDF file with Rails, but when I do I notice my system CPU starts to max out.  Initially, it will go from ~2.5% then increase to ~65%-$80% for a steady period of time and then finally max out almost prior to displaying them PDF in my iframe on my page.  Here are some messages I get when monitoring the memory usage on my system:</p>
  
  <pre><code>Warning or critical alerts (lasts 9 entries)
                            2017-06-09 14:58:07 (0:00:04) - CRITICAL on CPU_SYSTEM (100.0)
                            2017-06-09 14:58:04 (0:00:13) - CRITICAL on CPU_USER (Min:72.8 Mean:83.3 Max:93.7)
                            2017-06-09 14:47:39 (0:00:06) - CRITICAL on CPU_USER (93.0)
                            2017-06-09 14:47:29 (0:00:04) - WARNING on CPU_SYSTEM (74.7)
                            2017-06-09 14:36:48 (0:00:04) - CRITICAL on CPU_SYSTEM (100.0)
                            2017-06-09 14:36:45 (0:00:10) - CRITICAL on CPU_IOWAIT (Min:78.6 Mean:85.7 Max:97.4)
                            2017-06-09 14:18:06 (0:00:04) - CRITICAL on CPU_SYSTEM (94.3)
                            2017-06-09 14:18:06 (0:00:07) - CRITICAL on CPU_USER (91.0)
  2017-06-09 15:01:14       2017-06-09 14:17:44 (0:00:04) - WARNING on CPU_SYSTEM (73.8)
  </code></pre>
  
  <p>The gems I have installed for my PDF generation are <code>wicked_pdf (1.0.6)</code> and <code>wkhtmltopdf-binary-edge (0.12.4.0)</code>.  And the process with code for each are as follows:</p>
  
  <p><strong>controllers/concerns/pdf_player_reports.rb</strong></p>
  
  <pre><code>def director_report_pdf
    @players = Player.where(id: params["player_ids"]
  
    respond_to do |format|
    format.html
    format.pdf do
      render pdf: "#{params['pdf_title']}",
        template: 'players/director_summary_report.pdf.erb',
          layout: 'print',
          show_as_html: params.key?('debug'),
          window_status: 'Loading...',
          disable_internal_links: true,
          disable_external_links: true,
          dpi: 75,
          disable_javascript: true,
          :margin =&gt; {:top =&gt; 7, :bottom  =&gt; 7, :left =&gt; 6, :right =&gt; 0},
          encoding: 'utf8'
    end
  end
  </code></pre>
  
  <p><strong>players/director_summary_report.pdf.erb</strong></p>
  
  <pre><code>&lt;div class="document" style="margin-top: -63px;"&gt;
    &lt;% @players.each do |player| %&gt;
       &lt;% reports = player.reports.order(created_at: :desc) %&gt;
       &lt;% if player.is_college_player? %&gt;
         &lt;%= render partial: 'college_director_report.html.erb', player: player %&gt;
       &lt;% else %&gt;
         &lt;%= render partial: 'pro_director_report.html.erb', player: player %&gt;
       &lt;% end %&gt;
       &lt;%= "&lt;div class='page-break'&gt;&lt;/div&gt;".html_safe %&gt;
    &lt;% end %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>college_director_report.html.erb</strong></p>
  
  <pre><code>&lt;%= wicked_pdf_stylesheet_link_tag "application", media: "all" %&gt;
  &lt;%= wicked_pdf_javascript_include_tag "application" %&gt;
  &lt;% provide(:title, "#{player.football_name}") %&gt;
  &lt;% self.formats = [:html, :pdf, :css, :coffee, :scss] %&gt;
  
  &lt;style&gt;
      thead { display: table-row-group; page-break-inside: avoid }
      tfoot { display: table-row-group; }
      /*thead:before, thead:after { display: none; }*/
      table { page-break-inside: avoid; }
      tr { page-break-inside: avoid; }
      .page-break {
          display:block; clear:both; page-break-after:always;
      }
      .keep-together { page-break-before: always !important; }
      .table-striped&gt;tbody&gt;tr:nth-child(odd)&gt;td,
      tr.found{
          background-color:#e2e0e0 !important;
      }
  &lt;/style&gt;
  
  &lt;div class="row"&gt;
      &lt;div class="col-xs-6"&gt;
          &lt;span&gt;DIRECTOR SUMMARY&lt;/span&gt;
      &lt;/div&gt;
      &lt;div class="col-xs-6 text-right"&gt;
          &lt;%= "#{player.full_name} / #{player.school.short_name}".upcase %&gt;
          &lt;h1&gt;&lt;%= "#{player.full_name(true)} (#{player.school.code})".upcase %&gt;&lt;/h1&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="row"&gt;
    &lt;div class="col-xs-12"&gt;
      &lt;%= render 'directors_report_player_header', player: player %&gt;
      &lt;%= render 'directors_report_workouts', player: player %&gt;
      &lt;%= render 'directors_report_grades', player: player %&gt;
      &lt;%= render 'legacy_directors_report_contacts', player: player %&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>directors_report_player_header.html.erb</strong></p>
  
  <pre><code>&lt;table class="table table-condensed table-bordered"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Name&lt;/th&gt;
              &lt;th&gt;School&lt;/th&gt;
              &lt;th&gt;#&lt;/th&gt;
              &lt;th&gt;Position&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;%= player.full_name(true) %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= player.school.short_name %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= player.jersey %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= player.position.abbreviation %&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I ran an example PDF generator using the following and the CPU% is what ends up maxing out as shown below...</p>
  
  <p><a href="https://i.stack.imgur.com/bDaBL.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bDaBL.jpg" alt="enter image description here"></a></p>
  
  <pre><code> &lt;table class="table table-condensed"&gt;
      &lt;thead&gt;
        &lt;th&gt;Number&lt;/th&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;% (1..60000).each do |number| %&gt;
          &lt;tr&gt;
            &lt;td&gt;&lt;%= number %&gt;&lt;/td&gt;
          &lt;/tr&gt;
        &lt;% end %&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  </code></pre>