现在我正在使用rails 3.0.0.如果我在终端中运行我的项目,我会收到此警告.请帮我.
/usr/share/ruby-rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.0/lib/action_dispatch/http/mime_type.rb:98:警告:已经初始化的常量PDF
我正在尝试使用ActionMailer和wicked_pdf生成带有渲染PDF附件的电子邮件.
在我的网站上,我已经分别使用了wicked_pdf和actionmailer.我可以使用wicked_pdf在Web应用程序中提供pdf,并可以使用ActionMailer发送邮件,但是我无法将呈现的pdf内容附加到ActionMailer(针对内容进行编辑):
class UserMailer < ActionMailer::Base
default :from => "webadmin@mydomain.com"
def generate_pdf(invoice)
render :pdf => "test.pdf",
:template => 'invoices/show.pdf.erb',
:layout => 'pdf.html'
end
def email_invoice(invoice)
@invoice = invoice
attachments["invoice.pdf"] = {:mime_type => 'application/pdf',
:encoding => 'Base64',
:content => generate_pdf(@invoice)}
mail :subject => "Your Invoice", :to => invoice.customer.email
end
end
Run Code Online (Sandbox Code Playgroud)
使用Railscasts 206(Rails 3中的Action Mailer)作为指南,只有在我不尝试添加渲染的附件时,我才能发送包含我所需的丰富内容的电子邮件.
如果我尝试添加附件(如上所示),我会看到一个看起来正确大小的附件,只有附件的名称没有按预期发现,也不能作为pdf读取.除此之外,我的电子邮件内容丢失了......
有没有人在使用Rails 3.0动态渲染PDF时有使用ActionMailer的经验?
提前致谢! - 担
我一直在使用Wicked_pdf将视图呈现为PDF和动作管理器来发送电子邮件,但我不能让它们一起工作.我想使用actionmailer将特定视图的PDF版本附加到电子邮件中,然后通过单击链接或按钮将其发送出去.我有一个发送电子邮件的link_to命令.这是我的控制器获取生成的电子邮件:
def sendemail
@user = User.find(params[:id])
Sendpdf.send_report(@user).deliver
redirect_to user_path(@user)
flash[:notice] = 'Email has been sent!'
end
Run Code Online (Sandbox Code Playgroud)
这是我在动作管理器中的内容:
class Sendpdf < ActionMailer::Base
default :from => "myemail@email.com"
def send_report(user)
@user = user
attachment "application/pdf" do |a|
a.body = #Something should go here, maybe WickedPDF.new.something?
a.filename = 'MyPDF'
end
mail(:to => user.email, :subject => "awesome pdf, check it")
end
end
Run Code Online (Sandbox Code Playgroud)
我见过很多问题和答案,大多数都与Prawn打交道.似乎应该有一个简单的答案.有人可以帮忙吗?
更新我很感激在下面的答案中使用建议作为替代选项.但是,我真的想学习如何将视图呈现为PDF并将其附加到我的电子邮件中.如果需要,我愿意使用像Prawn或其他任何东西一样的东西.
pdf-generation ruby-on-rails actionmailer ruby-on-rails-3 wicked-pdf
我正在使用本指南在heroku上集成wicked_pdf.但不知何故它似乎不起作用.我从heroku获得了日志,它说:
Processing PdfController#get_pdf to pdf (for 115.248.175.50 at 2011-02-15 23:54:44) [GET]
Parameters: {"format"=>"pdf", "action"=>"get_pdf", "id"=>"1", "controller"=>"pdf"}
***************WICKED***************
Rendering pdf/get_pdf
RuntimeError (PDF could not be generated!
/usr/ruby1.8.7/lib/ruby/1.8/open3.rb:73:in `exec': No such file or directory - /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/bin/wkhtmltopdf-amd64 - - (Errno::ENOENT)
from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:73:in `popen3'
from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:59:in `fork'
from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:59:in `popen3'
from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:57:in `fork'
from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:57:in `popen3'
from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/wicked_pdf.rb:22:in `pdf_from_string'
from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/pdf_helper.rb:28:in `make_pdf'
from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/pdf_helper.rb:39:in `make_and_send_pdf'
from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/pdf_helper.rb:13:in `render'
from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/app/controllers/schedule_controller.rb:33:in `get_pdf'
Run Code Online (Sandbox Code Playgroud)
请帮忙.提前致谢.
编辑:
我在代码中做了一些更改并完成了这个错误,但仍然停留在这里
NoMethodError (undefined method empty? for #<Pathname:0x2b7112392480>)
pdf-generation ruby-on-rails heroku production-environment wicked-pdf
我安装了邪恶的PDF并修改了我的控制器:
def show
respond_to do |format|
format.pdf do
render :pdf => "file_name"
end
format.html
end
end
Run Code Online (Sandbox Code Playgroud)
以下是我如何链接到pdf: compte_contrat_path(c,:format=>'pdf')
它适用于html(没有格式)但对于PDF失败,出现以下错误:
模板丢失了
Missing template contrats/show with {:locale=>[:fr], :formats=>[:pdf], :handlers=>[:erb, :builder, :coffee, :arb]}. Searched in:* "/home/sylario/ruby/place_de_marche/app/views"* "/usr/local/rvm/gems/ruby-1.9.2-p136/gems/activeadmin-0.5.0/app/views" *"/usr/local/rvm/gems/ruby-1.9.2-p136/gems/kaminari-0.14.1/app/views"*"/usr/local/rvm/gems/ruby-1.9.2-p136/gems/devise-2.2.0/app/views"
What am I doing wrong?
在Rails3中,我使用它WickedPDF gem来渲染我的一个模型的PDF格式.这很好用:/invoices/123返回HTML,/invoices/123.pdf下载PDF.
在我的发票模型中,我使用state_machine gem来跟踪发票状态.当发票从"未开单"状态变为"开票"状态时,我想获取发票PDF的副本并使用CarrierWave将其附加到发票模型.
我将这三个部分分开工作:控制器创建PDF视图,模型跟踪状态并在进行正确转换时触发回调,并正确设置CarrierWave.但是,我有一段时间让他们一起玩得很好.
如果我只是想获取发票的HTML版本,我可以render_to_string从模型中调用.但render_to_string似乎在收到PDF二进制文件时窒息.如果我可以获得一个数据流,那么很容易将这些数据写入临时文件并将其附加到上传器,但我无法弄清楚如何获取数据流.
有什么想法吗?代码如下:
发票控制器
def show
@invoice = @agg_class.find(params[:id])
respond_to do |format|
format.pdf do
render_pdf
end
format.html # show.html.erb
format.json { render json: @aggregation }
end
end
Run Code Online (Sandbox Code Playgroud)
...
def render_pdf(options = {})
options[:pdf] = pdf_filename
options[:layout] = 'pdf.html'
options[:page_size] = 'Letter'
options[:wkhtmltopdf] = '/usr/local/bin/wkhtmltopdf'
options[:margin] = {
:top => '0.5in',
:bottom => '1in',
:left => '0in',
:right => '0in'
}
options[:footer] = {
:html => { …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的prod服务器上使用wicked_pdf,但它仍然令人心烦:
RuntimeError (Failed to execute:
"/usr/bin/wkhtmltopdf" -q "file:////tmp/wicked_pdf20130709-23109-1adqx5g.html" "/tmp/wicked_pdf_generated_file20130709-23109-1ic5dbe.pdf"
Error: PDF could not be generated!
Command Error: wkhtmltopdf: cannot connect to X server
):
app/controllers/contrats_controller.rb:15:in `block (2 levels) in show'
app/controllers/contrats_controller.rb:11:in `show'
Run Code Online (Sandbox Code Playgroud)
我试着按照这个答案:wkhtmltopdf:无法连接到X服务器,但它仍然无法正常工作.
我正在使用wicked_pdf和rails 3.2.11以及ruby 1.9.3从HTML生成PDF并部署到Heroku.
我的pdf.css.scss.erb:
<% app_fullhost = Constants["app_fullhost"] %>
@font-face {
font-family:'DosisMedium'; font-style:normal; font-weight:500;
src: url(<%=app_fullhost%>/app/font/dosis/Dosis-Medium.ttf) format('woff');
}
*, body {
font-family: "DosisLight", 'Times New Roman', 'Arial', sans-serif;
}
Run Code Online (Sandbox Code Playgroud)
app_fullhost在开发或生产中,确切的主机在哪里.
我的pdf布局包括:
%html{:lang => I18n.locale}
%head
%meta{:charset => "utf-8"}
%title= content_for?(:title) ? yield(:title) : Settings.app_name
= wicked_pdf_stylesheet_link_tag "pdf"
Run Code Online (Sandbox Code Playgroud)
在production.rb我有
config.assets.precompile +=%w(pdf.css)
Run Code Online (Sandbox Code Playgroud)
这在开发中没有问题,但在Heroku上,pdf文件不会加载所需的字体.我也尝试过不同的解决方案,比如在production.rb中添加这些解决方案:
config.assets.paths << "#{Rails.root}/app/assets/fonts"
config.assets.precompile += %w(*.svg *.eot *.woff *.ttf)
config.assets.precompile += %w(.svg .eot .woff .ttf)
Run Code Online (Sandbox Code Playgroud)
我也尝试改变(在pdf.css.scss.erb中):
@font-face {
font-family:'Dosis'; font-style:normal; font-weight:500;
src: url('Dosis-Medium.ttf') format('woff');
}
Run Code Online (Sandbox Code Playgroud)
要么 …
我试图在Rails 3站点上使用wicked_pdf在HTML中使用特定字体生成PDF文件.我在这里找到了其他建议.对我来说唯一(大部分)工作的是将字体转换为base64.我在这里找到了原始答案: 邪恶的PDF +字体+ heroku + rails3.2
我不得不将@ font-face CSS直接放入使用它的部分文件中,而不是将其放入样式表中以使其工作.它现在在我的本地副本中工作正常.当我将它部署到我们的登台服务器时,它只有一半工作.其中一种字体加载,但不加载字体的粗体版本.
这是我在部分中包含的@ font-face CSS(这个pastebin包含了很有用的整个Base64代码):
<style type="text/css">
@font-face {
font-family: 'MuseoSans300';
src: url(data:font/truetype;charset=utf-8;base64,AAEAAAATAQAABAA...excess text removed);
}
@font-face {
font-family:'MuseoSans700';
src: url(data:font/truetype;charset=utf-8;base64,AAEAAAATAQAABAA...excess text removed);
}
</style>
Run Code Online (Sandbox Code Playgroud)
使用这些字体的常规样式表(使用SASS)中的样式如下所示:
#profile_pdf {
font-family: 'MuseoSans300';
h1 {
font-size: 30px;
font-family: 'MuseoSans700';
}
h2 {
font-size: 20px;
font-family: 'MuseoSans300';
}
}
Run Code Online (Sandbox Code Playgroud)
我试过用各种方法改变它.我使用了与此建议相同的格式:http: //blog.shahariaazam.com/use-google-web-fonts-for-wkhtmltopdf-tools/#.UtwZUmQo5hE
这使它完全停止工作.
使用上面显示的格式,它可以在我本地运行的副本上工作.在登台服务器上只有一种字体可以工作; 它只加载300版本的所有内容,700版本不加载.有没有其他人遇到这个问题?
我想生成一个包含我们部门徽标的PDF.当我尝试在我的控制器中使用WickedPdf类时(使用https://github.com/mileszs/wicked_pdf中描述的方法):
def some_action
image_tag_string = image_tag('logo.jpg')
pdf = WickedPdf.new.pdf_from_string(image_tag_string)
save_path = Rails.root.join('testpdfs','logotest.pdf')
File.open(save_path, 'wb') do |file|
file << pdf
end
end
Run Code Online (Sandbox Code Playgroud)
...应用程序将PDF保存到目标目录,但它有一个蓝白色的'?' 标记图像应该在哪里.
如果我这样做:
image_tag_string = wicked_pdf_image_tag('logo.jpg')
pdf = WickedPdf.new.pdf_from_string(image_tag_string)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
NoMethodError:
undefined method `wicked_pdf_image_tag' for #<...
Run Code Online (Sandbox Code Playgroud)
看来我的Rails应用程序也缺少/没有链接到属于wicked-pdf gem的帮助文件.
StackOverflow上类似问题的答案建议编写一个自定义的"图像标记"帮助程序来定位图像或安装wkhtmltopdf.对我来说,当放置在View(whatever.html.erb)中时,image-tag显示徽标就好了."logo.jpg"已经位于资产管道和#{RailsRoot}/public/images中.最后,我在Ubuntu 14.04上使用wkhtmltopdf 0.9.9,wicked-pdf 0.11.0和rails 4.
简而言之 - 我做错了什么导致WickedPDF无法渲染图像?
wicked-pdf ×10
actionmailer ×2
heroku ×2
wkhtmltopdf ×2
base64 ×1
carrierwave ×1
font-face ×1
fonts ×1
pdf ×1
pdfkit ×1
ruby ×1
rubygems ×1
templates ×1