pdf.rb_rfpdf115.patch

Jun NAITOH, 2011-03-27 09:34

Download (13.1 KB)

View differences:

lib/redmine/export/pdf.rb 2011-03-23 20:05:28.000000000 -0400
19 19

  
20 20
require 'iconv'
21 21
require 'rfpdf/fpdf'
22
require 'rfpdf/chinese'
22
require 'fpdf/chinese'
23
require 'fpdf/japanese'
24
require 'fpdf/korean'
23 25

  
24 26
module Redmine
25 27
  module Export
26 28
    module PDF
27 29
      include ActionView::Helpers::TextHelper
28 30
      include ActionView::Helpers::NumberHelper
29
      
30
      class IFPDF < FPDF
31
      include Redmine::I18n
32

  
33
      class IFPDF < (l(:general_pdf_encoding).upcase == 'UTF-8' ? TCPDF : FPDF )
31 34
        include Redmine::I18n
32 35
        attr_accessor :footer_date
33 36
        
34 37
        def initialize(lang)
35 38
          super()
36 39
          set_language_if_valid lang
40

  
41
          @font_for_content = 'FreeSans'
42
          @font_for_footer = 'FreeSans'              
43
          @is_utf8 = true if l(:general_pdf_encoding).upcase == 'UTF-8'
44

  
37 45
          case current_language.to_s.downcase
38 46
          when 'ko'
39
            extend(PDF_Korean)
40
            AddUHCFont()
41
            @font_for_content = 'UHC'
42
            @font_for_footer = 'UHC'
47
            if not @is_utf8
48
              extend(PDF_Korean)
49
              AddUHCFont()
50
              @font_for_content = 'UHC'
51
              @font_for_footer = 'UHC'
52
            end
43 53
          when 'ja'
44
            extend(PDF_Japanese)
45
            AddSJISFont()
46
            @font_for_content = 'SJIS'
47
            @font_for_footer = 'SJIS'
54
            if not @is_utf8
55
              extend(PDF_Japanese)
56
              AddSJISFont()
57
              @font_for_content = 'SJIS'
58
              @font_for_footer = 'SJIS'
59
            end
48 60
          when 'zh'
49
            extend(PDF_Chinese)
50
            AddGBFont()
51
            @font_for_content = 'GB'
52
            @font_for_footer = 'GB'
61
            if not @is_utf8
62
              extend(PDF_Chinese)
63
              AddGBFont()
64
              @font_for_content = 'GB'
65
              @font_for_footer = 'GB'
66
            end
53 67
          when 'zh-tw'
54
            extend(PDF_Chinese)
55
            AddBig5Font()
56
            @font_for_content = 'Big5'
57
            @font_for_footer = 'Big5'
68
            if not @is_utf8
69
              extend(PDF_Chinese)
70
              AddBig5Font()
71
              @font_for_content = 'Big5'
72
              @font_for_footer = 'Big5'
73
            end
58 74
          else
59
            @font_for_content = 'Arial'
60
            @font_for_footer = 'Helvetica'              
75
            if not @is_utf8
76
              @font_for_content = 'Arial'
77
              @font_for_footer = 'Helvetica'              
78
            end
61 79
          end
62 80
          SetCreator(Redmine::Info.app_name)
63 81
          SetFont(@font_for_content)
......
88 106
          end
89 107
        end
90 108
          
91
        def Cell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
109
        def fix_text_encoding(txt)
110
          return txt if @is_utf8
111

  
92 112
          @ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8')
93 113
          # these quotation marks are not correctly rendered in the pdf
94 114
          txt = txt.gsub(/[“�]/, '"') if txt
......
100 120
          rescue
101 121
            txt
102 122
          end || ''
103
          super w,h,txt,border,ln,align,fill,link
123
            return txt
104 124
        end
105
        
125

  
126
        def UTF8Cell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
127
            Cell(w,h,fix_text_encoding(txt),border,ln,align,fill,link)
128
        end
129

  
130
        def UTF8MultiCell(w,h=0,txt='',border=0,align='',fill=0)
131
            MultiCell(w,h,fix_text_encoding(txt),border,align,fill)
132
        end
133

  
106 134
        def Footer
107 135
          SetFont(@font_for_footer, 'I', 8)
