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