##// END OF EJS Templates
PDF: switch encoding and font whether CJK and Thainland non UTF-8 or not in TCPDF Redmine class (#8312)....
Toshi MARUYAMA -
r5598:5742f78bd1b7
parent child
Show More
@@ -1,496 +1,523
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2011 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 super()
38 37 if RUBY_VERSION < '1.9'
39 38 @ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8')
40 39 end
40 pdf_encoding = l(:general_pdf_encoding).upcase
41 super('P', 'mm', 'A4', (pdf_encoding == 'UTF-8'), pdf_encoding )
41 42 set_language_if_valid lang
42 @font_for_content = 'FreeSans'
43 @font_for_footer = 'FreeSans'
43 case pdf_encoding
44 when 'UTF-8'
45 @font_for_content = 'FreeSans'
46 @font_for_footer = 'FreeSans'
47 when 'CP949'
48 extend(PDF_Korean)
49 AddUHCFont()
50 @font_for_content = 'UHC'
51 @font_for_footer = 'UHC'
52 when 'CP932'
53 extend(PDF_Japanese)
54 AddSJISFont()
55 @font_for_content = 'SJIS'
56 @font_for_footer = 'SJIS'
57 when 'GB18030'
58 extend(PDF_Chinese)
59 AddGBFont()
60 @font_for_content = 'GB'
61 @font_for_footer = 'GB'
62 when 'BIG5'
63 extend(PDF_Chinese)
64 AddBig5Font()
65 @font_for_content = 'Big5'
66 @font_for_footer = 'Big5'
67 else
68 @font_for_content = 'Arial'
69 @font_for_footer = 'Helvetica'
70 end
44 71 SetCreator(Redmine::Info.app_name)
45 72 SetFont(@font_for_content)
46 73 end
47 74
48 75 def SetFontStyle(style, size)
49 76 SetFont(@font_for_content, style, size)
50 77 end
51 78
52 79 def SetTitle(txt)
53 80 txt = begin
54 81 utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', txt)
55 82 hextxt = "<FEFF" # FEFF is BOM
56 83 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
57 84 hextxt << ">"
58 85 rescue
59 86 txt
60 87 end || ''
61 88 super(txt)
62 89 end
63 90
64 91 def textstring(s)
65 92 # Format a text string
66 93 if s =~ /^</ # This means the string is hex-dumped.
67 94 return s
68 95 else
69 96 return '('+escape(s)+')'
70 97 end
71 98 end
72 99
73 100 def fix_text_encoding(txt)
74 101 RDMPdfEncoding::rdm_pdf_iconv(@ic, txt)
75 102 end
76 103
77 104 def RDMCell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
78 105 Cell(w,h,fix_text_encoding(txt),border,ln,align,fill,link)
79 106 end
80 107
81 108 def RDMMultiCell(w,h=0,txt='',border=0,align='',fill=0)
82 109 MultiCell(w,h,fix_text_encoding(txt),border,align,fill)
83 110 end
84 111
85 112 def Footer
86 113 SetFont(@font_for_footer, 'I', 8)
87 114 SetY(-15)
88 115 SetX(15)
89 116 RDMCell(0, 5, @footer_date, 0, 0, 'L')
90 117 SetY(-15)
91 118 SetX(-30)
92 119 RDMCell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
93 120 end
94 121 end
95 122
96 123 class IFPDF < FPDF
97 124 include Redmine::I18n
98 125 attr_accessor :footer_date
99 126
100 127 def initialize(lang)
101 128 super()
102 129 if RUBY_VERSION < '1.9'
103 130 @ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8')
104 131 end
105 132 set_language_if_valid lang
106 133 case l(:general_pdf_encoding).upcase
107 134 when 'CP949'
108 135 extend(PDF_Korean)
109 136 AddUHCFont()
110 137 @font_for_content = 'UHC'
111 138 @font_for_footer = 'UHC'
112 139 when 'CP932'
113 140 extend(PDF_Japanese)
114 141 AddSJISFont()
115 142 @font_for_content = 'SJIS'
116 143 @font_for_footer = 'SJIS'
117 144 when 'GB18030'
118 145 extend(PDF_Chinese)
119 146 AddGBFont()
120 147 @font_for_content = 'GB'
121 148 @font_for_footer = 'GB'
122 149 when 'BIG5'
123 150 extend(PDF_Chinese)
124 151 AddBig5Font()
125 152 @font_for_content = 'Big5'
126 153 @font_for_footer = 'Big5'
127 154 else
128 155 @font_for_content = 'Arial'
129 156 @font_for_footer = 'Helvetica'
130 157 end
131 158 SetCreator(Redmine::Info.app_name)
132 159 SetFont(@font_for_content)
133 160 end
134 161
135 162 def SetFontStyle(style, size)
136 163 SetFont(@font_for_content, style, size)
137 164 end
138 165
139 166 def SetTitle(txt)
140 167 txt = begin
141 168 utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', txt)
142 169 hextxt = "<FEFF" # FEFF is BOM
143 170 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
144 171 hextxt << ">"
145 172 rescue
146 173 txt
147 174 end || ''
148 175 super(txt)
149 176 end
150 177
151 178 def textstring(s)
152 179 # Format a text string
153 180 if s =~ /^</ # This means the string is hex-dumped.
154 181 return s
155 182 else
156 183 return '('+escape(s)+')'
157 184 end
158 185 end
159 186
160 187 def fix_text_encoding(txt)
161 188 RDMPdfEncoding::rdm_pdf_iconv(@ic, txt)
162 189 end
163 190
164 191 def RDMCell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
165 192 Cell(w,h,fix_text_encoding(txt),border,ln,align,fill,link)
166 193 end
167 194
168 195 def RDMMultiCell(w,h=0,txt='',border=0,align='',fill=0)
169 196 MultiCell(w,h,fix_text_encoding(txt),border,align,fill)
170 197 end
171 198
172 199 def Footer
173 200 SetFont(@font_for_footer, 'I', 8)
174 201 SetY(-15)
175 202 SetX(15)
176 203 RDMCell(0, 5, @footer_date, 0, 0, 'L')
177 204 SetY(-15)
178 205 SetX(-30)
179 206 RDMCell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
180 207 end
181 208 alias alias_nb_pages AliasNbPages
182 209 end
183 210
184 211 # Returns a PDF string of a list of issues
185 212 def issues_to_pdf(issues, project, query)
186 213 if l(:general_pdf_encoding).upcase != 'UTF-8'
187 214 pdf = IFPDF.new(current_language)
188 215 else
189 216 pdf = ITCPDF.new(current_language)
190 217 end
191 218 title = query.new_record? ? l(:label_issue_plural) : query.name
192 219 title = "#{project} - #{title}" if project
193 220 pdf.SetTitle(title)
194 221 pdf.alias_nb_pages
195 222 pdf.footer_date = format_date(Date.today)
196 223 pdf.SetAutoPageBreak(false)
197 224 pdf.AddPage("L")
198 225
199 226 # Landscape A4 = 210 x 297 mm
200 227 page_height = 210
201 228 page_width = 297
202 229 right_margin = 10
203 230 bottom_margin = 20
204 231 col_id_width = 10
205 232 row_height = 5
206 233
207 234 # column widths
208 235 table_width = page_width - right_margin - 10 # fixed left margin
209 236 col_width = []
210 237 unless query.columns.empty?
211 238 col_width = query.columns.collect do |c|
212 239 (c.name == :subject || (c.is_a?(QueryCustomFieldColumn) && ['string', 'text'].include?(c.custom_field.field_format)))? 4.0 : 1.0
213 240 end
214 241 ratio = (table_width - col_id_width) / col_width.inject(0) {|s,w| s += w}
215 242 col_width = col_width.collect {|w| w * ratio}
216 243 end
217 244
218 245 # title
219 246 pdf.SetFontStyle('B',11)
220 247 pdf.RDMCell(190,10, title)
221 248 pdf.Ln
222 249
223 250 # headers
224 251 pdf.SetFontStyle('B',8)
225 252 pdf.SetFillColor(230, 230, 230)
226 253
227 254 # render it background to find the max height used
228 255 base_x = pdf.GetX
229 256 base_y = pdf.GetY
230 257 max_height = issues_to_pdf_write_cells(pdf, query.columns, col_width, row_height, true)
231 258 pdf.Rect(base_x, base_y, table_width, max_height, 'FD');
232 259 pdf.SetXY(base_x, base_y);
233 260
234 261 # write the cells on page
235 262 pdf.RDMCell(col_id_width, row_height, "#", "T", 0, 'C', 1)
236 263 issues_to_pdf_write_cells(pdf, query.columns, col_width, row_height, true)
237 264 issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width)
238 265 pdf.SetY(base_y + max_height);
239 266
240 267 # rows
241 268 pdf.SetFontStyle('',8)
242 269 pdf.SetFillColor(255, 255, 255)
243 270 previous_group = false
244 271 issues.each do |issue|
245 272 if query.grouped? &&
246 273 (group = query.group_by_column.value(issue)) != previous_group
247 274 pdf.SetFontStyle('B',9)
248 275 pdf.RDMCell(277, row_height,
249 276 (group.blank? ? 'None' : group.to_s) + " (#{query.issue_count_by_group[group]})",
250 277 1, 1, 'L')
251 278 pdf.SetFontStyle('',8)
252 279 previous_group = group
253 280 end
254 281 # fetch all the row values
255 282 col_values = query.columns.collect do |column|
256 283 s = if column.is_a?(QueryCustomFieldColumn)
257 284 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
258 285 show_value(cv)
259 286 else
260 287 value = issue.send(column.name)
261 288 if value.is_a?(Date)
262 289 format_date(value)
263 290 elsif value.is_a?(Time)
264 291 format_time(value)
265 292 else
266 293 value
267 294 end
268 295 end
269 296 s.to_s
270 297 end
271 298
272 299 # render it off-page to find the max height used
273 300 base_x = pdf.GetX
274 301 base_y = pdf.GetY
275 302 pdf.SetY(2 * page_height)
276 303 max_height = issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
277 304 pdf.SetXY(base_x, base_y)
278 305
279 306 # make new page if it doesn't fit on the current one
280 307 space_left = page_height - base_y - bottom_margin
281 308 if max_height > space_left
282 309 pdf.AddPage("L")
283 310 base_x = pdf.GetX
284 311 base_y = pdf.GetY
285 312 end
286 313
287 314 # write the cells on page
288 315 pdf.RDMCell(col_id_width, row_height, issue.id.to_s, "T", 0, 'C', 1)
289 316 issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
290 317 issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width)
291 318 pdf.SetY(base_y + max_height);
292 319 end
293 320
294 321 if issues.size == Setting.issues_export_limit.to_i
295 322 pdf.SetFontStyle('B',10)
296 323 pdf.RDMCell(0, row_height, '...')
297 324 end
298 325 pdf.Output
299 326 end
300 327
301 328 # Renders MultiCells and returns the maximum height used
302 329 def issues_to_pdf_write_cells(pdf, col_values, col_widths, row_height, head=false)
303 330 base_y = pdf.GetY
304 331 max_height = row_height
305 332 col_values.each_with_index do |column, i|
306 333 col_x = pdf.GetX
307 334 if head == true
308 335 pdf.RDMMultiCell(col_widths[i], row_height, column.caption, "T", 'L', 1)
309 336 else
310 337 pdf.RDMMultiCell(col_widths[i], row_height, column, "T", 'L', 1)
311 338 end
312 339 max_height = (pdf.GetY - base_y) if (pdf.GetY - base_y) > max_height
313 340 pdf.SetXY(col_x + col_widths[i], base_y);
314 341 end
315 342 return max_height
316 343 end
317 344
318 345 # Draw lines to close the row (MultiCell border drawing in not uniform)
319 346 def issues_to_pdf_draw_borders(pdf, top_x, top_y, lower_y, id_width, col_widths)
320 347 col_x = top_x + id_width
321 348 pdf.Line(col_x, top_y, col_x, lower_y) # id right border
322 349 col_widths.each do |width|
323 350 col_x += width
324 351 pdf.Line(col_x, top_y, col_x, lower_y) # columns right border
325 352 end
326 353 pdf.Line(top_x, top_y, top_x, lower_y) # left border
327 354 pdf.Line(top_x, lower_y, col_x, lower_y) # bottom border
328 355 end
329 356
330 357 # Returns a PDF string of a single issue
331 358 def issue_to_pdf(issue)
332 359 if l(:general_pdf_encoding).upcase != 'UTF-8'
333 360 pdf = IFPDF.new(current_language)
334 361 else
335 362 pdf = ITCPDF.new(current_language)
336 363 end
337 364 pdf.SetTitle("#{issue.project} - ##{issue.tracker} #{issue.id}")
338 365 pdf.alias_nb_pages
339 366 pdf.footer_date = format_date(Date.today)
340 367 pdf.AddPage
341 368 pdf.SetFontStyle('B',11)
342 369 pdf.RDMMultiCell(190,5, "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}")
343 370 pdf.Ln
344 371
345 372 y0 = pdf.GetY
346 373
347 374 pdf.SetFontStyle('B',9)
348 375 pdf.RDMCell(35,5, l(:field_status) + ":","LT")
349 376 pdf.SetFontStyle('',9)
350 377 pdf.RDMCell(60,5, issue.status.to_s,"RT")
351 378 pdf.SetFontStyle('B',9)
352 379 pdf.RDMCell(35,5, l(:field_priority) + ":","LT")
353 380 pdf.SetFontStyle('',9)
354 381 pdf.RDMCell(60,5, issue.priority.to_s,"RT")
355 382 pdf.Ln
356 383
357 384 pdf.SetFontStyle('B',9)
358 385 pdf.RDMCell(35,5, l(:field_author) + ":","L")
359 386 pdf.SetFontStyle('',9)
360 387 pdf.RDMCell(60,5, issue.author.to_s,"R")
361 388 pdf.SetFontStyle('B',9)
362 389 pdf.RDMCell(35,5, l(:field_category) + ":","L")
363 390 pdf.SetFontStyle('',9)
364 391 pdf.RDMCell(60,5, issue.category.to_s,"R")
365 392 pdf.Ln
366 393
367 394 pdf.SetFontStyle('B',9)
368 395 pdf.RDMCell(35,5, l(:field_created_on) + ":","L")
369 396 pdf.SetFontStyle('',9)
370 397 pdf.RDMCell(60,5, format_date(issue.created_on),"R")
371 398 pdf.SetFontStyle('B',9)
372 399 pdf.RDMCell(35,5, l(:field_assigned_to) + ":","L")
373 400 pdf.SetFontStyle('',9)
374 401 pdf.RDMCell(60,5, issue.assigned_to.to_s,"R")
375 402 pdf.Ln
376 403
377 404 pdf.SetFontStyle('B',9)
378 405 pdf.RDMCell(35,5, l(:field_updated_on) + ":","LB")
379 406 pdf.SetFontStyle('',9)
380 407 pdf.RDMCell(60,5, format_date(issue.updated_on),"RB")
381 408 pdf.SetFontStyle('B',9)
382 409 pdf.RDMCell(35,5, l(:field_due_date) + ":","LB")
383 410 pdf.SetFontStyle('',9)
384 411 pdf.RDMCell(60,5, format_date(issue.due_date),"RB")
385 412 pdf.Ln
386 413
387 414 for custom_value in issue.custom_field_values
388 415 pdf.SetFontStyle('B',9)
389 416 pdf.RDMCell(35,5, custom_value.custom_field.name + ":","L")
390 417 pdf.SetFontStyle('',9)
391 418 pdf.RDMMultiCell(155,5, (show_value custom_value),"R")
392 419 end
393 420
394 421 pdf.SetFontStyle('B',9)
395 422 pdf.RDMCell(35,5, l(:field_subject) + ":","LT")
396 423 pdf.SetFontStyle('',9)
397 424 pdf.RDMMultiCell(155,5, issue.subject,"RT")
398 425
399 426 pdf.SetFontStyle('B',9)
400 427 pdf.RDMCell(35,5, l(:field_description) + ":","LT")
401 428 pdf.SetFontStyle('',9)
402 429 pdf.RDMMultiCell(155,5, issue.description.to_s,"RT")
403 430
404 431 pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
405 432 pdf.Line(pdf.GetX, pdf.GetY, pdf.GetX + 190, pdf.GetY)
406 433 pdf.Ln
407 434
408 435 if issue.changesets.any? &&
409 436 User.current.allowed_to?(:view_changesets, issue.project)
410 437 pdf.SetFontStyle('B',9)
411 438 pdf.RDMCell(190,5, l(:label_associated_revisions), "B")
412 439 pdf.Ln
413 440 for changeset in issue.changesets
414 441 pdf.SetFontStyle('B',8)
415 442 pdf.RDMCell(190,5,
416 443 format_time(changeset.committed_on) + " - " + changeset.author.to_s)
417 444 pdf.Ln
418 445 unless changeset.comments.blank?
419 446 pdf.SetFontStyle('',8)
420 447 pdf.RDMMultiCell(190,5, changeset.comments.to_s)
421 448 end
422 449 pdf.Ln
423 450 end
424 451 end
425 452
426 453 pdf.SetFontStyle('B',9)
427 454 pdf.RDMCell(190,5, l(:label_history), "B")
428 455 pdf.Ln
429 456 for journal in issue.journals.find(
430 457 :all, :include => [:user, :details],
431 458 :order => "#{Journal.table_name}.created_on ASC")
432 459 pdf.SetFontStyle('B',8)
433 460 pdf.RDMCell(190,5,
434 461 format_time(journal.created_on) + " - " + journal.user.name)
435 462 pdf.Ln
436 463 pdf.SetFontStyle('I',8)
437 464 for detail in journal.details
438 465 pdf.RDMMultiCell(190,5, "- " + show_detail(detail, true))
439 466 end
440 467 if journal.notes?
441 468 pdf.Ln unless journal.details.empty?
442 469 pdf.SetFontStyle('',8)
443 470 pdf.RDMMultiCell(190,5, journal.notes.to_s)
444 471 end
445 472 pdf.Ln
446 473 end
447 474
448 475 if issue.attachments.any?
449 476 pdf.SetFontStyle('B',9)
450 477 pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
451 478 pdf.Ln
452 479 for attachment in issue.attachments
453 480 pdf.SetFontStyle('',8)
454 481 pdf.RDMCell(80,5, attachment.filename)
455 482 pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
456 483 pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
457 484 pdf.RDMCell(65,5, attachment.author.name,0,0,"R")
458 485 pdf.Ln
459 486 end
460 487 end
461 488 pdf.Output
462 489 end
463 490
464 491 class RDMPdfEncoding
465 492 include Redmine::I18n
466 493 def self.rdm_pdf_iconv(ic, txt)
467 494 txt ||= ''
468 495 if txt.respond_to?(:force_encoding)
469 496 txt.force_encoding('UTF-8')
470 497 if l(:general_pdf_encoding).upcase != 'UTF-8'
471 498 txt = txt.encode(l(:general_pdf_encoding), :invalid => :replace,
472 499 :undef => :replace, :replace => '?')
473 500 else
474 501 txt = Redmine::CodesetUtil.replace_invalid_utf8(txt)
475 502 end
476 503 txt.force_encoding('ASCII-8BIT')
477 504 else
478 505 ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8')
479 506 txtar = ""
480 507 begin
481 508 txtar += ic.iconv(txt)
482 509 rescue Iconv::IllegalSequence
483 510 txtar += $!.success
484 511 txt = '?' + $!.failed[1,$!.failed.length]
485 512 retry
486 513 rescue
487 514 txtar += $!.success
488 515 end
489 516 txt = txtar
490 517 end
491 518 txt
492 519 end
493 520 end
494 521 end
495 522 end
496 523 end
General Comments 0
You need to be logged in to leave comments. Login now