##// END OF EJS Templates
Use the correct local variables in the PDF export. #6382...
Eric Davis -
r3990:1809eefe4cd1
parent child
Show More
@@ -1,317 +1,317
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2009 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 'rfpdf/chinese'
23 23
24 24 module Redmine
25 25 module Export
26 26 module PDF
27 27 include ActionView::Helpers::TextHelper
28 28 include ActionView::Helpers::NumberHelper
29 29
30 30 class IFPDF < FPDF
31 31 include Redmine::I18n
32 32 attr_accessor :footer_date
33 33
34 34 def initialize(lang)
35 35 super()
36 36 set_language_if_valid lang
37 37 case current_language.to_s.downcase
38 38 when 'ko'
39 39 extend(PDF_Korean)
40 40 AddUHCFont()
41 41 @font_for_content = 'UHC'
42 42 @font_for_footer = 'UHC'
43 43 when 'ja'
44 44 extend(PDF_Japanese)
45 45 AddSJISFont()
46 46 @font_for_content = 'SJIS'
47 47 @font_for_footer = 'SJIS'
48 48 when 'zh'
49 49 extend(PDF_Chinese)
50 50 AddGBFont()
51 51 @font_for_content = 'GB'
52 52 @font_for_footer = 'GB'
53 53 when 'zh-tw'
54 54 extend(PDF_Chinese)
55 55 AddBig5Font()
56 56 @font_for_content = 'Big5'
57 57 @font_for_footer = 'Big5'
58 58 else
59 59 @font_for_content = 'Arial'
60 60 @font_for_footer = 'Helvetica'
61 61 end
62 62 SetCreator(Redmine::Info.app_name)
63 63 SetFont(@font_for_content)
64 64 end
65 65
66 66 def SetFontStyle(style, size)
67 67 SetFont(@font_for_content, style, size)
68 68 end
69 69
70 70 def SetTitle(txt)
71 71 txt = begin
72 72 utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', txt)
73 73 hextxt = "<FEFF" # FEFF is BOM
74 74 hextxt << utf16txt.unpack("C*").map {|x| sprintf("%02X",x) }.join
75 75 hextxt << ">"
76 76 rescue
77 77 txt
78 78 end || ''
79 79 super(txt)
80 80 end
81 81
82 82 def textstring(s)
83 83 # Format a text string
84 84 if s =~ /^</ # This means the string is hex-dumped.
85 85 return s
86 86 else
87 87 return '('+escape(s)+')'
88 88 end
89 89 end
90 90
91 91 def Cell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
92 92 @ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8')
93 93 # these quotation marks are not correctly rendered in the pdf
94 94 txt = txt.gsub(/[“�]/, '"') if txt
95 95 txt = begin
96 96 # 0x5c char handling
97 97 txtar = txt.split('\\')
98 98 txtar << '' if txt[-1] == ?\\
99 99 txtar.collect {|x| @ic.iconv(x)}.join('\\').gsub(/\\/, "\\\\\\\\")
100 100 rescue
101 101 txt
102 102 end || ''
103 103 super w,h,txt,border,ln,align,fill,link
104 104 end
105 105
106 106 def Footer
107 107 SetFont(@font_for_footer, 'I', 8)
108 108 SetY(-15)
109 109 SetX(15)
110 110 Cell(0, 5, @footer_date, 0, 0, 'L')
111 111 SetY(-15)
112 112 SetX(-30)
113 113 Cell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
114 114 end
115 115 end
116 116
117 117 # Returns a PDF string of a list of issues
118 118 def issues_to_pdf(issues, project, query)
119 119 pdf = IFPDF.new(current_language)
120 120 title = query.new_record? ? l(:label_issue_plural) : query.name
121 121 title = "#{project} - #{title}" if project
122 122 pdf.SetTitle(title)
123 123 pdf.AliasNbPages
124 124 pdf.footer_date = format_date(Date.today)
125 125 pdf.AddPage("L")
126 126
127 127 row_height = 6
128 128 col_width = []
129 129 unless query.columns.empty?
130 130 col_width = query.columns.collect {|column| column.name == :subject ? 4.0 : 1.0 }
131 131 ratio = 262.0 / col_width.inject(0) {|s,w| s += w}
132 132 col_width = col_width.collect {|w| w * ratio}
133 133 end
134 134
135 135 # title
136 136 pdf.SetFontStyle('B',11)
137 137 pdf.Cell(190,10, title)
138 138 pdf.Ln
139 139
140 140 # headers
141 141 pdf.SetFontStyle('B',8)
142 142 pdf.SetFillColor(230, 230, 230)
143 143 pdf.Cell(15, row_height, "#", 1, 0, 'L', 1)
144 144 query.columns.each_with_index do |column, i|
145 145 pdf.Cell(col_width[i], row_height, column.caption, 1, 0, 'L', 1)
146 146 end
147 147 pdf.Ln
148 148
149 149 # rows
150 150 pdf.SetFontStyle('',8)
151 151 pdf.SetFillColor(255, 255, 255)
152 152 previous_group = false
153 153 issues.each do |issue|
154 154 if query.grouped? && (group = query.group_by_column.value(issue)) != previous_group
155 155 pdf.SetFontStyle('B',9)
156 156 pdf.Cell(277, row_height,
157 (group.blank? ? 'None' : group.to_s) + " (#{@issue_count_by_group[group]})",
157 (group.blank? ? 'None' : group.to_s) + " (#{query.issue_count_by_group[group]})",
158 158 1, 1, 'L')
159 159 pdf.SetFontStyle('',8)
160 160 previous_group = group
161 161 end
162 162 pdf.Cell(15, row_height, issue.id.to_s, 1, 0, 'L', 1)
163 163 query.columns.each_with_index do |column, i|
164 164 s = if column.is_a?(QueryCustomFieldColumn)
165 165 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
166 166 show_value(cv)
167 167 else
168 168 value = issue.send(column.name)
169 169 if value.is_a?(Date)
170 170 format_date(value)
171 171 elsif value.is_a?(Time)
172 172 format_time(value)
173 173 else
174 174 value
175 175 end
176 176 end
177 177 pdf.Cell(col_width[i], row_height, s.to_s, 1, 0, 'L', 1)
178 178 end
179 179 pdf.Ln
180 180 end
181 181 if issues.size == Setting.issues_export_limit.to_i
182 182 pdf.SetFontStyle('B',10)
183 183 pdf.Cell(0, row_height, '...')
184 184 end
185 185 pdf.Output
186 186 end
187 187
188 188 # Returns a PDF string of a single issue
189 189 def issue_to_pdf(issue)
190 190 pdf = IFPDF.new(current_language)
191 191 pdf.SetTitle("#{issue.project} - ##{issue.tracker} #{issue.id}")
192 192 pdf.AliasNbPages
193 193 pdf.footer_date = format_date(Date.today)
194 194 pdf.AddPage
195 195
196 196 pdf.SetFontStyle('B',11)
197 197 pdf.Cell(190,10, "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}")
198 198 pdf.Ln
199 199
200 200 y0 = pdf.GetY
201 201
202 202 pdf.SetFontStyle('B',9)
203 203 pdf.Cell(35,5, l(:field_status) + ":","LT")
204 204 pdf.SetFontStyle('',9)
205 205 pdf.Cell(60,5, issue.status.to_s,"RT")
206 206 pdf.SetFontStyle('B',9)
207 207 pdf.Cell(35,5, l(:field_priority) + ":","LT")
208 208 pdf.SetFontStyle('',9)
209 209 pdf.Cell(60,5, issue.priority.to_s,"RT")
210 210 pdf.Ln
211 211
212 212 pdf.SetFontStyle('B',9)
213 213 pdf.Cell(35,5, l(:field_author) + ":","L")
214 214 pdf.SetFontStyle('',9)
215 215 pdf.Cell(60,5, issue.author.to_s,"R")
216 216 pdf.SetFontStyle('B',9)
217 217 pdf.Cell(35,5, l(:field_category) + ":","L")
218 218 pdf.SetFontStyle('',9)
219 219 pdf.Cell(60,5, issue.category.to_s,"R")
220 220 pdf.Ln
221 221
222 222 pdf.SetFontStyle('B',9)
223 223 pdf.Cell(35,5, l(:field_created_on) + ":","L")
224 224 pdf.SetFontStyle('',9)
225 225 pdf.Cell(60,5, format_date(issue.created_on),"R")
226 226 pdf.SetFontStyle('B',9)
227 227 pdf.Cell(35,5, l(:field_assigned_to) + ":","L")
228 228 pdf.SetFontStyle('',9)
229 229 pdf.Cell(60,5, issue.assigned_to.to_s,"R")
230 230 pdf.Ln
231 231
232 232 pdf.SetFontStyle('B',9)
233 233 pdf.Cell(35,5, l(:field_updated_on) + ":","LB")
234 234 pdf.SetFontStyle('',9)
235 235 pdf.Cell(60,5, format_date(issue.updated_on),"RB")
236 236 pdf.SetFontStyle('B',9)
237 237 pdf.Cell(35,5, l(:field_due_date) + ":","LB")
238 238 pdf.SetFontStyle('',9)
239 239 pdf.Cell(60,5, format_date(issue.due_date),"RB")
240 240 pdf.Ln
241 241
242 242 for custom_value in issue.custom_field_values
243 243 pdf.SetFontStyle('B',9)
244 244 pdf.Cell(35,5, custom_value.custom_field.name + ":","L")
245 245 pdf.SetFontStyle('',9)
246 246 pdf.MultiCell(155,5, (show_value custom_value),"R")
247 247 end
248 248
249 249 pdf.SetFontStyle('B',9)
250 250 pdf.Cell(35,5, l(:field_subject) + ":","LTB")
251 251 pdf.SetFontStyle('',9)
252 252 pdf.Cell(155,5, issue.subject,"RTB")
253 253 pdf.Ln
254 254
255 255 pdf.SetFontStyle('B',9)
256 256 pdf.Cell(35,5, l(:field_description) + ":")
257 257 pdf.SetFontStyle('',9)
258 pdf.MultiCell(155,5, @issue.description,"BR")
258 pdf.MultiCell(155,5, issue.description,"BR")
259 259
260 260 pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
261 261 pdf.Line(pdf.GetX, pdf.GetY, 170, pdf.GetY)
262 262 pdf.Ln
263 263
264 264 if issue.changesets.any? && User.current.allowed_to?(:view_changesets, issue.project)
265 265 pdf.SetFontStyle('B',9)
266 266 pdf.Cell(190,5, l(:label_associated_revisions), "B")
267 267 pdf.Ln
268 268 for changeset in issue.changesets
269 269 pdf.SetFontStyle('B',8)
270 270 pdf.Cell(190,5, format_time(changeset.committed_on) + " - " + changeset.author.to_s)
271 271 pdf.Ln
272 272 unless changeset.comments.blank?
273 273 pdf.SetFontStyle('',8)
274 274 pdf.MultiCell(190,5, changeset.comments)
275 275 end
276 276 pdf.Ln
277 277 end
278 278 end
279 279
280 280 pdf.SetFontStyle('B',9)
281 281 pdf.Cell(190,5, l(:label_history), "B")
282 282 pdf.Ln
283 283 for journal in issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
284 284 pdf.SetFontStyle('B',8)
285 285 pdf.Cell(190,5, format_time(journal.created_on) + " - " + journal.user.name)
286 286 pdf.Ln
287 287 pdf.SetFontStyle('I',8)
288 288 for detail in journal.details
289 289 pdf.Cell(190,5, "- " + show_detail(detail, true))
290 290 pdf.Ln
291 291 end
292 292 if journal.notes?
293 293 pdf.SetFontStyle('',8)
294 294 pdf.MultiCell(190,5, journal.notes)
295 295 end
296 296 pdf.Ln
297 297 end
298 298
299 299 if issue.attachments.any?
300 300 pdf.SetFontStyle('B',9)
301 301 pdf.Cell(190,5, l(:label_attachment_plural), "B")
302 302 pdf.Ln
303 303 for attachment in issue.attachments
304 304 pdf.SetFontStyle('',8)
305 305 pdf.Cell(80,5, attachment.filename)
306 306 pdf.Cell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
307 307 pdf.Cell(25,5, format_date(attachment.created_on),0,0,"R")
308 308 pdf.Cell(65,5, attachment.author.name,0,0,"R")
309 309 pdf.Ln
310 310 end
311 311 end
312 312 pdf.Output
313 313 end
314 314
315 315 end
316 316 end
317 317 end
General Comments 0
You need to be logged in to leave comments. Login now