To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / lib / redmine / export / pdf.rb @ 442:753f1380d6bc

History | View | Annotate | Download (14.6 KB)

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