##// END OF EJS Templates
PDF: use explicit return value in Redmine::Export::PDF.fix_text_encoding() (#61)....
Toshi MARUYAMA -
r5478:c47144a14f71
parent child
Show More
@@ -1,402 +1,403
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2009 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 require 'iconv'
21 21 require 'rfpdf/fpdf'
22 22 require 'fpdf/chinese'
23 23 require 'fpdf/japanese'
24 24 require 'fpdf/korean'
25 25
26 26 module Redmine
27 27 module Export
28 28 module PDF
29 29 include ActionView::Helpers::TextHelper
30 30 include ActionView::Helpers::NumberHelper
31 31
32 32 class ITCPDF < TCPDF
33 33 include Redmine::I18n
34 34 attr_accessor :footer_date
35 35
36 36 def initialize(lang)
37 37 super()
38 38 set_language_if_valid lang
39 39 @font_for_content = 'FreeSans'
40 40 @font_for_footer = 'FreeSans'
41 41 SetCreator(Redmine::Info.app_name)
42 42 SetFont(@font_for_content)
43 43 end
44 44
45 45 def SetFontStyle(style, size)
46 46 SetFont(@font_for_content, style, size)
47 47 end
48 48
49 49 def SetTitle(txt)
50 50 txt = begin
51 51 utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', txt)
52 52 hextxt = "<FEFF" # FEFF is BOM
53 53 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
54 54 hextxt << ">"
55 55 rescue
56 56 txt
57 57 end || ''
58 58 super(txt)
59 59 end
60 60
61 61 def textstring(s)
62 62 # Format a text string
63 63 if s =~ /^</ # This means the string is hex-dumped.
64 64 return s
65 65 else
66 66 return '('+escape(s)+')'
67 67 end
68 68 end
69 69
70 70 alias RDMCell Cell
71 71 alias RDMMultiCell MultiCell
72 72
73 73 def Footer
74 74 SetFont(@font_for_footer, 'I', 8)
75 75 SetY(-15)
76 76 SetX(15)
77 77 RDMCell(0, 5, @footer_date, 0, 0, 'L')
78 78 SetY(-15)
79 79 SetX(-30)
80 80 RDMCell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
81 81 end
82 82 end
83 83
84 84 class IFPDF < FPDF
85 85 include Redmine::I18n
86 86 attr_accessor :footer_date
87 87
88 88 def initialize(lang)
89 89 super()
90 90 set_language_if_valid lang
91 91 case l(:general_pdf_encoding).upcase
92 92 when 'CP949'
93 93 extend(PDF_Korean)
94 94 AddUHCFont()
95 95 @font_for_content = 'UHC'
96 96 @font_for_footer = 'UHC'
97 97 when 'CP932'
98 98 extend(PDF_Japanese)
99 99 AddSJISFont()
100 100 @font_for_content = 'SJIS'
101 101 @font_for_footer = 'SJIS'
102 102 when 'GB18030'
103 103 extend(PDF_Chinese)
104 104 AddGBFont()
105 105 @font_for_content = 'GB'
106 106 @font_for_footer = 'GB'
107 107 when 'BIG5'
108 108 extend(PDF_Chinese)
109 109 AddBig5Font()
110 110 @font_for_content = 'Big5'
111 111 @font_for_footer = 'Big5'
112 112 else
113 113 @font_for_content = 'Arial'
114 114 @font_for_footer = 'Helvetica'
115 115 end
116 116 SetCreator(Redmine::Info.app_name)
117 117 SetFont(@font_for_content)
118 118 end
119 119
120 120 def SetFontStyle(style, size)
121 121 SetFont(@font_for_content, style, size)
122 122 end
123 123
124 124 def SetTitle(txt)
125 125 txt = begin
126 126 utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', txt)
127 127 hextxt = "<FEFF" # FEFF is BOM
128 128 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
129 129 hextxt << ">"
130 130 rescue
131 131 txt
132 132 end || ''
133 133 super(txt)
134 134 end
135 135
136 136 def textstring(s)
137 137 # Format a text string
138 138 if s =~ /^</ # This means the string is hex-dumped.
139 139 return s
140 140 else
141 141 return '('+escape(s)+')'
142 142 end
143 143 end
144 144
145 145 def fix_text_encoding(txt)
146 146 txt ||= ''
147 147 if txt.respond_to?(:force_encoding)
148 148 txt.force_encoding('UTF-8')
149 149 txt = txt.encode(l(:general_pdf_encoding), :invalid => :replace,
150 150 :undef => :replace, :replace => '?')
151 151 txt.force_encoding('ASCII-8BIT')
152 152 else
153 153 @ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8')
154 154 txtar = ""
155 155 begin
156 156 txtar += @ic.iconv(txt)
157 157 rescue Iconv::IllegalSequence
158 158 txtar += $!.success
159 159 txt = '?' + $!.failed[1,$!.failed.length]
160 160 retry
161 161 rescue
162 162 txtar += $!.success
163 163 end
164 164 txt = txtar
165 165 end
166 txt
166 167 end
167 168
168 169 def RDMCell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
169 170 Cell(w,h,fix_text_encoding(txt),border,ln,align,fill,link)
170 171 end
171 172
172 173 def RDMMultiCell(w,h=0,txt='',border=0,align='',fill=0)
173 174 MultiCell(w,h,fix_text_encoding(txt),border,align,fill)
174 175 end
175 176
176 177 def Footer
177 178 SetFont(@font_for_footer, 'I', 8)
178 179 SetY(-15)
179 180 SetX(15)
180 181 RDMCell(0, 5, @footer_date, 0, 0, 'L')
181 182 SetY(-15)
182 183 SetX(-30)
183 184 RDMCell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
184 185 end
185 186 alias alias_nb_pages AliasNbPages
186 187 end
187 188
188 189 # Returns a PDF string of a list of issues
189 190 def issues_to_pdf(issues, project, query)
190 191 if l(:general_pdf_encoding).upcase != 'UTF-8'
191 192 pdf = IFPDF.new(current_language)
192 193 else
193 194 pdf = ITCPDF.new(current_language)
194 195 end
195 196 title = query.new_record? ? l(:label_issue_plural) : query.name
196 197 title = "#{project} - #{title}" if project
197 198 pdf.SetTitle(title)
198 199 pdf.alias_nb_pages
199 200 pdf.footer_date = format_date(Date.today)
200 201 pdf.AddPage("L")
201 202
202 203 row_height = 6
203 204 col_width = []
204 205 unless query.columns.empty?
205 206 col_width = query.columns.collect {|column| column.name == :subject ? 4.0 : 1.0 }
206 207 ratio = 262.0 / col_width.inject(0) {|s,w| s += w}
207 208 col_width = col_width.collect {|w| w * ratio}
208 209 end
209 210
210 211 # title
211 212 pdf.SetFontStyle('B',11)
212 213 pdf.RDMCell(190,10, title)
213 214 pdf.Ln
214 215
215 216 # headers
216 217 pdf.SetFontStyle('B',8)
217 218 pdf.SetFillColor(230, 230, 230)
218 219 pdf.RDMCell(15, row_height, "#", 1, 0, 'L', 1)
219 220 query.columns.each_with_index do |column, i|
220 221 pdf.RDMCell(col_width[i], row_height, column.caption, 1, 0, 'L', 1)
221 222 end
222 223 pdf.Ln
223 224
224 225 # rows
225 226 pdf.SetFontStyle('',8)
226 227 pdf.SetFillColor(255, 255, 255)
227 228 previous_group = false
228 229 issues.each do |issue|
229 230 if query.grouped? &&
230 231 (group = query.group_by_column.value(issue)) != previous_group
231 232 pdf.SetFontStyle('B',9)
232 233 pdf.RDMCell(277, row_height,
233 234 (group.blank? ? 'None' : group.to_s) + " (#{query.issue_count_by_group[group]})",
234 235 1, 1, 'L')
235 236 pdf.SetFontStyle('',8)
236 237 previous_group = group
237 238 end
238 239 pdf.RDMCell(15, row_height, issue.id.to_s, 1, 0, 'L', 1)
239 240 query.columns.each_with_index do |column, i|
240 241 s = if column.is_a?(QueryCustomFieldColumn)
241 242 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
242 243 show_value(cv)
243 244 else
244 245 value = issue.send(column.name)
245 246 if value.is_a?(Date)
246 247 format_date(value)
247 248 elsif value.is_a?(Time)
248 249 format_time(value)
249 250 else
250 251 value
251 252 end
252 253 end
253 254 pdf.RDMCell(col_width[i], row_height, s.to_s, 1, 0, 'L', 1)
254 255 end
255 256 pdf.Ln
256 257 end
257 258 if issues.size == Setting.issues_export_limit.to_i
258 259 pdf.SetFontStyle('B',10)
259 260 pdf.RDMCell(0, row_height, '...')
260 261 end
261 262 pdf.Output
262 263 end
263 264
264 265 # Returns a PDF string of a single issue
265 266 def issue_to_pdf(issue)
266 267 if l(:general_pdf_encoding).upcase != 'UTF-8'
267 268 pdf = IFPDF.new(current_language)
268 269 else
269 270 pdf = ITCPDF.new(current_language)
270 271 end
271 272 pdf.SetTitle("#{issue.project} - ##{issue.tracker} #{issue.id}")
272 273 pdf.alias_nb_pages
273 274 pdf.footer_date = format_date(Date.today)
274 275 pdf.AddPage
275 276
276 277 pdf.SetFontStyle('B',11)
277 278 pdf.RDMCell(190,10,
278 279 "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}")
279 280 pdf.Ln
280 281
281 282 y0 = pdf.GetY
282 283
283 284 pdf.SetFontStyle('B',9)
284 285 pdf.RDMCell(35,5, l(:field_status) + ":","LT")
285 286 pdf.SetFontStyle('',9)
286 287 pdf.RDMCell(60,5, issue.status.to_s,"RT")
287 288 pdf.SetFontStyle('B',9)
288 289 pdf.RDMCell(35,5, l(:field_priority) + ":","LT")
289 290 pdf.SetFontStyle('',9)
290 291 pdf.RDMCell(60,5, issue.priority.to_s,"RT")
291 292 pdf.Ln
292 293
293 294 pdf.SetFontStyle('B',9)
294 295 pdf.RDMCell(35,5, l(:field_author) + ":","L")
295 296 pdf.SetFontStyle('',9)
296 297 pdf.RDMCell(60,5, issue.author.to_s,"R")
297 298 pdf.SetFontStyle('B',9)
298 299 pdf.RDMCell(35,5, l(:field_category) + ":","L")
299 300 pdf.SetFontStyle('',9)
300 301 pdf.RDMCell(60,5, issue.category.to_s,"R")
301 302 pdf.Ln
302 303
303 304 pdf.SetFontStyle('B',9)
304 305 pdf.RDMCell(35,5, l(:field_created_on) + ":","L")
305 306 pdf.SetFontStyle('',9)
306 307 pdf.RDMCell(60,5, format_date(issue.created_on),"R")
307 308 pdf.SetFontStyle('B',9)
308 309 pdf.RDMCell(35,5, l(:field_assigned_to) + ":","L")
309 310 pdf.SetFontStyle('',9)
310 311 pdf.RDMCell(60,5, issue.assigned_to.to_s,"R")
311 312 pdf.Ln
312 313
313 314 pdf.SetFontStyle('B',9)
314 315 pdf.RDMCell(35,5, l(:field_updated_on) + ":","LB")
315 316 pdf.SetFontStyle('',9)
316 317 pdf.RDMCell(60,5, format_date(issue.updated_on),"RB")
317 318 pdf.SetFontStyle('B',9)
318 319 pdf.RDMCell(35,5, l(:field_due_date) + ":","LB")
319 320 pdf.SetFontStyle('',9)
320 321 pdf.RDMCell(60,5, format_date(issue.due_date),"RB")
321 322 pdf.Ln
322 323
323 324 for custom_value in issue.custom_field_values
324 325 pdf.SetFontStyle('B',9)
325 326 pdf.RDMCell(35,5, custom_value.custom_field.name + ":","L")
326 327 pdf.SetFontStyle('',9)
327 328 pdf.RDMMultiCell(155,5, (show_value custom_value),"R")
328 329 end
329 330
330 331 pdf.SetFontStyle('B',9)
331 332 pdf.RDMCell(35,5, l(:field_subject) + ":","LTB")
332 333 pdf.SetFontStyle('',9)
333 334 pdf.RDMCell(155,5, issue.subject,"RTB")
334 335 pdf.Ln
335 336
336 337 pdf.SetFontStyle('B',9)
337 338 pdf.RDMCell(35,5, l(:field_description) + ":")
338 339 pdf.SetFontStyle('',9)
339 340 pdf.RDMMultiCell(155,5, issue.description.to_s,"BR")
340 341
341 342 pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
342 343 pdf.Line(pdf.GetX, pdf.GetY, 170, pdf.GetY)
343 344 pdf.Ln
344 345
345 346 if issue.changesets.any? &&
346 347 User.current.allowed_to?(:view_changesets, issue.project)
347 348 pdf.SetFontStyle('B',9)
348 349 pdf.RDMCell(190,5, l(:label_associated_revisions), "B")
349 350 pdf.Ln
350 351 for changeset in issue.changesets
351 352 pdf.SetFontStyle('B',8)
352 353 pdf.RDMCell(190,5,
353 354 format_time(changeset.committed_on) + " - " + changeset.author.to_s)
354 355 pdf.Ln
355 356 unless changeset.comments.blank?
356 357 pdf.SetFontStyle('',8)
357 358 pdf.RDMMultiCell(190,5, changeset.comments.to_s)
358 359 end
359 360 pdf.Ln
360 361 end
361 362 end
362 363
363 364 pdf.SetFontStyle('B',9)
364 365 pdf.RDMCell(190,5, l(:label_history), "B")
365 366 pdf.Ln
366 367 for journal in issue.journals.find(
367 368 :all, :include => [:user, :details],
368 369 :order => "#{Journal.table_name}.created_on ASC")
369 370 pdf.SetFontStyle('B',8)
370 371 pdf.RDMCell(190,5,
371 372 format_time(journal.created_on) + " - " + journal.user.name)
372 373 pdf.Ln
373 374 pdf.SetFontStyle('I',8)
374 375 for detail in journal.details
375 376 pdf.RDMCell(190,5, "- " + show_detail(detail, true))
376 377 pdf.Ln
377 378 end
378 379 if journal.notes?
379 380 pdf.SetFontStyle('',8)
380 381 pdf.RDMMultiCell(190,5, journal.notes.to_s)
381 382 end
382 383 pdf.Ln
383 384 end
384 385
385 386 if issue.attachments.any?
386 387 pdf.SetFontStyle('B',9)
387 388 pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
388 389 pdf.Ln
389 390 for attachment in issue.attachments
390 391 pdf.SetFontStyle('',8)
391 392 pdf.RDMCell(80,5, attachment.filename)
392 393 pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
393 394 pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
394 395 pdf.RDMCell(65,5, attachment.author.name,0,0,"R")
395 396 pdf.Ln
396 397 end
397 398 end
398 399 pdf.Output
399 400 end
400 401 end
401 402 end
402 403 end
General Comments 0
You need to be logged in to leave comments. Login now