简体   繁体   English

Rails Wicked_PDF ActiveStorage

[英]Rails Wicked_PDF ActiveStorage

I'm exporting PDF's in my Rails 6 application in a Cronjob with the crono gem.我正在使用 crono gem 在 Cronjob 中的 Rails 6 应用程序中导出 PDF。 This for itself works fine, my styles are applied to the table etc:这本身工作正常,我的样式应用于表格等:

def perform
        view = ActionController::Base.new
        view.extend(ApplicationHelper)
        view.extend(Rails.application.routes.url_helpers)
        # include helpers and routes
        view.instance_variable_set("@clinic_image", Configuration.first.image)
        view.instance_variable_set("@meal_triplet_week_vollkost", Meal.this_week_vollkost)
        view.instance_variable_set("@meal_triplet_week_schonkost", Meal.this_week_schonkost)
        view.instance_variable_set("@meal_triplet_week_vegetarisch", Meal.this_week_vegetarisch)
        view.instance_variable_set("@clinic_name", Configuration.first.clinicName)

        pdf = WickedPdf.new.pdf_from_string(
            view.render_to_string('meals/exportMealsCurrentWeek.html.erb', layout: 'pdf_week.html.erb'))

        save_path = Rails.root.join('pdfs','filename.pdf')
        File.open(save_path, 'wb') do |file|
            file << pdf
        end
    end

My problem is the following:我的问题如下:

When I call the action to render the PDF from the controller, the ActiveStorage Image from my database gets displayed without problems:当我调用该操作以从控制器呈现 PDF 时,我的数据库中的 ActiveStorage 图像将毫无问题地显示出来:

<%= image_tag(@clinic_image, size: "400x64", alt: "BDH Klinik Braunfels") %>

The @clinic_image is defined from my database in the default content for the application: @clinic_image 是从我的数据库中在应用程序的默认内容中定义的:

if Configuration.first.image.attached?
   @clinic_image = Configuration.first.image 
end

But in my PDF which is generated in the cronjob, I get the two following errors:但是在我在 cronjob 中生成的 PDF 中,我收到以下两个错误:

If I keep the image_tag in the view I get:如果我将 image_tag 保留在视图中,我会得到:

Can't resolve image into URL: undefined method `polymorphic_url' for #<#:0x000000000c119c28>无法将图像解析为 URL:#<#:0x000000000c119c28> 的未定义方法`polymorphic_url'

If I try to use the wicked_pdf_image_tag instead of normal image_tag helper I get the following error:如果我尝试使用wicked_pdf_image_tag而不是普通的 image_tag 助手,我会收到以下错误:

no implicit conversion of ActiveStorage::Attached::One into String没有将 ActiveStorage::Attached::One 隐式转换为 String

So I either need the proper URL (where I first have to download the image I think) or I need the ActiveStorage Image as a string.所以我要么需要正确的 URL(我首先必须下载我认为的图像)或者我需要 ActiveStorage Image 作为字符串。

I tried some "solutions" or better workarounds from the GitHub repo, but it doesn't seem to work for me (maybe I did something wrong, not sure).我从 GitHub 存储库中尝试了一些“解决方案”或更好的解决方法,但它似乎对我不起作用(也许我做错了什么,不确定)。

If I use the normal path to the image in my assets folder it*s working, but I always need the image which is set in the configuration.如果我使用资产文件夹中图像的正常路径,它可以正常工作,但我总是需要在配置中设置的图像。

Is there any solution to this problem?这个问题有什么解决办法吗?

通过使用 wicked_pdf_image_tag 自己修复了它,并编写了一个帮助函数,将请求放入资产管道中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM