Class: CFPrawn

Inherits:
Object
  • Object
show all
Includes:
BasicLogging, Prawn, Translating
Defined in:
lib/cfprawn.rb

Overview

An object of this class wrapps the Prawn PDF generator to convert a single message from an eml-file to PDF.

Constant Summary collapse

'008000'
@@DEF_REPLACEMENT_COLOR =
'808080'
@@DEF_CONTENT_TYPE_COLOR =
'000080'
@@DEF_REPLACEMENT =
"<?>"

Constants included from BasicLogging

BasicLogging::DEBUG, BasicLogging::ERROR, BasicLogging::FATAL, BasicLogging::INFO, BasicLogging::Levels, BasicLogging::UNKNOWN, BasicLogging::WARN

Instance Attribute Summary collapse

Attributes included from BasicLogging

#log_level, #target

Instance Method Summary collapse

Methods included from BasicLogging

is_muted?, #log, mute, #set_level, #set_target

Methods included from Translating

language, trl, #trl

Constructor Details

#initialize(mail, path) ⇒ CFPrawn

initialize a CFPrawn instance, prepare the tag-handlers for the processing of HTML-mail and begin splitting up the message into body, headers, attachments. Treat all of that some way or other to create a PDF-document.

[View source]

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cfprawn.rb', line 45

def initialize(mail, path) 
	pfx = $CONF.pdf_prefix
	sfx = $CONF.pdf_suffix
	page_size = $CONF.page_size
	page_size ||= 'A4'

	page_layout = $CONF.page_layout
	page_layout ||= 'portrait'
	
	@msg = nil		
	@doc = Document.new(:page_size => page_size, :page_layout => page_layout.to_sym)
	@parts_count = 0
	@headers = mail.headers ? mail.headers : []
	begin
		debug(trl("headers are %s") %[@headers.join(', ')])
        rescue Exception => e	
		warn(trl("Found a potential problem with the mail-headers.") << " " << e.message)
	end
	@body = mail.body
	@attachments = mail.attachments
	@attachment_option = $CONF.save_attachments
	@attachment_option ||= false
	check_fonts	
	render
	@outfile = path.dup << File::Separator << (pfx ? pfx : '') << File::basename(mail.filename) << (sfx ? sfx : '') << '.pdf'
	debug(Translating::trl("writing file %s") %outfile )

	if(@attachment_option && !@attachments.empty? )
		if('pdf' == @attachment_option)
			# create PDF, attach files
			@doc.render_file(@outfile)
			handle_attachments(@outfile)
		else
			# create attachment-files, link, then create PDF
			handle_attachments(@outfile)
			@doc.render_file(@outfile)
		end
	else
		# create directly PDF, ignore attachments.
		@doc.render_file(@outfile)
	end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Delegates method-calls to Prawn, or not.

[View source]

117
118
119
120
121
122
123
124
125
# File 'lib/cfprawn.rb', line 117

def method_missing(m, *args)
	debug('method missing ' << m.to_s << '( ' << args.join(', ') << ')')
	if(@doc.respond_to?(m) )
		@doc.send(m, *args)
	else 
		@msg = trl("The method %s is not defined for objects of type %s") %[m, self.class.name] 
		error(@msg)
	end
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.


383
384
385
# File 'lib/cfprawn.rb', line 383

def headers
  @headers
end

#msgObject (readonly)

Returns the value of attribute msg.


383
384
385
# File 'lib/cfprawn.rb', line 383

def msg
  @msg
end

#outfileObject (readonly)

Returns the value of attribute outfile.


383
384
385
# File 'lib/cfprawn.rb', line 383

def outfile
  @outfile
end

Instance Method Details

#check_fontsObject

Finds font-files and defines the font-settings for the PDF.

[View source]

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cfprawn.rb', line 89

def check_fonts()

	user_font = {}
        # ***BUGFIX*** Story: if one font-file does not exist, but the other does,
	# use both anyway and crash.
	# READ TWICE: I AM TOO DUMB TO PROGRAM !	
	msg = trl('user-defined fonts cannot be applied please verify your configuration.')

	{:normal=>$CONF.regular_font, :bold=>$CONF.bold_font, :italic=>$CONF.italic_font, :bold_itlic=>$CONF.bold_italic_font}.each_pair do |fs, font|
		if(font && !font.strip.empty?)
			if(File.exist?(font ))
				user_font[fs] = font	
			else
				msg << " (" << trl("font-file '%s' does not exist") %(font ? font.to_s : ' NIL ') << ")"
				warn(msg)
				# do not use *ANY* user-defined font
				return
			end
		else 
			warn(msg)
			return
		end
	end
	@doc.font_families.update("user_font" => user_font) if !user_font.empty?	
	@doc.font("user_font") if !user_font.empty?
	debug("using fonts: " << @doc.font_families.inspect)
end