Rails 3 和 PDFkit
我正在尝试遵循此教程。
当我将 .pdf
添加到我的网址时,它什么也不做。我的控制器有:
respond_to :html, :pdf.
我的 mime 类型已声明。
我也尝试过这个:
respond_to do |format|
format.html
format.pdf {
html = render_to_string(:layout => false , :action => "www.google.fr")
kit = PDFKit.new(html)
send_data(kit.to_pdf, :filename => "candidats.pdf", :type => 'application/pdf')
return # to avoid double render call
}
end
但它不起作用,而且我没有收到错误。我的浏览器一直在等待本地主机,但没有任何反应。
那么我应该如何尝试使用 pdfkit ?
编辑2:
根据我的rails日志,rails成功渲染了HTML。我在 .log 中看到了这一点,rails 没有将其发送到 webrick 或我的浏览器。我的浏览器一直在等待,一直等待,什么也没有发生。我这里只有小照片。
编辑 3:我的 webrick 服务器似乎无法响应其他请求,一旦他开始获取我的网址的 .pdf
版本,有什么想法吗?
编辑4:
我正在使用rails 3.1、wkhtmltopdf 0.9.5(Windows安装程序)和pdfkit 0.5.2
I'm trying to follow this tutorial.
When I'm adding .pdf
to my url it does nothing. My controller has:
respond_to :html, :pdf.
My mime type has been declared.
I tried this too:
respond_to do |format|
format.html
format.pdf {
html = render_to_string(:layout => false , :action => "www.google.fr")
kit = PDFKit.new(html)
send_data(kit.to_pdf, :filename => "candidats.pdf", :type => 'application/pdf')
return # to avoid double render call
}
end
but it does not work and I don't get errors. My browser keep waiting for localhost, but nothing happens.
So how should I try to use pdfkit ?
edit 2 :
According to my rails' logs, rails render successfully the HTML. I saw this in the .log, rails doesn't send it to webrick nor to my browser. And my browser keeps waiting, and waiting and nothing happen. I only have little pictures here.
edit 3 : My webrick server seem unable to respond to other request, once he starts getting a .pdf
version of my url, any ideas ?
edit 4 :
I am using rails 3.1, wkhtmltopdf 0.9.5 (windows installer) and pdfkit 0.5.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了一种更好的方法来访问我的 .pdf url,即使在开发模式下也是如此。
Config.cache_classes 是一个注释,因为当它不是时我遇到了一些问题。这样,即使在 Rails 3.1 中,pdfkit 也能发挥出色的效果。但是,您不会在请求之间重新加载代码。
这并不是真正的问题,因为您首先处理 html,然后切换配置,以便检查 pdf 结果。这样您就不必担心您的生产数据库。
I found a better way to access my .pdf urls even in developpement mode.
Config.cache_classes is a comment, cause i had some problems when it wasn't. This way pdfkit works wonder even with rails 3.1. But, you don't reload code between requests.
That's not really a problem, cause you first work on your html, and you switch configuration, in order to check the pdf result. This way you don't have to bother about your production database.
感谢另一个堆栈溢出答案,我得到了部分解决方案。
这是有效的:
您可以将
attachment
替换为inline,
,以便 pdf 显示在您的浏览器中。在我谈到的堆栈溢出答案中(我不记得链接),缺少
.to_pdf
,但这是强制性的。否则PDF阅读器无法识别它。我正在尝试使用 .pdf 网址来完成这项工作。
编辑 3:
我的 .pdf url 问题已解决。 Rails 3.1 的已知问题,但 google 无法找到它们。
解释:解释
解决方法(尚未尝试)。 解决方法
Thanks to another stack overflow answer I got part of solution.
This works:
You can replace
attachement
byinline,
so the pdf is displayed in your browser.In the stack overflow answer I spoke about (I don't remember the link), the
.to_pdf
was missing, but is mandatory. Otherwise PDF reader doesn't recognize it.I'm trying to get this work with a .pdf url.
Edit 3:
My problem with .pdf urls is solved. known issue with rails 3.1, but google was unable to find them.
explanation : explanation
Workaround (hadn't tryied yet). workaround