简体   繁体   中英

Rails Wicked_PDF ActiveStorage

I'm exporting PDF's in my Rails 6 application in a Cronjob with the crono gem. 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:

<%= 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:

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:

If I keep the image_tag in the view I get:

Can't resolve image into URL: undefined method `polymorphic_url' for #<#:0x000000000c119c28>

If I try to use the wicked_pdf_image_tag instead of normal image_tag helper I get the following error:

no implicit conversion of ActiveStorage::Attached::One into 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.

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).

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 自己修复了它,并编写了一个帮助函数,将请求放入资产管道中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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