pdfkit 不设置 pdf 样式
我有一个 Rails 3.1 应用程序,它使用 pdfkit 创建 pdf 文档,除了生成的 pdf 没有任何样式之外,一切都按指定工作。我假设 wkhtmltopdf 无法访问我的样式表,并且这不是一个更大的问题。有人知道如何允许访问这些样式表吗?我基本上遵循了关于这个主题的railscast #220,但是我必须创建一个新的初始化程序才能让pdfkit与rails 3.1一起工作。
这是我必须用来让 pdfkit 与 Rails 3.1 一起使用的初始化程序
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
}
pdf 的链接如下所示:
<%= link_to 'Download PDF', load_path(@load, :format => "pdf") %>
这将为我提供一个没有样式的 pdf 的链接。
在我的 application.rb 中,我已这样配置 pdfkit:
config.middleware.use PDFKit::Middleware, :print_media_type => true
我还将其添加到我的 layouts/application.html.erb 文件中:
<%= stylesheet_link_tag "application", :media => "all" %>
I have a rails 3.1 app that creates pdf documents using pdfkit, and everything works as specified, except for the fact that the generated pdfs don't have any styling. I am assuming that wkhtmltopdf doesn't have access to my stylesheets and that it is not a larger issue than that. Would anyone have a clue as to how you would allow access to these stylesheets? I have basically followed railscast #220 on the subject, however I have had to create a new initializer to get pdfkit to work with rails 3.1.
This is the initializer that I had to use to get pdfkit to work with rails 3.1
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
}
The link to the pdf looks like this:
<%= link_to 'Download PDF', load_path(@load, :format => "pdf") %>
This will give me a link to the pdf that has no styling.
In my application.rb I have configured pdfkit as such:
config.middleware.use PDFKit::Middleware, :print_media_type => true
I have also added this to my layouts/application.html.erb file:
<%= stylesheet_link_tag "application", :media => "all" %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从 https://github.com/pdfkit 中找到的中间件代码中窃取几行/pdfkit/blob/master/lib/pdfkit/middleware.rb
你可以使用:
我的例子是:
Stealing a couple of lines from the middleware code found at https://github.com/pdfkit/pdfkit/blob/master/lib/pdfkit/middleware.rb
You can use:
My example is:
对我来说,这是 ubuntu 安装的问题。我刚刚从源代码重新安装:
现在一切都对我有用。所以我的建议是不要通过此命令安装 wkhtmltopdf sudo apt-get install wkhtmltopdf 并从源安装它。完整说明
用于安装过程
For me it was problem with installation for ubuntu. I just reinstalled from source:
And everything works now for me. So my advice is do not install wkhtmltopdf by this command
sudo apt-get install wkhtmltopdf
and install it from sources. Full instructionsfor installation process
我知道您正在寻找将呈现整个页面的解决方案,只是提醒人们,
现在仍然存在无问题的解决方法,pdf.css必须是css,所以理论上,如果您需要加载sass,请从预编译的公共加载它/资产/
I know you're looking for solution that will render whole page, just reminder for googling people that there is still problem free workaround
now the pdf.css must be css, so teoretically if you need to load sass load it from pre-compiled public/assets/
我也遇到了这个问题,似乎在 Rails 3.1 中添加资源管道时,pdfkit 的样式表链接出现了问题。请参阅有关此问题的 GitHub 问题。
我最终切换到 wicked_pdf 并且对此感到非常满意。他们已经解决了这个问题,并且在 Rails 3.2.x 上运行得很好(还没有尝试过 3.1.x)。
I ran into this problem as well, and it appears that when the asset pipeline was added in Rails 3.1, pdfkit has a problem with the stylesheet links. See the GitHub issue about this problem.
I ended up switching to wicked_pdf and am really happy with it. They've solved this problem and it works nicely on Rails 3.2.x (haven't tried 3.1.x).
我使用 gem 'wicked_pdf' 及其助手将 CSS 包含到页面中。在内部,帮助程序只是读取所有 CSS 文件并包含到页面本身中。因此,如果您更喜欢使用 PdfKit,请尝试研究如何包含非内联样式表。
I have used gem 'wicked_pdf' and its helpers to include CSS into pages. Internally that helpers just read all CSS files and include into the page itself. So if you prefer to use PdfKit try to investigate how to include non-inline stylesheets.
我已经在 Rails 3.1 上成功运行 PDFKit。不过,我使用了不同的设置。
起初我遇到了与您相同的问题,但那是因为
stylesheet_link_tag
的默认设置为media =>; “屏幕”
;明确指定media => “all”修复了它。
I have successfully run PDFKit on Rails 3.1. I have used a different setup though.
At first I had the same problem you did but that was because
stylesheet_link_tag
has a default set tomedia => "screen"
; specifying explicitelymedia => "all"
fixed it.