pdfkit 无法在 Rails 3.1 中正确呈现 [英] pdfkit not rendering correctly in rails 3.1

查看:62
本文介绍了pdfkit 无法在 Rails 3.1 中正确呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循以下 railscast 关于将 pdfkit 添加到应用程序,并且我有一些pdf生成的问题.以下是我做过的事情:

I have followed the following railscast about adding pdfkit to an application, and I am having some issues with the generation of pdfs. Here are the following things that I have done:

我通过自制软件包管理器下载了 wkhtmltopdf

I downloaded wkhtmltopdf via the homebrew package manager

brew install wkhtmltopdf

然后我将 pdfkit gem 添加到我的 gemfile 并运行 bundle install 命令.我将以下内容添加到我的 config/application.rb 文件

Then I added the pdfkit gem to my gemfile and ran the bundle install command. I added the following to my config/application.rb file

require 'pdfkit'
...
config.middleware.use PDFKit::Middleware, :print_media_type => true

然后我更改了我的应用程序布局文件以包含所有样式表类型.

I then changed my application layout file to include all stylesheet types.

如果我运行 rake 中间件,该命令将起作用并且我可以看到 pdfkit 中间件

If I run rake middleware, the command works and I can see the pdfkit middleware

当我尝试将 pdf 附加到路由的末尾时,应用程序只是挂起,我必须通过命令行退出.如果我创建一个指向我想要制作为 pdf 的页面的链接,它会更改所有标记,使其看起来像一个损坏的文件.(看起来你在文字处理器中打开了一个文本文件,反之亦然,如果有帮助,我可以提供图像)如果我尝试在我的样式表中进行 css 更改,当我使用 pdf 链接查看它们时,它们不会生效.我假设这与 rails 中的新资产管道有关,有没有其他人遇到过这个问题?

When I try to append pdf to the end of my routes the application just hangs and I have to exit via the command line. If I create a link to the page I want to make into a pdf, it changes all of the markup so it looks like a corrupted file. (it looks like you opened a text file into a word processor or vice versa I can provide images if that helps) If I try to make css changes in my stylesheet they do not go into effect when I view them with the link to pdf. I am assuming that this has something to do with the new asset pipeline in rails has anyone else experienced this issue?

推荐答案

所以我认为我的错误与资产管道有关是正确的,经过一些研究后,您似乎需要创建一个新的初始化程序和添加以下代码:

So I was right in assuming that my error had something to do with the asset pipeline, after doing some research it looks like you need to create a new initializer and add the following code:

ActionController::Base.asset_host = Proc.new { |source, request|
  if request.env["REQUEST_PATH"].include? ".pdf"
    "file://#{Rails.root.join('public')}"
  else
    "#{request.protocol}#{request.host_with_port}"
  end
}

这篇关于pdfkit 无法在 Rails 3.1 中正确呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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