108 136
          SetY(-15)
109 137
          SetX(15)
110
          Cell(0, 5, @footer_date, 0, 0, 'L')
138
          UTF8Cell(0, 5, @footer_date, 0, 0, 'L')
111 139
          SetY(-15)
112 140
          SetX(-30)
113
          Cell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
141
          UTF8Cell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
114 142
        end
143

  
144
        alias alias_nb_pages AliasNbPages if l(:general_pdf_encoding).upcase != 'UTF-8'
115 145
      end
116 146
      
117 147
      # Returns a PDF string of a list of issues
......
120 150
        title = query.new_record? ? l(:label_issue_plural) : query.name
121 151
        title = "#{project} - #{title}" if project
122 152
        pdf.SetTitle(title)
123
        pdf.AliasNbPages
153
        pdf.alias_nb_pages
124 154
        pdf.footer_date = format_date(Date.today)
125 155
        pdf.AddPage("L")
126 156
        
......
134 164
        
135 165
        # title
136 166
        pdf.SetFontStyle('B',11)    
137
        pdf.Cell(190,10, title)
167
        pdf.UTF8Cell(190,10, title)
138 168
        pdf.Ln
139 169
        
140 170
        # headers
141 171
        pdf.SetFontStyle('B',8)
142 172
        pdf.SetFillColor(230, 230, 230)
143
        pdf.Cell(15, row_height, "#", 1, 0, 'L', 1)
173
        pdf.UTF8Cell(15, row_height, "#", 1, 0, 'L', 1)
144 174
        query.columns.each_with_index do |column, i|
145
          pdf.Cell(col_width[i], row_height, column.caption, 1, 0, 'L', 1)
175
          pdf.UTF8Cell(col_width[i], row_height, column.caption, 1, 0, 'L', 1)
146 176
        end
147 177
        pdf.Ln
148 178
        
......
153 183
        issues.each do |issue|
154 184
          if query.grouped? && (group = query.group_by_column.value(issue)) != previous_group
155 185
            pdf.SetFontStyle('B',9)
