rails 3 和 PDFkit [英] rails 3 and PDFkit

查看:39
本文介绍了rails 3 和 PDFkit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循这个教程.

当我将 .pdf 添加到我的网址时,它什么也不做.我的控制器有:

When I'm adding .pdf to my url it does nothing. My controller has:

respond_to :html, :pdf.

我的 MIME 类型已声明.

My mime type has been declared.

我也试过这个:

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

但它不起作用,我没有收到错误消息.我的浏览器一直在等待 localhost,但没有任何反应.

but it does not work and I don't get errors. My browser keep waiting for localhost, but nothing happens.

那么我应该如何尝试使用 pdfkit 呢?

So how should I try to use pdfkit ?

根据我的 rails 日志,rails 成功渲染了 HTML.我在 .log 中看到了这一点,rails 不会将它发送到 webrick,也不会发送到我的浏览器.我的浏览器一直在等待,等待,什么也没发生.我这里只有小图片.

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.

编辑 3:我的 webrick 服务器似乎无法响应其他请求,一旦他开始获取我的 url 的 .pdf 版本,有什么想法吗?

edit 3 : My webrick server seem unable to respond to other request, once he starts getting a .pdf version of my url, any ideas ?

我使用的是 rails 3.1、wkhtmltopdf 0.9.5(Windows 安装程序)和 pdfkit 0.5.2

I am using rails 3.1, wkhtmltopdf 0.9.5 (windows installer) and pdfkit 0.5.2

推荐答案

我找到了一种更好的方式来访问我的 .pdf 网址,即使在开发模式下也是如此.

I found a better way to access my .pdf urls even in developpement mode.

# Enable threaded mode
config.threadsafe!

# Code is not reloaded between requests
#config.cache_classes = true

Config.cache_classes 是一个注释,因为它不是我遇到了一些问题.这样 pdfkit 即使使用 rails 3.1 也能奇迹般地工作.但是,您不会在请求之间重新加载代码.

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.

这不是真正的问题,因为您首先处理 html,然后切换配置,以检查 pdf 结果.这样您就不必担心您的生产数据库.

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.

这篇关于rails 3 和 PDFkit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