pdfkit 不设置 pdf 样式

发布于 2024-12-13 23:17:54 字数 940 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

当梦初醒 2024-12-20 23:17:55

https://github.com/pdfkit 中找到的中间件代码中窃取几行/pdfkit/blob/master/lib/pdfkit/middleware.rb

你可以使用:

root = PDFKit.configuration.root_url || "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
html.gsub!(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/, '\1=\2' + root + '\3\2')

我的例子是:

html = render_to_string #render current action to string
root = PDFKit.configuration.root_url || "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
html.gsub!(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/, '\1=\2' + root + '\3\2')
kit = PDFKit.new(html, :print_media_type => true)

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:

root = PDFKit.configuration.root_url || "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
html.gsub!(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/, '\1=\2' + root + '\3\2')

My example is:

html = render_to_string #render current action to string
root = PDFKit.configuration.root_url || "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
html.gsub!(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/, '\1=\2' + root + '\3\2')
kit = PDFKit.new(html, :print_media_type => true)
倾`听者〃 2024-12-20 23:17:55

对我来说,这是 ubuntu 安装的问题。我刚刚从源代码重新安装:

# first, installing dependencies
sudo aptitude install openssl build-essential xorg libssl-dev

# for 64bits OS
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2 
tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf
chmod +x /usr/local/bin/wkhtmltopdf

# for 32bits OS
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-i386.tar.bz2 
tar xvjf wkhtmltopdf-0.9.9-static-i386.tar.bz2
mv wkhtmltopdf-i386 /usr/local/bin/wkhtmltopdf
chmod +x /usr/local/bin/wkhtmltopdf

现在一切都对我有用。所以我的建议是不要通过此命令安装 wkhtmltopdf sudo apt-get install wkhtmltopdf 并从源安装它。完整说明
用于安装过程

For me it was problem with installation for ubuntu. I just reinstalled from source:

# first, installing dependencies
sudo aptitude install openssl build-essential xorg libssl-dev

# for 64bits OS
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2 
tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf
chmod +x /usr/local/bin/wkhtmltopdf

# for 32bits OS
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-i386.tar.bz2 
tar xvjf wkhtmltopdf-0.9.9-static-i386.tar.bz2
mv wkhtmltopdf-i386 /usr/local/bin/wkhtmltopdf
chmod +x /usr/local/bin/wkhtmltopdf

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 instructions
for installation process

不知所踪 2024-12-20 23:17:55

我知道您正在寻找将呈现整个页面的解决方案,只是提醒人们,

class DocumentController < ApplicationController

  def show
    @document = Document.last
    # ... implement your respond_to

    kit = PDFKit.new(@document.content, :page_size => 'Letter')
    kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/pdf.css"
    send_data kit.to_pdf, :filename => "#{@document.title}.pdf", :type => 'application/pdf'
  end

end

现在仍然存在无问题的解决方法,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

class DocumentController < ApplicationController

  def show
    @document = Document.last
    # ... implement your respond_to

    kit = PDFKit.new(@document.content, :page_size => 'Letter')
    kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/pdf.css"
    send_data kit.to_pdf, :filename => "#{@document.title}.pdf", :type => 'application/pdf'
  end

end

now the pdf.css must be css, so teoretically if you need to load sass load it from pre-compiled public/assets/

唐婉 2024-12-20 23:17:55

我也遇到了这个问题,似乎在 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).

几味少女 2024-12-20 23:17:55

我使用 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.

花辞树 2024-12-20 23:17:55

我已经在 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 to media => "screen"; specifying explicitely media => "all" fixed it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文