156
            pdf.Cell(277, row_height, 
186
            pdf.UTF8Cell(277, row_height, 
157 187
              (group.blank? ? 'None' : group.to_s) + " (#{query.issue_count_by_group[group]})",
158 188
              1, 1, 'L')
159 189
            pdf.SetFontStyle('',8)
160 190
            previous_group = group
161 191
          end
162
          pdf.Cell(15, row_height, issue.id.to_s, 1, 0, 'L', 1)
192
          pdf.UTF8Cell(15, row_height, issue.id.to_s, 1, 0, 'L', 1)
163 193
          query.columns.each_with_index do |column, i|
164 194
            s = if column.is_a?(QueryCustomFieldColumn)
165 195
              cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
......
174 204
                value
175 205
              end
176 206
            end
177
            pdf.Cell(col_width[i], row_height, s.to_s, 1, 0, 'L', 1)
207
            pdf.UTF8Cell(col_width[i], row_height, s.to_s, 1, 0, 'L', 1)
178 208
          end
179 209
          pdf.Ln
180 210
        end
181 211
        if issues.size == Setting.issues_export_limit.to_i
182 212
          pdf.SetFontStyle('B',10)
183
          pdf.Cell(0, row_height, '...')
213
          pdf.UTF8Cell(0, row_height, '...')
184 214
        end
185 215
        pdf.Output
186 216
      end
......
189 219
      def issue_to_pdf(issue)
190 220
        pdf = IFPDF.new(current_language)
191 221
        pdf.SetTitle("#{issue.project} - ##{issue.tracker} #{issue.id}")
192
        pdf.AliasNbPages
222
        pdf.alias_nb_pages
193 223
        pdf.footer_date = format_date(Date.today)
194 224
        pdf.AddPage
195 225
        
196 226
        pdf.SetFontStyle('B',11)    
197
        pdf.Cell(190,10, "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}")
227
        pdf.UTF8Cell(190,10, "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}")
198 228
        pdf.Ln
199 229
        
200 230
        y0 = pdf.GetY
201 231
        
202 232
        pdf.SetFontStyle('B',9)
203
        pdf.Cell(35,5, l(:field_status) + ":","LT")
233
        pdf.UTF8Cell(35,5, l(:field_status) + ":","LT")
204 234
        pdf.SetFontStyle('',9)
205
        pdf.Cell(60,5, issue.status.to_s,"RT")
235
        pdf.UTF8Cell(60,5, issue.status.to_s,"RT")
206 236
        pdf.SetFontStyle('B',9)
207
        pdf.Cell(35,5, l(:field_priority) + ":","LT")
237
        pdf.UTF8Cell(35,5, l(:field_priority) + ":","LT")
208 238
        pdf.SetFontStyle('',9)
209
        pdf.Cell(60,5, issue.priority.to_s,"RT")        
239
        pdf.UTF8Cell(60,5, issue.priority.to_s,"RT")        
210 240
        pdf.Ln
211 241
        
212 242
        pdf.SetFontStyle('B',9)
213
        pdf.Cell(35,5, l(:field_author) + ":","L")
243
        pdf.UTF8Cell(35,5, l(:field_author) + ":","L")
214 244
        pdf.SetFontStyle('',9)
215
        pdf.Cell(60,5, issue.author.to_s,"R")
245
        pdf.UTF8Cell(60,5, issue.author.to_s,"R")
216 246
        pdf.SetFontStyle('B',9)
217
        pdf.Cell(35,5, l(:field_category) + ":","L")
247
        pdf.UTF8Cell(35,5, l(:field_category) + ":","L")
218 248
        pdf.SetFontStyle('',9)
219
        pdf.Cell(60,5, issue.category.to_s,"R")
249
        pdf.UTF8Cell(60,5, issue.category.to_s,"R")
220 250
        pdf.Ln   
221 251
        
222 252
        pdf.SetFontStyle('B',9)
223
        pdf.Cell(35,5, l(:field_created_on) + ":","L")
253
        pdf.UTF8Cell(35,5, l(:field_created_on) + ":","L")
224 254
        pdf.SetFontStyle('',9)
225
        pdf.Cell(60,5, format_date(issue.created_on),"R")
255
        pdf.UTF8Cell(60,5, format_date(issue.created_on),"R")
226 256
        pdf.SetFontStyle('B',9)
227
        pdf.Cell(35,5, l(:field_assigned_to) + ":","L")
257
        pdf.UTF8Cell(35,5, l(:field_assigned_to) + ":","L")
228 258
        pdf.SetFontStyle('',9)
229
        pdf.Cell(60,5, issue.assigned_to.to_s,"R")
259
        pdf.UTF8Cell(60,5, issue.assigned_to.to_s,"R")
230 260
        pdf.Ln
231 261
        
232 262
        pdf.SetFontStyle('B',9)
233
        pdf.Cell(35,5, l(:field_updated_on) + ":","LB")
263
        pdf.UTF8Cell(35,5, l(:field_updated_on) + ":","LB")
234 264
        pdf.SetFontStyle('',9)
235
        pdf.Cell(60,5, format_date(issue.updated_on),"RB")
265
        pdf.UTF8Cell(60,5, format_date(issue.updated_on),"RB")
236 266
        pdf.SetFontStyle('B',9)
237
        pdf.Cell(35,5, l(:field_due_date) + ":","LB")
267
        pdf.UTF8Cell(35,5, l(:field_due_date) + ":","LB")
238 268
        pdf.SetFontStyle('',9)
239
        pdf.Cell(60,5, format_date(issue.due_date),"RB")
269
        pdf.UTF8Cell(60,5, format_date(issue.due_date),"RB")
240 270
        pdf.Ln
241 271
        
242 272
        for custom_value in issue.custom_field_values
243 273
          pdf.SetFontStyle('B',9)
244
          pdf.Cell(35,5, custom_value.custom_field.name + ":","L")
274
          pdf.UTF8Cell(35,5, custom_value.custom_field.name + ":","L")
245 275
          pdf.SetFontStyle('',9)
246
          pdf.MultiCell(155,5, (show_value custom_value),"R")
276
          pdf.UTF8MultiCell(155,5, (show_value custom_value),"R")
247 277
        end
248 278
        
249 279
        pdf.SetFontStyle('B',9)
250
        pdf.Cell(35,5, l(:field_subject) + ":","LTB")
280
        pdf.UTF8Cell(35,5, l(:field_subject) + ":","LTB")
251 281
        pdf.SetFontStyle('',9)
252
        pdf.Cell(155,5, issue.subject,"RTB")
282
        pdf.UTF8Cell(155,5, issue.subject,"RTB")
253 283
        pdf.Ln    
254 284
        
255 285
        pdf.SetFontStyle('B',9)
256
        pdf.Cell(35,5, l(:field_description) + ":")
286
        pdf.UTF8Cell(35,5, l(:field_description) + ":")
257 287
        pdf.SetFontStyle('',9)
258
        pdf.MultiCell(155,5, issue.description.to_s,"BR")
288
        pdf.UTF8MultiCell(155,5, issue.description.to_s,"BR")
259 289
        
260 290
        pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
261 291
        pdf.Line(pdf.GetX, pdf.GetY, 170, pdf.GetY)
......
263 293
        
264 294
        if issue.changesets.any? && User.current.allowed_to?(:view_changesets, issue.project)
265 295
          pdf.SetFontStyle('B',9)
266
          pdf.Cell(190,5, l(:label_associated_revisions), "B")
296
          pdf.UTF8Cell(190,5, l(:label_associated_revisions), "B")
267 297
          pdf.Ln
268 298
          for changeset in issue.changesets
269 299
            pdf.SetFontStyle('B',8)
270
            pdf.Cell(190,5, format_time(changeset.committed_on) + " - " + changeset.author.to_s)
300
            pdf.UTF8Cell(190,5, format_time(changeset.committed_on) + " - " + changeset.author.to_s)
271 301
            pdf.Ln
272 302
            unless changeset.comments.blank?
273 303
              pdf.SetFontStyle('',8)
274
              pdf.MultiCell(190,5, changeset.comments.to_s)
304
              pdf.UTF8MultiCell(190,5, changeset.comments.to_s)
275 305
            end   
276 306
            pdf.Ln
277 307
          end
278 308
        end
279 309
        
280 310
        pdf.SetFontStyle('B',9)
281
        pdf.Cell(190,5, l(:label_history), "B")
311
        pdf.UTF8Cell(190,5, l(:label_history), "B")
282 312
        pdf.Ln  
283 313
        for journal in issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
284 314
          pdf.SetFontStyle('B',8)
285
          pdf.Cell(190,5, format_time(journal.created_on) + " - " + journal.user.name)
315
          pdf.UTF8Cell(190,5, format_time(journal.created_on) + " - " + journal.user.name)
286 316
          pdf.Ln
287 317
          pdf.SetFontStyle('I',8)
288 318
          for detail in journal.details
289
            pdf.Cell(190,5, "- " + show_detail(detail, true))
319
            pdf.UTF8Cell(190,5, "- " + show_detail(detail, true))
290 320
            pdf.Ln
291 321
          end
292 322
          if journal.notes?
293 323
            pdf.SetFontStyle('',8)
294
            pdf.MultiCell(190,5, journal.notes.to_s)
324
            pdf.UTF8MultiCell(190,5, journal.notes.to_s)
295 325
          end   
296 326
          pdf.Ln
297 327
        end
298 328
        
299 329
        if issue.attachments.any?
300 330
          pdf.SetFontStyle('B',9)
301
          pdf.Cell(190,5, l(:label_attachment_plural), "B")
331
          pdf.UTF8Cell(190,5, l(:label_attachment_plural), "B")
302 332
          pdf.Ln
303 333
          for attachment in issue.attachments
304 334
            pdf.SetFontStyle('',8)
305
            pdf.Cell(80,5, attachment.filename)
306
            pdf.Cell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
307
            pdf.Cell(25,5, format_date(attachment.created_on),0,0,"R")
308
            pdf.Cell(65,5, attachment.author.name,0,0,"R")
335
            pdf.UTF8Cell(80,5, attachment.filename)
336
            pdf.UTF8Cell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
337
            pdf.UTF8Cell(25,5, format_date(attachment.created_on),0,0,"R")
338
            pdf.UTF8Cell(65,5, attachment.author.name,0,0,"R")
309 339
            pdf.Ln
310 340
          end
311 341
        end