我正在使用Rails 4,Wicked_PDF和Chartkick Gem的
Google图表我使用:
<%= javascript_include_tag"//www.google.com/jsapi","chartkik"%>
html视图提供了图表和所有预期的内容.当我将.pdf附加到网址时,pdf文档会在浏览器中显示,但ChartKick图表不会显示.
图表应出现以下错误:
加载图表时出错:找不到适配器
我在PDFKit文档中找到了以下内容.
资源不包含在PDF中:图像,CSS或JavaScript似乎没有在PDF中正确下载.这是因为wkhtmltopdf不知道在哪里找到这些文件.确保使用绝对路径(以正斜杠开头)到您的资源.如果您使用PDFKit从原始HTML源生成PDF,请确保使用完整路径(包括域的文件路径或URL).在限制性服务器环境中,root_url配置可能是您要更改资产主机的内容.
我假设wkhtmltopdf没有找到图表的链接,但我不知道如何解决这个问题.
有没有人有建议?
我找到了这个链接:
在wicked_pdf中渲染jQuery
Unixmonkey帮助FattRyan为Highcharts解决这个问题.
任何人都可以帮助如何设置此wicked_pdf_javascript_include_tag,以便Wicket_PDF可以使用Google图表接受Chartkick的图表吗?
我一直在使用wicked_pdf在Rails中生成一些PDF,并且它在我的开发环境中工作正常,但是当我尝试在我的生产环境中生成一个错误时,我得到500错误(但我的日志中没有特定错误).我注意到的第一件事是wkhtmltopdf二进制文件位于我的生产盒上的不同位置,所以我将以下内容添加到我的wicked_pdf.rb初始化程序中:
if Rails.env == "production"
WickedPdf.config = {
:exe_path => '/usr/bin/wkhtmltopdf'
}
end
Run Code Online (Sandbox Code Playgroud)
这是我在控制器中调用它的方式:
def certificate
@inspection = Inspection.find(params[:id])
@council = Council.find(@inspection.councilid)
respond_to do |format|
format.pdf do
render :pdf => @inspection.slug,
:show_as_html => params[:debug].present?,
:margin => {:top => 0,
:bottom => 0,
:left => 0,
:right => 0}
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是我的观点的内容:
# certificate.pdf.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
body {
margin: 0;
padding: 0; …Run Code Online (Sandbox Code Playgroud) 现在我正在使用rails 3.0.0.i我正在使用Ubuntu 11.10,64位os.i想要安装wkhtmltopdf.please告诉我静态版本的wkhtmltopdf.
ruby-on-rails ruby-on-rails-plugins wkhtmltopdf ruby-on-rails-3 wicked-pdf
现在我已经:header => {:html => {:template => "layouts/pdf_header.html.erb"}}把事情做好了。我可以传递一些仅在文档第一页上显示标题的内容吗?
我也可以对页脚执行此操作吗?这在这里几乎更重要,因为当每次长度和页数不同时,我真的想不出任何其他方法可以让页脚动态地粘在页面底部。
我正在使用Wicked PDF,并且在单击按钮时在新选项卡中生成了pdf。但是我想通过单击按钮自动打印pdf 。我添加了这个print_media_type并将值设置为true,我希望我的打印机选项可以开始工作,但是什么也不会发生!有什么想法吗?
respond_to do |format|
format.html
format.pdf do
render pdf: "file_name", :template => 'officials/individual_receipt_export.html.erb', encoding: 'utf8',page_size: 'A4',:print_media_type => true
end
end
Run Code Online (Sandbox Code Playgroud) 我有一些复杂的PDF生成逻辑,需要在控制器外部渲染视图,然后将HTML传递到WickedPDF:
ActionView::Base.send(:define_method, :protect_against_forgery?) { false }
av = ActionView::Base.new
av.view_paths = ActionController::Base.view_paths
income_statement_html = av.render :template => "reports/income_statement.pdf.erb", :layout => 'layouts/report.html.erb',
locals: {:@revenue_accounts => revenue_accounts,
:@expense_accounts => expense_accounts,
:@start_date => start_date,
:@end_date => end_date,
:@business => business}
Run Code Online (Sandbox Code Playgroud)
这一切都适用于Rails 4,但在我们升级到Rails 5时已停止工作.
我们在这里设置的所有实例变量最终都nil在视图中.还有一种方法可以像这样从渲染调用中设置实例变量吗?
我正在使用 wicked_pdf gem,目前正在尝试加快我的规范套件的速度。
我意识到我的一些规范正在我的 tmp 文件夹中生成 PDF,这非常耗时。
有没有办法完全存根 wicked_pdf 生成,因此它实际上不会生成 PDF?
我有一个场景,我需要根据对象数组生成多页 pdf。
以下是我的模型:
# Member has_many certificates
class Member < ActiveRecord::Base
has_many :certificates, dependent: :destroy
before_save :set_expiry
def self.delete_expired
where(expire_at: Date.today).destroy_all
end
private
def set_expiry
self.expire_at = 15.days.from_now
end
end
# certificate belongs_to member
class Certificate < ActiveRecord::Base
belongs_to :member
belongs_to :certificate_type
belongs_to :company
def company_name
company.name
end
def page_orientation
company_name == 'CompanyA' ? 'Landscape' : 'Portrait'
end
def page_height
company_name == 'CompanyA' ? '10.5in' : '11in'
end
def page_width
company_name == 'CompanyA' ? '7in' : '8.5in'
end
end
# …Run Code Online (Sandbox Code Playgroud) 使用chartkick带wicked-pdf。
应用程序/布局/pdf.html.erb
<!doctype html>
<html>
<head>
<meta charset='utf-8' />
<%= wicked_pdf_stylesheet_link_tag 'application', media: 'all' %>
<%= wicked_pdf_javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= javascript_include_tag "http://www.google.com/jsapi"%>
<%= wicked_pdf_javascript_include_tag 'chartkick'%>
</head>
<body onload='number_pages'>
<div id="header">
<h2>Report</h2>
</div>
<div id="content">
<%= yield %>
</div>
Run Code Online (Sandbox Code Playgroud)
但它在这条线上给出了一个错误
<%= wicked_pdf_stylesheet_link_tag 'application', media: 'all' %>
Run Code Online (Sandbox Code Playgroud)
错误
undefined method `start_with?' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
我如何删除这个?错误
ActionView::Template::Error (undefined method `start_with?' for nil:NilClass):
2: <html>
3: <head>
4: <meta charset='utf-8' />
5: <%= wicked_pdf_stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' …Run Code Online (Sandbox Code Playgroud) 我正在使用 gem 'wicked_pdf' 将 html 转换为 pdf。它在开发模式下运行良好。但是在生产中它被nginx抛出了一个错误。这是生产中的错误日志:
I, [2017-10-17T12:34:09.726920 #17004] INFO -- : [4a94b9f6-fdf6-4e55-b325-9b3096132d4e] Completed 500 Internal Server Error in 2638ms (ActiveRecord: 30.6ms)
F, [2017-10-17T12:34:09.729566 #17004] FATAL -- : [4a94b9f6-fdf6-4e55-b325-9b3096132d4e]
F, [2017-10-17T12:34:09.729763 #17004] FATAL -- : [4a94b9f6-fdf6-4e55-b325-9b3096132d4e] RuntimeError (Failed to execute:
["/home/deploy/phoenix/shared/bundle/ruby/2.4.0/bin/wkhtmltopdf", "-q", "file:////tmp/wicked_pdf20171017-17004-ps7ahv.html", "/tmp/wicked_pdf_generated_file20171017-17004-17rkfch.pdf"]
Error: PDF could not be generated!
Command Error: /home/deploy/phoenix/shared/bundle/ruby/2.4.0/gems/wkhtmltopdf-binary-0.12.3.1/bin/wkhtmltopdf_linux_amd64: error while loading shared libraries: libXrender.so.1: cannot open shared object file: No such file or directory
):
F, [2017-10-17T12:34:09.729900 #17004] FATAL -- : [4a94b9f6-fdf6-4e55-b325-9b3096132d4e]
F, [2017-10-17T12:34:09.730005 …Run Code Online (Sandbox Code Playgroud) ruby-on-rails ×10
wicked-pdf ×10
ruby ×4
pdf ×3
wkhtmltopdf ×3
chartkick ×2
charts ×1
nginx ×1