我正在使用WickedPdf
respond_to do |format|
format.html
format.pdf do
render :pdf => "file_name"
end
end
Run Code Online (Sandbox Code Playgroud)
这工作正常.用户可以下载生成的pdf.但我需要将生成的pdf存储在服务器中用于其他目的,如邮件等等.如何保存这个生成的pdf?
我尝试了以下但不知道如何将html传递给wickedpdf wicked_pdf不起作用 - Ruby on Rails
提前致谢
如何获得以pdf格式显示的产品图像?
我在视图文件中有这个代码
<div id="img-slide">
<% for asset in @car.assets %>
<%= image_tag(asset.asset.url(:medium)) %>
<% end %>
</div>
Run Code Online (Sandbox Code Playgroud)
它显示了这辆车的所有图像.
但是如果我在show.pdf.erb中使用相同的代码,那么我只会得到问号而不是图像...就像图像丢失的东西一样.那么,有没有办法让它们在纸上?谢谢.
ps有什么控制台显示
***************WICKED***************
Asset Load (0.2ms) SELECT `assets`.* FROM `assets` WHERE (`assets`.car_id = 29)
Carmodel Load (0.2ms) SELECT `carmodels`.* FROM `carmodels` WHERE `carmodels`.`id` = 28 LIMIT 1
Rendered cars/show.pdf.erb (255.2ms)
"***************/usr/bin/wkhtmltopdf -q - - ***************"
Run Code Online (Sandbox Code Playgroud)
更新
<%= pdf_image_tag('/public/system/assets/163/medium/2011_lincoln_navigator_l_angularfront.jpg', :style=>"margin:0px;padding:0px", :width=>"300", :height=>"240")%>
Run Code Online (Sandbox Code Playgroud)
这段代码显示了链接在html中应该是什么样子,这段代码我可以渲染一张汽车的照片,但对于所有汽车来说都是一样的,所以我做的并不多.
163号码是分配给汽车的资产的ID,这里我保留一个图像的尺寸更大(拇指,中等,大......),我有一张汽车有5个不同数字的地图.所以我有很多像这样的数字地图,因为我每辆车至少有5张照片.每辆车有5个资产.在show.html中我可以看到它们,但不是pdf.我把它放在应用程序助手中:
def pdf_image_tag(image, options = {})
options[:src] = File.expand_path(RAILS_ROOT) + '' + image
tag(:img, options)
end
Run Code Online (Sandbox Code Playgroud)
但这仅适用于您服务器上的图像,并且对于所有车辆都是相同的,我如何才能获得每张车的至少一张图像以pdf显示?Pleaseeeee.救命!!!
当我尝试在由wicked_pdf(wkhtmltopdf)生成的PDF中包含SVG时,它会显示为空白.知道如何让svg在pdf中显示吗?
应用程序/视图/条码/ to_pdf.html.haml
<descriptive text here>
%object#code_image{:data=>"/barcodes/generate_svg?code=4567898", :type=>"image/svg+xml", :height=>70}
Run Code Online (Sandbox Code Playgroud)
条形码控制器
def generate_svg
require 'barby'
require 'barby/barcode/code_128'
require 'barby/outputter/svg_outputter'
barcode = Barby::Code128B.new(params[:code])
render :text => barcode.to_svg({:height=>30, :width => 170})
end
def to_pdf
render :pdf => 'file_name'
end
Run Code Online (Sandbox Code Playgroud) 我目前运行Rails 3.1并使用最新版本的Wicked_pdf进行PDF生成.我已经正确设置了所有内容,并且PDF生成得很好.
但是,我希望用户能够单击按钮以下载pdf.目前,单击时,浏览器将呈现pdf并将其显示在页面上.
<%= link_to "Download PDF", { :action => "show", :format => :pdf }, class: 'button nice red large radius', target: '_blank'%>
Run Code Online (Sandbox Code Playgroud)
我的控制器.
def show
@curric = Curric.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @curric }
format.pdf do
render :pdf => @curric.name + " CV",
:margin => {:top => 20,
:bottom => 20 }
end
end
end
Run Code Online (Sandbox Code Playgroud)
我被指向send_file,但绝对不知道如何在这种情况下使用它.
任何帮助表示赞赏.
本地它像魔术一样工作,但当我尝试在服务器上生成PDF时它会:
RuntimeError (Failed to execute:
"/path/to/my/project/vendor/bundle/ruby/1.9.1/bin/wkhtmltopdf" -q - -
Error: Broken pipe):
Run Code Online (Sandbox Code Playgroud)
这是my_controller上的内容.
format.pdf do
pdf = render_to_string(
:pdf => "invoice",
:template => "my_controller/my_view.pdf.erb",
:layout=>"pdf.html.erb"
)
save_path = Rails.root.join('pdfs','invoice.pdf')
File.open(save_path, 'wb') do |file|
file << pdf
end
send_file(save_path)
end
Run Code Online (Sandbox Code Playgroud)
在我的Gemfile中
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
Run Code Online (Sandbox Code Playgroud) 现在我正在使用Rails 3.0.0.i已经安装了gem wicked_pdf.Now想要将pdf文件保存在公共文件夹中.请帮助我.
pdf-generation ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 wicked-pdf
我使用wkhtmltopdf 0.12与wicked_pdf或pdfkit,标题几乎占据页面高度的100%.
它会产生以下问题:
我使用两个Gems进行html到pdf转换.
使用" https://github.com/mileszs/wicked_pdf "
gem 'wicked_pdf'
gem "wkhtmltopdf-binary"
Run Code Online (Sandbox Code Playgroud)
/initializer/wicked_pdf.rb
WickedPdf.config = {
exe_path => "xxxxxxxxxxxxxxxxxxx"
}
Run Code Online (Sandbox Code Playgroud)
我在exe_path中使用了什么路径?
我已经使用wicked pdf gem for html to pdf generate in rails 4.它对我不起作用,它会在/events/calendar_month_print.pdf中抛出类似于ActionView :: MissingTemplate的错误.缺少模板事件/ calendar_month_print.html.erb with { :locale => [:en],:formats => [:pdf],:variants => [],:handlers => [:erb,:builder,:raw,:ruby,:rabl,:rxls,:" xls.rxls",:jbuilder]}.搜索:我也在我的应用程序中创建了calendar_month_print.html.erb文件.我不知道我在哪里丢失.
我的宝石文件
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
Run Code Online (Sandbox Code Playgroud)
我的Controller文件
respond_to do |format|
format.html
format.pdf do
render :pdf => "monthly_events.pdf",:template => "events/calendar_month_print.html.erb"
end
end
Run Code Online (Sandbox Code Playgroud)
我的视图文件
<%= button_to "Print as pdf", calendar_month_print_events_path(format: "pdf"), :class => "btn btn-primary button monthly_print pull-right",:method => :get,target: '_blank' %>
Run Code Online (Sandbox Code Playgroud)
我的config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf
Run Code Online (Sandbox Code Playgroud)
我的config/initializers/wicked_pdf.rb
WickedPdf.config = { …Run Code Online (Sandbox Code Playgroud) 我已经将wicked_pdf添加到我的rails应用程序中,其中包含自述文件后面的默认选项(偏差是我通过osx安装程序安装了wkhtmltopdf).
当我使用.pdf?debug = true选项渲染页面时,页面看起来应该完全正确.
然后,当我省略调试选项时,它会生成一个带有小版本页面的PDF.像1/16的大小.
我没有使用任何选项或配置.我哪里错了?我应该在哪里看?