Skip to content

Commit

Permalink
support optional format flag
Browse files Browse the repository at this point in the history
  • Loading branch information
JensRavens committed Dec 20, 2023
1 parent d7a05db commit e149c4a
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/pixelpress/renderers/weasyprint_renderer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Pixelpress::WeasyPrintRenderer
def render(html)

command = "#{executable_path} --encoding UTF-8 - -"
command = "#{executable_path} --encoding UTF-8 #{format} - -"

error = nil
result = nil
Expand All @@ -13,16 +13,21 @@ def render(html)
error = stderr.read
process = thread.value
end
raise "command failed (exitstatus=#{result.exitstatus}): #{command}\n #{error}" unless process.success?
raise "command failed (exitstatus=#{process.exitstatus}): #{command}\n #{error}" unless process.success?
return result
end

def version
`#{executable_path} --version`.chomp.scan(/(\d+\.\d+)/).flatten.first
`#{executable_path} --version`.chomp.then { _1.scan(/(\d+\.\d+)/).flatten.first || _1.scan(/(\d+)/).flatten.first }
end

private

def format
puts "WeasyPrint version: #{version}"
"-f pdf" if version.to_f >= 51
end

def executable_path
`which weasyprint`.chomp || raise(StandardError, "could not find weasyprint")
end
Expand Down

0 comments on commit e149c4a

Please sign in to comment.