Generating file with PDFKit on Heroku - No such file or directory

余生长醉 提交于 2019-12-13 01:24:51

问题


I'm using PDFKit to create pdfs of from a given url within a Resque job on Heroku Cedar. My code looks like:

kit = PDFKit.new(url)
pdf = kit.to_file("/tmp/#{SecureRandom.hex}.pdf")

I then upload the file using fog to S3 for permanent storage. This job usually works, but also fails maybe a third of the time with:

No such file or directory - /tmp/a05c165fc80878b75fd15c447695de71.pdf

Manually running through the code in console will produce the same error.

According to the Heroku docs, I should be able to write a temporary file anywhere in the app's directory on Cedar. I've tried creating the tmp directory first (in console) but that didn't seem to change anything. Neither did saving to "#{Rails.root}/tmp/#{SecureRandom.hex}.pdf".

Any ideas would be greatly appreciated.

UPDATE

The full error in console is:

Error: Failed loading page http://grist.org/living/you-look-great-in-green-clothing-industry-gets-a-makeover-maybe.html
(sometimes it will work just to ignore this error with --load-error-handling ignore)
Errno::ENOENT: No such file or directory - /tmp/55a1d418074736decfd4e123d8e2bba2.pdf

It seems that maybe this is an error coming from wkhtmltopdf, however, I'm not sure where to add this flag if I'm using wkhtmltopdf via PDFkit.


回答1:


Looks like the solution, as the error I posted second suggested, was to ignore the load error. I did this with:

PDFKit.configure do |config|
  config.default_options[:load_error_handling] = 'ignore'
end

I wasn't seeing this warning at first because Resque was only showing me the final error. Thanks @ctshryock, your question made me think a little more about exactly where this error was coming from.



来源:https://stackoverflow.com/questions/15662756/generating-file-with-pdfkit-on-heroku-no-such-file-or-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!