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