我試圖用Rails生成PDF文件,但是當我這樣做時我注意到我的系統CPU開始最大化。最初,它將從大約2.5%上升,然後在穩定的時間段內增加到大約65%-80%,然後在我的頁面的iframe中顯示PDF之前,最終達到最大值。這裏有一些消息監視我的系統上的內存使用情況,當我得到:使用Wicked_PDF在Rails中生成PDF時的高CPU使用率gem
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)
我已經安裝了我的PDF生成的寶石wicked_pdf (1.0.6)
和wkhtmltopdf-binary-edge (0.12.4.0)
。並且與每個代碼的過程如下:
控制器/關切/ pdf_player_reports.rb
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 => {:top => 7, :bottom => 7, :left => 6, :right => 0},
encoding: 'utf8'
end
end
播放器/ director_summary_report.pdf.erb
<div class="document" style="margin-top: -63px;">
<% @players.each do |player| %>
<% reports = player.reports.order(created_at: :desc) %>
<% if player.is_college_player? %>
<%= render partial: 'college_director_report.html.erb', player: player %>
<% else %>
<%= render partial: 'pro_director_report.html.erb', player: player %>
<% end %>
<%= "<div class='page-break'></div>".html_safe %>
<% end %>
</div>
college_director_report。 html.erb
<%= wicked_pdf_stylesheet_link_tag "application", media: "all" %>
<%= wicked_pdf_javascript_include_tag "application" %>
<% provide(:title, "#{player.football_name}") %>
<% self.formats = [:html, :pdf, :css, :coffee, :scss] %>
<style>
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>tbody>tr:nth-child(odd)>td,
tr.found{
background-color:#e2e0e0 !important;
}
</style>
<div class="row">
<div class="col-xs-6">
<span>DIRECTOR SUMMARY</span>
</div>
<div class="col-xs-6 text-right">
<%= "#{player.full_name}/#{player.school.short_name}".upcase %>
<h1><%= "#{player.full_name(true)} (#{player.school.code})".upcase %></h1>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<%= render 'directors_report_player_header', player: player %>
<%= render 'directors_report_workouts', player: player %>
<%= render 'directors_report_grades', player: player %>
<%= render 'legacy_directors_report_contacts', player: player %>
</div>
</div>
directors_report_player_header.html.erb
<table class="table table-condensed table-bordered">
<thead>
<tr>
<th>Name</th>
<th>School</th>
<th>#</th>
<th>Position</th>
</tr>
</thead>
<tbody>
<tr>
<td><%= player.full_name(true) %></td>
<td><%= player.school.short_name %></td>
<td><%= player.jersey %></td>
<td><%= player.position.abbreviation %></td>
</tr>
</tbody>
</table>
UPDATE
我用下面和CPU%是什麼最終杏,如下圖所示跑了一個例子PDF生成器...
<table class="table table-condensed">
<thead>
<th>Number</th>
</thead>
<tbody>
<% (1..60000).each do |number| %>
<tr>
<td><%= number %></td>
</tr>
<% end %>
</tbody>
</table>
你在哪裏舉辦? – jvillian
@jvillian,我在內部Ubuntu服務器上託管14.04 LTS服務器,帶有1CPU,16GB,但是我的本地機器上運行的是最新的Ubuntu桌面版本8GB,結果相同 – daveomcd
在我的情況中,我很好當地的,但在Heroku有一個記憶失控。罪魁禍首是wkhtmltopdf-binary。我切換到wkhtmltopdf-heroku,這一切都理順了。也許看看它? – jvillian