Class: DocPDF::Adapters::Converters::Prawn

Inherits:
Base
  • Object
show all
Defined in:
lib/docpdf/adapters/converters/prawn.rb

Constant Summary collapse

MIME_TYPES =
%w[text/plain].freeze
LINE_SEPARATORS =

U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR mean "break the line", but Windows-1252 has no room for them, so Prawn's built-in fonts reject them. Text pasted out of word processors and web pages carries them routinely, so translate to a newline instead of failing on it.

/[

]/

Class Method Summary collapse

Class Method Details

.convert(data, _source_filename) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/docpdf/adapters/converters/prawn.rb', line 19

def convert(data, _source_filename)
  config = DocPDF.configuration
  opts = config.text_options
  content = normalize(data)
  pdf = ::Prawn::Document.new(page_size: config.page_size, margin: opts[:margins])
  register_font(pdf, opts[:font], opts[:font_file])
  pdf.font(opts[:font], size: opts[:font_size])
  pdf.text content, color: opts[:color]
  pdf.render
rescue *PRAWN_ERRORS, ::Encoding::UndefinedConversionError => e
  raise ConversionError, "Prawn failed to render text to PDF: #{e.message}"
end