在Wicked_pdf中使用Activestorage映像

发布于 2025-02-01 12:43:13 字数 562 浏览 5 评论 0原文

我无法获得激活图像来生产。我想在我生成的PDF主体内使用调整大小的图像(变体)。

= image_tag(@post.image.variant(resize_to_limit: [150, 100]))

它在开发中起作用,但是在生产生产中,PDF无限期地挂起,除非我将这条线排除在外。

我已经尝试了@post.image.variant(resize_to_limit:[150,100])。processed.url和设置rails.application.default_url_options = {主机: “}

具有讽刺意味的是,当我重新启动乘客时,它将PDF发送到浏览器,实际上看起来不错。包括图像。

这是类似的:

= wicked_pdf_image_tag(@post.image.variant(resize_to_limit: [150, 100]).processed.url)

Rails 7.0.3,Ruby 3.1.2,Wicked_pdf 2.6.3

I can't get ActiveStorage images to work in production. I want to use a resized image (variant) within the body of the PDF I'm generating.

= image_tag(@post.image.variant(resize_to_limit: [150, 100]))

It worked in development but in production generating the PDF hangs indefinitely unless I take that line out.

I've tried things like @post.image.variant(resize_to_limit: [150, 100]).processed.url and setting Rails.application.default_url_options = { host: "example.com" }

Ironically when I restart Passenger it sends the PDF to the browser and it actually looks fine. The image is included.

This is similar:

= wicked_pdf_image_tag(@post.image.variant(resize_to_limit: [150, 100]).processed.url)

Rails 7.0.3, Ruby 3.1.2, wicked_pdf 2.6.3

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

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

发布评论

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

评论(1

酒浓于脸红 2025-02-08 12:43:13

多亏了@unixmonkey,我添加了passenger_min_instances 3;到我的服务器块中的NGINX配置中,它最初起作用,但会在负载下悬挂乘客。由于我没有提高该数字时投掷的RAM,因此我根据文件的读取图像提出了另一种解决方案。

= image_tag(active_storage_to_base64_image(@post.image.variant(resize_to_limit: [150, 100])))

然后,我在application_helper.rb中创建了一个助手,

def active_storage_to_base64_image(image)
  require "base64"
  file = File.open(ActiveStorage::Blob.service.path_for(image.processed.key))
  base64 = Base64.encode64(file.read).gsub(/\s+/, '')
  file.close
  "data:image/png;base64,#{Rack::Utils.escape(base64)}"
end

我将其硬编码为PNG文件,因为这就是我所需要的。仅适用于磁盘存储。欢迎改进

Thanks to @Unixmonkey I added passenger_min_instances 3; to my server block in Nginx config and it worked initially but would hang Passenger under load. Since I didn't have the RAM to throw at increasing that number I came up with a different solution based on reading images from file.

= image_tag(active_storage_to_base64_image(@post.image.variant(resize_to_limit: [150, 100])))

Then I created a helper in application_helper.rb

def active_storage_to_base64_image(image)
  require "base64"
  file = File.open(ActiveStorage::Blob.service.path_for(image.processed.key))
  base64 = Base64.encode64(file.read).gsub(/\s+/, '')
  file.close
  "data:image/png;base64,#{Rack::Utils.escape(base64)}"
end

I've hard coded it for PNG files as that's all I needed. Only works for Disk storage. Would welcome improvements

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