我正在尝试使用Wicked_pdf gem和Highcharts生成PDF报告.我已经尝试过设置animation,enableMouseTracking而且是shadow假的.但是当wkhtmltopdf尝试生成PDF文件时,我收到了一个错误.
我真的不知道该怎么做,因为当我为HTML做同样的程序时,Highcharts完美渲染.
在我的发票系统中,我想要一个备份功能,将所有发票一次性下载到一个 zip 文件中。该系统在 heroku 上运行 - 因此只能临时保存 pdf。
我已经安装了 rubyzip 和 wicked_pdf gem。
我在控制器中的当前代码:
def zip_all_bills
@bill = Bill.all
if @bill.count > 0
t = Tempfile.new("bill_tmp_#{Time.now}")
Zip::ZipOutputStream.open(t.path) do |z|
@bill.each do |bill|
@bills = bill
@customer = @bills.customer
@customer_name = @customer.name_company_id
t = WickedPdf.new.pdf_from_string(
render :template => '/bills/printing.html.erb',
:disposition => "attachment",
:margin => { :bottom => 23 },
:footer => { :html => { :template => 'pdf/footer.pdf.erb' } }
)
z.puts("invoice_#{bill.id}")
z.print IO.read(t.path)
end
end
send_file t.path, :type => "application/zip", …Run Code Online (Sandbox Code Playgroud) 环境: - Ruby - 1.9.3,Rails - 4.0.0
我使用wicked_pdf gem生成pdf.
为此,我在我的控制器中使用了以下代码: -
respond_to do |format|
format.html
format.pdf do
render :pdf => 'contractors_data',
:javascript_delay => 5000,
:disposition => 'attachment',
:template => 'tiles/templates/pdf/contractors.pdf.erb',
:header => { :right => 'Page [page] of [topage]' }
end
Run Code Online (Sandbox Code Playgroud)
在这个pdf中,我使用$.plotjavascript/jquery方法生成图形.所以我想等待pdf生成器等到$.plot没有响应,而不是在pdf上绘制图形.
我确信并确认javascript和jquery正在这个页面上工作,因为我使用了wicked_pdf_javascript_link_tag.
现在我:javascript_delay => 5000在respond_to格式选项中使用了.但是在正常工作的同时,它会产生错误:redirect_delay => 5000.
我也在cmd提示符下尝试这个.我在cmd上遇到类似于GUI工作的错误.
请回答好的解决方案.如果可能,请提供代码以供参考.
在我生成的pdf中,我得到了这个
0,00 €
Run Code Online (Sandbox Code Playgroud)
代替
0,00 €
Run Code Online (Sandbox Code Playgroud)
Application.html.erb
<meta charset="utf-8">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
Run Code Online (Sandbox Code Playgroud)
show.html.erb
<%=number_to_currency item.total%>
Run Code Online (Sandbox Code Playgroud) 在rails 3.2.18应用程序中,在gem文件中声明了wicked_pdf(0.11.0)和wkhtmltopdf-binary(0.9.9.3),mime-types.rb 未被修改为包含Mime::Type.register "application/pdf", :pdf否则,在osX开发平台上启动Thin ,给出以下警告:
/Users/it/.rvm/gems/ruby-1.9.3-p125@app/gems/actionpack-3.2.18/lib/action_dispatch/http/mime_type.rb:102: warning: already initialized constant PDF
Run Code Online (Sandbox Code Playgroud)
初始化器声明
WickedPdf.config = {
:exe_path => '/usr/local/bin/wkhtmltopdf'
}
Run Code Online (Sandbox Code Playgroud)
该路径/usr/local/bin包含wkhtmltopdf的别名.在展示视图中:
def show
@transaction = Transaction.find(params[:id])
respond_to do |format|
format.html { render :layout => 'pdf' } # show.html.erb
format.pdf do
render :pdf => "invoice_name"
end
format.json { render json: @transaction }
end
end
Run Code Online (Sandbox Code Playgroud)
节目视图呈现得恰当.附加路径.pdf和控制台注册:
"***************[\"/usr/local/bin/wkhtmltopdf\", \"-q\", \"file:///var/folders/kV/kVDOSPkcEuqSVnTjenAVRE+++TI/-Tmp-/wicked_pdf20141018-1401-m614pd.html\", \"/var/folders/kV/kVDOSPkcEuqSVnTjenAVRE+++TI/-Tmp-/wicked_pdf_generated_file20141018-1401-1hor23g.pdf\"]***************"
Run Code Online (Sandbox Code Playgroud)
并且服务器挂起.它被中断,ctrl-c然后服务器控制台添加:
[...]
Rendered transactions/show.pdf.erb (12.1ms)
Rendered text template (0.0ms)
Sent data invoice_name.pdf (18.8ms) …Run Code Online (Sandbox Code Playgroud) 我已经在我的台式机上安装了wkhtmltopdf 0.12.3(带有修补的qt)和我的Centos虚拟机上完全相同的版本.
我正在使用WickedPDF和Rails将HTML转换为PDF.
生成PDF时,它在字体上略有不同.一些奇怪的字母间距.我附上的图片显示了这一点.
你可以看到e和n之间的差距但是我的本地机器上没有.图片如下:
有谁知道为什么会这样?
任何意见,将不胜感激.
谢谢
这是我使用WickedPDF生成PDF的Ruby代码
def generate_pdf
pdf = WickedPdf.new
pdf_file = pdf.pdf_from_string(ActionController::Base.new().render_to_string('card_requests/show.pdf.haml', layout: 'pdf', locals: {card_request: self}),
dpi: '300',
margin: {top: 0, bottom: 0, left: 0, right: 0},
page_height: '2.25in',
page_width: '3.75in',
disable_smart_shrinking: false
)
tempfile = Tempfile.new(["#{self.name.gsub(' ', '_').downcase}_biz_card", '.pdf'], Rails.root.join('pdfs'))
tempfile.binmode
tempfile.write pdf_file
tempfile.close
self.pdf = File.open(tempfile.path)
tempfile.unlink
self.save
end
Run Code Online (Sandbox Code Playgroud)
这里也是show.pdf.haml文件,顶部有CSS:
!!! 5
%html
%head
:css
html * {
margin: 0 !important;
padding: 0 !important;
page-break-inside: avoid …Run Code Online (Sandbox Code Playgroud) 我被困在 gem wicked_pdf 上。
在 wicked_pdf 文档中,据说我可以使用wicked_pdf_stylesheet_pack_tag并wicked_pdf_javascript_pack_tag包含来自 webpack 的样式表和 javascript,但没有任何效果。
这是来自控制器的代码:
format.pdf do
render template: "pdf_reports/show",
layout: "wicked_layout",
pdf: "report"
end
Run Code Online (Sandbox Code Playgroud)
这是布局中的代码:
<!DOCTYPE html>
<html>
<head>
<%= csrf_meta_tags %>
<%= wicked_pdf_javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= wicked_pdf_stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= wicked_pdf_stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= yield %>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是视图 pdf.erb 中的代码:
<h1 class="text-red-base">Test pdf</h1>
<h2 class="test-wicked">mldgmdjgfd</h2>
Run Code Online (Sandbox Code Playgroud)
它适用于 wicked_pdf_stylesheet_link_tag(test-wicked …
我尝试使用 gem wicked_pdf 将 Rails 视图转换为 pdf。但是当我像这样执行 render_to_string 时
ActionController::Base.new.render_to_string(template: "templates/pdf_meteo.html.erb", locals: {communaute_meteo_id: id}, layout: 'pdf')
Run Code Online (Sandbox Code Playgroud)
像这样的方法user_path不起作用并返回未定义的方法错误...(请注意,如果我在 html 中呈现该页面,则该页面可以正常工作)如果有人可以帮助我!
使用 Rails 5.2.4,我尝试在后台(在作业内)生成 pdf,然后将其附加到模型,如下所示:
pdf_contents = ApplicationController.render(
pdf: "name",
template: 'mytemplate.html.erb',
layout: 'pdf_layout.html',
disposition: 'attachment'
)
@user.attach(io: StringIO.new(pdf_contents), filename: "file.pdf", content_type: "application/pdf")
Run Code Online (Sandbox Code Playgroud)
但我得到:
WARN: ActionView::Template::Error: private method `format' called for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
使用
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
Run Code Online (Sandbox Code Playgroud) 我用户mac osx并通过wickedpdf尝试我的html文件到pdf文件.我想在我的pdf文件的每一页都放一个字符串,但是我有一个关于不渲染的标题的问题.
我的wickedpdf方法是,
format.pdf do
render :pdf => '#{@examination.name}.pdf',
:disposition => 'inline',
:layout => 'examination_session_pdf.html.erb',
:no_background => true,
:header =>{:html =>{:template=>'shared/pdf/header.pdf.erb'}}
end
Run Code Online (Sandbox Code Playgroud)
并且头文件只包含"hello"字符串或什么都没有.但是,每次我看到这个错误,
can't convert nil into String
Run Code Online (Sandbox Code Playgroud)
问题行是":header => {:html => {:template =>'shared/pdf/header.pdf.erb'".另外,我看不到有关在控制台上呈现标题页的任何日志.
我该如何解决?
wicked-pdf ×10
ruby ×2
wkhtmltopdf ×2
flot ×1
fonts ×1
heroku ×1
highcharts ×1
javascript ×1
jquery ×1
macos ×1
rendering ×1
rubyzip ×1
utf-8 ×1
web ×1
webpack ×1