我使用pdfkit生成了一个pdf,但它既不是渲染图像也不是css文件。我不知道为什么会这样。任何帮助都会很明显。
布局:(report.html.erb)
<!DOCTYPE html>
<html lang='en'>
<head>
<%= stylesheet_link_tag 'merchant_report', rel: 'stylesheet'%>
<%= csrf_meta_tags %>
<%= favicon_link_tag 'favicon.ico' %>
</head>
<body>
<%= yield %>
</body>
</html>
行动:(report.pdf.erb)
<div class="checkin_data">
<h4 style="text-align: center;">No. of Customers Vs. Date</h4>
<div>No. of Checkins during this period on a day wise basis</div>
<% image_name = Time.now.strftime("%d-%m-%Y").to_s + '_' + $merchant.id.to_s + '_check_in_visit_history_image.png' %>
<!-- <img src="http://127.0.0.1:3000/system/report/#{image_name}"/> -->
<%= image_tag("/assets/images/report/#{image_name}")%>
<br>
<h4 style="text-align: center;">No. of Customers Vs. No. of Visits</h4>
<div>No. of Visits per Customer during this period</div>
<canvas height="400" width="600" id="customer_visit"></canvas>
<br>
</div>
控制器:(merchant_report_controller)
respond_to do |format|
format.html
format.pdf do
html = render_to_string(:layout => "report.html.erb" , :action => "report.pdf.erb")
kit = PDFKit.new(html)
kit.stylesheets << "#{Rails.root}/public/system/stylesheets/merchant_report.css"
send_data(kit.to_pdf, :filename => "some_name.pdf", :type => 'application/pdf', :disposition => 'inline')
return
end
end
pdfkit.rb
PDFKit.configure do |config|
config.wkhtmltopdf = '/usr/bin/wkhtmltopdf'
config.default_options = {
:page_size => 'Legal',
:print_media_type => true,
:disable_javascript => true
}
# Use only if your external hostname is unavailable on the server.
config.root_url = "http://localhost:3000"
config.verbose = false
end