我需要创建一个PDF文件(我现在可以渲染),然后使用paperclip保存它(已经安装并与其他模型一起使用)
我找到了这个解决方案:
https://github.com/thoughtbot/paperclip/wiki/PDF-Email-Attachments-with-Paperclip-and-wicked_pdf
但它使用已经创建的报告,我需要在每次创建新记录时创建此PDF,我得到“没有nill类的方法,所以可能这就是为什么不适合我。
我尝试在“if @ instrument.save”中添加此方法,但没有用。
让我告诉你我的代码:
instrument.erb
class Instrument < ActiveRecord::Base
belongs_to :evaluation
has_attached_file :instrument_document
validates_attachment :instrument_document, content_type: { content_type: "application/pdf" }
do_not_validate_attachment_file_type :instrument_document
end
仪器#节目
def show
respond_to do |format|
format.html { redirect_to instruments_url }
format.pdf do
render :pdf => "file.pdf", :template => 'instruments/instrument_document.html.erb', :margin => {:top => 15, :bottom =>15 },:header => { html: { template: 'instruments/header.pdf.erb', }}, :footer => { right: '[page]' }
end
end
end
仪器#创建
def create
@instrument = Instrument.new(instrument_params)
respond_to do |format|
if @instrument.save
# create an instance of ActionView, so we can use the render method outside of a controller
av = ActionView::Base.new()
av.view_paths = ActionController::Base.view_paths
# need these in case your view constructs any links or references any helper methods.
av.class_eval do
include Rails.application.routes.url_helpers
include ApplicationHelper
end
pdf_html = av.render template: 'instruments/instrument_document.html.erb'
# use wicked_pdf gem to create PDF from the doc HTML
doc_pdf = WickedPdf.new.pdf_from_string(
pdf_html,
page_size: 'Letter',
javascript_delay: 6000
)
# save PDF to disk
pdf_path = Rails.root.join('tmp/reports', "#{@instrument.id}_#{Date.today.iso8601}.pdf")
File.open(pdf_path, 'wb') do |file|
file << doc_pdf
end
@instrument.instrument_document = File.open pdf_path
@instrument.save!
# The report has now been saved elsewhere using Paperclip; we don't need to store it locally
File.delete(pdf_path) if File.exist?(pdf_path)
format.html { redirect_to @instrument, notice: 'Instrumento creado con éxito.' }
format.json { render :show, status: :created, location: @instrument }
else
format.html { render :new }
format.json { render json: @instrument.errors, status: :unprocessable_entity }
end
end
end
我得到了:
nil的未定义方法`评估':NilClass
谢谢!
答案 0 :(得分:1)
日志表明未创建@instrument,不关于 Paperclip 或 wicked_pdf 。请检查创建此变量的代码。
这对我有用;就我而言,我想做的事情非常接近你。
# pdf is the name of a pdf template ex.: invoice.pdf.erb
def generate_pdf (pdf)
html = render_to_string_with_wicked_pdf(:pdf => pdf,
:template => pdf,
:layout => 'application.pdf.html',
:encoding => 'UTF-8',
:page_size => 'A4',
:dpi => '300',
:print_media_type => true,
:no_background => true,
:margin => {
:top => 50,
:bottom => 25
},
:header => { :html => { :template => 'layouts/pdf-header.html' },
:spacing => 10,
:margin => {
:top => 40
}
},
:footer => { :html => { :template => 'layouts/pdf-footer.html' } }
)
end
此方法将pdf呈现为字符串。下一步是将这个pdf保存到这样的模型中:
class Invoice < ActiveRecord::Base
belongs_to :product
has_attached_file :invoice_pdf
validates_attachment_content_type :invoice_pdf, :content_type =>["application/pdf"]
validates_attachment_size :invoice_pdf, :less_than => 5.megabytes
end
这是控制器的一部分:
invoice = Invoice.new
invoice.product_id=@product.id
invoice.invoice_pdf = StringIO.new(generate_pdf("product/invoice.pdf.erb")) #mimic a real upload file
invoice.save!