Class: MakePDF::CommandBased::Writer

Inherits:
PDFWriter
  • Object
show all
Includes:
Arguments
Defined in:
lib/make_pdf/command_based.rb

Direct Known Subclasses

MakePDF::Chrome

Instance Attribute Summary collapse

Attributes included from Arguments

#base

Attributes inherited from PDFWriter

#logger, #output_dir, #source_url

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Arguments

#make_arguments

Methods inherited from PDFWriter

#make_output_filename, #make_pdf_filename, #make_relative_file, #make_source_url, #process

Methods included from PathManip

#relative_path

Constructor Details

#initialize(command: COMMAND, **options) ⇒ Writer

Returns a new instance of Writer.



30
31
32
33
# File 'lib/make_pdf/command_based.rb', line 30

def initialize(command: COMMAND, **options)
  super(**options)
  @command = command
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



23
24
25
# File 'lib/make_pdf/command_based.rb', line 23

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/make_pdf/command_based.rb', line 23

def options
  @options
end

Class Method Details

.prefixObject



26
27
28
# File 'lib/make_pdf/command_based.rb', line 26

def self.prefix
  '--'
end

Instance Method Details

#output_for(file, output_base_path: ".", version: [], **options) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/make_pdf/command_based.rb', line 52

def output_for(file, output_base_path: ".", version: [], **options)
  output_base_path = Pathname.new(output_base_path)
  file = Pathname.new(file)

  if @output_dir.nil?
    path = output_base_path / file.dirname
  else
    path = File.expand_path(relative_path(file, output_base_path), @output_dir)
  end

  FileUtils.mkdir_p path

  suffix = if version.empty? then "" else "_#{version.join("_")}" end
  File.expand_path(File.basename(file, ".html") + "#{suffix}.pdf", path)
end

#write(source_url, output_filename, **options) ⇒ Object

Raises:

  • (RuntimeError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/make_pdf/command_based.rb', line 35

def write(source_url, output_filename, **options)
  logger.info("converting #{source_url} with #{@command}")
  arguments = make_arguments(
    command: @command,
    source_url:,
    output_filename:,
    **options
  )
  logger.debug("Executing #{@command} #{arguments}")
  std_out = IO.popen([@command] + arguments, {:err =>[ :child, :out]}) do |pipe| 
    pipe.read
  end
  status = $?
  raise RuntimeError.new("Failure executing #{command} with #{arguments}.\n\noutput:\n\n---\n#{std_out}\n---\n") if status != 0
  logger.info("pdf-writer: Wrote #{output_filename}")
end