在heroku上实现wicked pdf时出现问题

发布于 2024-10-18 01:12:50 字数 1718 浏览 3 评论 0原文

我正在使用本指南在heroku上集成wicked_pdf。但不知怎的,它似乎不起作用。我从 Heroku 获取日志,上面写着:

Processing PdfController#get_pdf to pdf (for 115.248.175.50 at 2011-02-15 23:54:44) [GET]
  Parameters: {"format"=>"pdf", "action"=>"get_pdf", "id"=>"1", "controller"=>"pdf"}
***************WICKED***************
Rendering pdf/get_pdf

RuntimeError (PDF could not be generated!
/usr/ruby1.8.7/lib/ruby/1.8/open3.rb:73:in `exec': No such file or directory - /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/bin/wkhtmltopdf-amd64       - -  (Errno::ENOENT)
    from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:73:in `popen3'
    from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:59:in `fork'
    from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:59:in `popen3'
    from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:57:in `fork'
    from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:57:in `popen3'
    from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/wicked_pdf.rb:22:in `pdf_from_string'
    from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/pdf_helper.rb:28:in `make_pdf'
    from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/pdf_helper.rb:39:in `make_and_send_pdf'
    from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/pdf_helper.rb:13:in `render'
    from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/app/controllers/schedule_controller.rb:33:in `get_pdf'

请帮忙。提前致谢。

编辑:

我在代码中做了一些更改,并解决了这个错误,但仍然卡在这里

NoMethodError (未定义的方法为空? for #)

I am using this guide for integrating wicked_pdf on heroku. But somehow it doesn't seem to work. I got the logs from heroku and its says this:

Processing PdfController#get_pdf to pdf (for 115.248.175.50 at 2011-02-15 23:54:44) [GET]
  Parameters: {"format"=>"pdf", "action"=>"get_pdf", "id"=>"1", "controller"=>"pdf"}
***************WICKED***************
Rendering pdf/get_pdf

RuntimeError (PDF could not be generated!
/usr/ruby1.8.7/lib/ruby/1.8/open3.rb:73:in `exec': No such file or directory - /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/bin/wkhtmltopdf-amd64       - -  (Errno::ENOENT)
    from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:73:in `popen3'
    from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:59:in `fork'
    from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:59:in `popen3'
    from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:57:in `fork'
    from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:57:in `popen3'
    from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/wicked_pdf.rb:22:in `pdf_from_string'
    from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/pdf_helper.rb:28:in `make_pdf'
    from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/pdf_helper.rb:39:in `make_and_send_pdf'
    from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/pdf_helper.rb:13:in `render'
    from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/app/controllers/schedule_controller.rb:33:in `get_pdf'

Please Help. Thanks in Advance.

EDIT:

I made some changes in my code and got through this error but still stuck here

NoMethodError (undefined method empty? for #<Pathname:0x2b7112392480>)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

痴者 2024-10-25 01:12:50

如何让 WickedPdf gem 在 Heroku 上工作

(请参阅下面的引用以获取网址)

  1. 在 Rails 应用程序的根目录中创建一个名为 bin 的文件夹。
  2. 下载 0.9.9 版本的 wkhtmltopdf 二进制文件并将其解压到 bin 文件夹中。您将需要适合您的开发系统的版本以及适用于 Heroku 的 AMD64 版本。暂时不要添加到您的 git 存储库。
  3. 设置 bin 文件夹中每个二进制文件的执行权限,例如 chmod +x bin/wkhtmltopdf-amd64
  4. 现在 git add bin
  5. 添加 wicked_pdf 到您的 Gemfile捆绑安装。不需要其他 wkhtmltopdf gem。
  6. config/initializers 文件夹中创建一个名为 wicked_pdf.rb 的文件,其源代码如下。
  7. git commit -am 'added wkhtmltopdf binaries and wicked_pdf gem'
  8. git push heroku

现在,您的 Rails 项目已针对 Heroku 配置完毕。从这里,添加 PDF 文件的程序逻辑。

** config/initializers/wicked_pdf.rb **

if Rails.env.production?
  wkhtmltopdf_path = "#{Rails.root}/bin/wkhtmltopdf-amd64"
else
  # Linux (check your processor for Intel x86 or AMD x64)
  # wkhtmltopdf_path = "#{Rails.root}/bin/wkhtmltopdf-amd64"
  # wkhtmltopdf_path = "#{Rails.root}/bin/wkhtmltopdf-i386"
  # OS X
  wkhtmltopdf_path = "#{Rails.root}/bin/wkhtmltopdf-0.9.9-OS-X.i386"            
  # Windows
  # wkhtmltopdf_path = 'C:\Program Files/wkhtmltopdf/wkhtmltopdf.exe'
end

WickedPdf.config = { exe_path: wkhtmltopdf_path, wkhtmltopdf: wkhtmltopdf_path }

我如何得出这个结论:

对于初学者来说,网络上有几篇关于 wkhtmltopdf 的误导性文章,wicked_pdf gem 和 Heroku
最新版本的 wkhtmltopdf (10.0+) 也存在导致执行挂起的问题。

最后,在某个时刻,WickedPdf 的配置变量名称肯定发生了变化,因为有几篇文章引用了 :exe_path,而其他文章则引用了 :wkhtmltopdf >。
甚至 WickedPdf 的 GitHub 文档也会在每个变量之间交替。

我的解决方案您不需要需要从源代码编译wkhtmltopdf
不需要需要从Homebrew安装wkhtmltopdf。如果您确实想要brew install wkhtmltopdf,我提供了解决方案。
您也不需要需要将任何“wkhtmltopdf”helper gem 添加到您的项目中。我尝试了 wkhtmltopdfwkhtmltopdf-herokuwkhtmltopdf-engineyardwkhtmltopdf-binary gems 和 GitHub 存储库。在当地工作得很好。 Heroku 没有运气。

从 Heroku 上的 Rails 控制台:
我通过运行 RUBY_PLATFORM 调查了运行 Heroku 服务器的 CPU,结果是 “x86_64-linux”
我还通过发出 Dir.entries('bin') 来查看 Heroku 上的 bin 文件夹内部实际存在的文件。我注意到我正在测试的一些 wkhtmltopdf gems 将它们的可执行文件放置在这个文件夹中。一条线索!
我运行 WickedPdf.new.pdf_from_string('Hello') 来查看输出错误并测试各种 WickedPdf 配置。

然后,在 @barlow 对配置 PDFKit 的回答中,有一个子注释给出了最终的密钥。在提交到 Git 之前,您必须wkhtmltopdf 二进制文件授予 Unix 执行权限。宾果!

引用:

  1. 官方 wkhtmltopdf 遗留静态二进制文件 http://wkhtmltopdf.org/old-downloads.html
  2. wicked_pdf https://github.com/mileszs/wicked_pdf
  3. Barlow 的 PDFKit.config https://stackoverflow.com/a/5098984/307308
  4. 如何使用 Homebrew 安装 wkhtmltopdf `https://stackoverflow.com/a/14043085/307308

在撰写本文时:

  • rails (3.2.13)
  • wicked_pdf (0.9.6)
  • wkhtmltopdf (0.9.9)

2015 年 2 月 12 日修订

dscout 开发了一个包含我答案概念的 gem。如果您的 Heroku 实例支持构建包,我建议使用 gem。

2015 年 3 月 27 日修订

于 适用于 Heroku(和 Linux AMD64 操作系统)wkhtmltopdf-heroku
它会自动检测是否安装了 pdfkit、wicked_pdf 和wisepdf gems。

How to get WickedPdf gem to work on Heroku

(see citation below for urls)

  1. Create a folder named bin in the root of your Rails app.
  2. Download and extract version 0.9.9 of the wkhtmltopdf binaries to the bin folder. You will need the version for your development system and the AMD64 version for Heroku. Do not add to your git repo yet.
  3. Set the execute permissions for each binary in the bin folder e.g. chmod +x bin/wkhtmltopdf-amd64
  4. Now git add bin
  5. Add wicked_pdf to your Gemfile and bundle install. No other wkhtmltopdf gems are needed.
  6. Create a file in the config/initializers folder called wicked_pdf.rb with the source code below.
  7. git commit -am 'added wkhtmltopdf binaries and wicked_pdf gem'
  8. git push heroku

Your Rails project is now configured for Heroku. From here, add your program logic for PDF files.

** config/initializers/wicked_pdf.rb **

if Rails.env.production?
  wkhtmltopdf_path = "#{Rails.root}/bin/wkhtmltopdf-amd64"
else
  # Linux (check your processor for Intel x86 or AMD x64)
  # wkhtmltopdf_path = "#{Rails.root}/bin/wkhtmltopdf-amd64"
  # wkhtmltopdf_path = "#{Rails.root}/bin/wkhtmltopdf-i386"
  # OS X
  wkhtmltopdf_path = "#{Rails.root}/bin/wkhtmltopdf-0.9.9-OS-X.i386"            
  # Windows
  # wkhtmltopdf_path = 'C:\Program Files/wkhtmltopdf/wkhtmltopdf.exe'
end

WickedPdf.config = { exe_path: wkhtmltopdf_path, wkhtmltopdf: wkhtmltopdf_path }

How I came to this conclusion:

For starters, there are SEVERAL misleading articles on the Web regarding wkhtmltopdf, the wicked_pdf gem, and Heroku.
There are also issues with recent versions of wkhtmltopdf (10.0+) that cause execution to hang.

Lastly, at some point there must have been a change in the config variable names for WickedPdf because several articles reference :exe_path while other articles reference :wkhtmltopdf.
Even WickedPdf's GitHub documentation alternates between each variable.

My solution you do not need to compile wkhtmltopdf from source.
You do not need to install wkhtmltopdf from Homebrew. I provided the solution to this if you really want to brew install wkhtmltopdf.
You also do not need to add any of the 'wkhtmltopdf' helper gems to your project. I tried wkhtmltopdf, wkhtmltopdf-heroku, wkhtmltopdf-engineyard, wkhtmltopdf-binary gems and GitHub repos. Worked great locally. No luck at Heroku.

From a Rails console on Heroku:
I investigated what CPU was running Heroku's servers by running RUBY_PLATFORM which yielded "x86_64-linux".
I also peeked inside the bin folder on Heroku by issuing the Dir.entries('bin') to see what files actually exist there. I noticed some of the wkhtmltopdf gems I was testing placed their executables in this folder. A clue!
I ran WickedPdf.new.pdf_from_string('Hello') to see the output errors and test various WickedPdf configurations.

Then, in @barlow's answer to configuring PDFKit, there is a subnote that gave the ultimate key. You must give Unix execute permissions to wkhtmltopdf the binary before committing to Git. Bingo!

Cite:

  1. Official wkhtmltopdf legacy static binaries http://wkhtmltopdf.org/old-downloads.html
  2. wicked_pdf https://github.com/mileszs/wicked_pdf
  3. Barlow's PDFKit.config https://stackoverflow.com/a/5098984/307308
  4. How to install wkhtmltopdf using Homebrew `https://stackoverflow.com/a/14043085/307308

At the time of this writing:

  • rails (3.2.13)
  • wicked_pdf (0.9.6)
  • wkhtmltopdf (0.9.9)

Amended 2/12/2015

dscout has developed a gem that encompasses the concepts of my answer. I recommend using the gem if your Heroku instance supports buildpacks.

Amended 3/27/2015

Another gem that works with Heroku (and Linux AMD64 OSes) wkhtmltopdf-heroku
It auto-detects if pdfkit, wicked_pdf and wisepdf gems are installed.

◇流星雨 2024-10-25 01:12:50

查看您的编辑部分后,其中显示“未定义方法空?对于 #

这意味着您在路径对象上使用空?函数,而空?是一个字符串而不是路径的函数

尝试找出您在哪里使用了路径对象并在该对象上使用 to_s

After looking at your Edit part which says "undefined method empty? for #<Pathname:0x2b...>

This means you are somewhere using empty? function on a path Object whereas empty? is a function of a string and not path.

try to find out where you have used a path Object and use to_s on that object.

Try it out.

安穩 2024-10-25 01:12:50

如果您将 wkhtmltopdf 的本地副本添加到您的存储库中,您可以在初始化程序中像这样指向它。

PDFKit.configure do |config|
config.wkhtmltopdf = "#{RAILS_ROOT}/lib/wkhtmltopdf"
end

记得在你的仓库中 chmod +x 你的 wkhtmltopdf 副本,然后再 - git add 它

希望这会有所帮助。

If you add a local copy of wkhtmltopdf to your repo you can point to it like this in an initializer.

PDFKit.configure do |config|
config.wkhtmltopdf = "#{RAILS_ROOT}/lib/wkhtmltopdf"
end

remember to chmod +x your wkhtmltopdf copy in your repo before you - git add it

Hope this helps.

残花月 2024-10-25 01:12:50

正在寻找 wicked_pdf 二进制文件: /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/bin/wkhtmltopdf-amd64

并且不存在,请将二进制文件放入应用程序中的文件夹中,然后然后更改文件 config/initializers/wicked_pdf.rb 以包括:

:exe_path => "#{Rails.root}/path/to/wkhtmltopdf-amd64"

如果 wicked_pdf 创建文件,您可能会遇到的另一个问题是 Heroku 文件系统只能读取,所以除非您找到一种方法来告诉wicked_pdf 在 tmp 目录中创建 pdf 文件,您可能无法使用它。

Is looking for the wicked_pdf binary in : /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/bin/wkhtmltopdf-amd64

And is not there, put the binary in a folder within your application and then change the file config/initializers/wicked_pdf.rb to include:

:exe_path => "#{Rails.root}/path/to/wkhtmltopdf-amd64"

Another problem you might come to if wicked_pdf create files, is the fact that Heroku filesystem is only read, so unless you find a way to tell wicked_pdf to create the pdfs in the tmp directory you might not be able to use it.

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