Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow view template to be HTML #9

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

mynameisnotbruce
Copy link
Contributor

Relaxed treats input based on file extension (.pug vs .html), but previous code always created a temporary file "in.pug"; this could lead to relaxed treating HTML code as PUG code, resulting in errors. The change retains the view template's file extension for the temporary file and removes ".erb" if present.

The relaxed console command seems to rely on the file extension to decide how to treat a file
Heroku complains about unavailable sandboxes, this might fix it
@mabras
Copy link

mabras commented May 17, 2020

Hi @igorkasyanchuk,

The gem is not usable with html.erb without this. Do have plan to merge?

Thanks.

@igorkasyanchuk
Copy link
Owner

I think I'll, but I don't like this change --no-sandbox, I would like to see at configuration option

Can you do it?

@revskill10
Copy link

Could we update to Rails 7 ?

@igorkasyanchuk
Copy link
Owner

@revskill10 if you can help with upgrade it would be great

@revskill10
Copy link

revskill10 commented May 22, 2023

Hi @igorkasyanchuk I have to make some changes to make it work, but it's a bit buggy.

def render(&block)
      controller = ActionController::Base.new
      ctx = ActionView::LookupContext.new(ActionController::Base.view_paths)
      view = ActionView::Base.new(ctx, {}, controller)
      params = { file: @file, layout: @layout }
      params = params.merge(locals: @locals) if @locals.present?
      content = view.render(params)
      template = ERB.new(content)
      content = template.result

      logger.debug "RailsPDF ====="
      logger.debug "RailsPDF content:\n#{content}"
      logger.debug "RailsPDF ====="

      begin
        input  = BetterTempfile.new("in.pug")
        output = BetterTempfile.new("out.pdf")

        input.write(content)
        input.flush

        command = "#{RailsPDF.relaxed} #{input.path.to_s} #{output.path.to_s} --basedir / --build-once --no-sandbox"
        stdin, stdout, wait_thr = Open3.popen2e(command, pgroup: true)
        sleep(5)
        pgid = Process.getpgid(wait_thr.pid)
        Process.kill '-TERM', pgid
        logger.debug "RailsPDF ===== #{command}"
        output.rewind
        output.binmode

        data = output.read

        yield(data)
      ensure
        input.try(:close!)
        output.try(:close!)
      end
    end
    ```

1. I need to use ERB to render the template
2. I need to wait and kill child process (even with the --build-once arg, the relaxed process still watch for changes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants