##// END OF EJS Templates
Wrap long text fields properly in PDF exports (#5629)....
Toshi MARUYAMA -
r5484:880e8e575a14
parent child
Show More
@@ -198,16 +198,28 module Redmine
198 pdf.SetTitle(title)
198 pdf.SetTitle(title)
199 pdf.alias_nb_pages
199 pdf.alias_nb_pages
200 pdf.footer_date = format_date(Date.today)
200 pdf.footer_date = format_date(Date.today)
201 pdf.SetAutoPageBreak(false)
201 pdf.AddPage("L")
202 pdf.AddPage("L")
202
203
203 row_height = 6
204 # Landscape A4 = 210 x 297 mm
205 page_height = 210
206 page_width = 297
207 right_margin = 10
208 bottom_margin = 20
209 col_id_width = 10
210 row_height = 5
211
212 # column widths
213 table_width = page_width - right_margin - 10 # fixed left margin
204 col_width = []
214 col_width = []
205 unless query.columns.empty?
215 unless query.columns.empty?
206 col_width = query.columns.collect {|column| column.name == :subject ? 4.0 : 1.0 }
216 col_width = query.columns.collect do |c|
207 ratio = 262.0 / col_width.inject(0) {|s,w| s += w}
217 (c.name == :subject || (c.is_a?(QueryCustomFieldColumn) && ['string', 'text'].include?(c.custom_field.field_format)))? 4.0 : 1.0
218 end
219 ratio = (table_width - col_id_width) / col_width.inject(0) {|s,w| s += w}
208 col_width = col_width.collect {|w| w * ratio}
220 col_width = col_width.collect {|w| w * ratio}
209 end
221 end
210
222
211 # title
223 # title
212 pdf.SetFontStyle('B',11)
224 pdf.SetFontStyle('B',11)
213 pdf.RDMCell(190,10, title)
225 pdf.RDMCell(190,10, title)
@@ -216,7 +228,7 module Redmine
216 # headers
228 # headers
217 pdf.SetFontStyle('B',8)
229 pdf.SetFontStyle('B',8)
218 pdf.SetFillColor(230, 230, 230)
230 pdf.SetFillColor(230, 230, 230)
219 pdf.RDMCell(15, row_height, "#", 1, 0, 'L', 1)
231 pdf.RDMCell(col_id_width, row_height, "#", 1, 0, 'C', 1)
220 query.columns.each_with_index do |column, i|
232 query.columns.each_with_index do |column, i|
221 pdf.RDMCell(col_width[i], row_height, column.caption, 1, 0, 'L', 1)
233 pdf.RDMCell(col_width[i], row_height, column.caption, 1, 0, 'L', 1)
222 end
234 end
@@ -236,8 +248,8 module Redmine
236 pdf.SetFontStyle('',8)
248 pdf.SetFontStyle('',8)
237 previous_group = group
249 previous_group = group
238 end
250 end
239 pdf.RDMCell(15, row_height, issue.id.to_s, 1, 0, 'L', 1)
251 # fetch all the row values
240 query.columns.each_with_index do |column, i|
252 col_values = query.columns.collect do |column|
241 s = if column.is_a?(QueryCustomFieldColumn)
253 s = if column.is_a?(QueryCustomFieldColumn)
242 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
254 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
243 show_value(cv)
255 show_value(cv)
@@ -251,17 +263,63 module Redmine
251 value
263 value
252 end
264 end
253 end
265 end
254 pdf.RDMCell(col_width[i], row_height, s.to_s, 1, 0, 'L', 1)
266 s.to_s
255 end
267 end
256 pdf.Ln
268
269 # render it off-page to find the max height used
270 base_x = pdf.GetX
271 base_y = pdf.GetY
272 pdf.SetY(2 * page_height)
273 max_height = issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
274 pdf.SetXY(base_x, base_y)
275
276 # make new page if it doesn't fit on the current one
277 space_left = page_height - base_y - bottom_margin
278 if max_height > space_left
279 pdf.AddPage("L")
280 base_x = pdf.GetX
281 base_y = pdf.GetY
282 end
283
284 # write the cells on page
285 pdf.RDMCell(col_id_width, row_height, issue.id.to_s, "T", 0, 'C', 1)
286 issues_to_pdf_write_cells(pdf, col_values, col_width, row_height)
287 issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width)
288 pdf.SetY(base_y + max_height);
257 end
289 end
290
258 if issues.size == Setting.issues_export_limit.to_i
291 if issues.size == Setting.issues_export_limit.to_i
259 pdf.SetFontStyle('B',10)
292 pdf.SetFontStyle('B',10)
260 pdf.RDMCell(0, row_height, '...')
293 pdf.RDMCell(0, row_height, '...')
261 end
294 end
262 pdf.Output
295 pdf.Output
263 end
296 end
264
297
298 # Renders MultiCells and returns the maximum height used
299 def issues_to_pdf_write_cells(pdf, col_values, col_widths, row_height)
300 base_y = pdf.GetY
301 max_height = row_height
302 col_values.each_with_index do |column, i|
303 col_x = pdf.GetX
304 pdf.RDMMultiCell(col_widths[i], row_height, col_values[i], "T", 'L', 1)
305 max_height = (pdf.GetY - base_y) if (pdf.GetY - base_y) > max_height
306 pdf.SetXY(col_x + col_widths[i], base_y);
307 end
308 return max_height
309 end
310
311 # Draw lines to close the row (MultiCell border drawing in not uniform)
312 def issues_to_pdf_draw_borders(pdf, top_x, top_y, lower_y, id_width, col_widths)
313 col_x = top_x + id_width
314 pdf.Line(col_x, top_y, col_x, lower_y) # id right border
315 col_widths.each do |width|
316 col_x += width
317 pdf.Line(col_x, top_y, col_x, lower_y) # columns right border
318 end
319 pdf.Line(top_x, top_y, top_x, lower_y) # left border
320 pdf.Line(top_x, lower_y, col_x, lower_y) # bottom border
321 end
322
265 # Returns a PDF string of a single issue
323 # Returns a PDF string of a single issue
266 def issue_to_pdf(issue)
324 def issue_to_pdf(issue)
267 if l(:general_pdf_encoding).upcase != 'UTF-8'
325 if l(:general_pdf_encoding).upcase != 'UTF-8'
@@ -273,14 +331,12 module Redmine
273 pdf.alias_nb_pages
331 pdf.alias_nb_pages
274 pdf.footer_date = format_date(Date.today)
332 pdf.footer_date = format_date(Date.today)
275 pdf.AddPage
333 pdf.AddPage
276
334 pdf.SetFontStyle('B',11)
277 pdf.SetFontStyle('B',11)
335 pdf.RDMMultiCell(190,5, "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}")
278 pdf.RDMCell(190,10,
279 "#{issue.project} - #{issue.tracker} # #{issue.id}: #{issue.subject}")
280 pdf.Ln
336 pdf.Ln
281
337
282 y0 = pdf.GetY
338 y0 = pdf.GetY
283
339
284 pdf.SetFontStyle('B',9)
340 pdf.SetFontStyle('B',9)
285 pdf.RDMCell(35,5, l(:field_status) + ":","LT")
341 pdf.RDMCell(35,5, l(:field_status) + ":","LT")
286 pdf.SetFontStyle('',9)
342 pdf.SetFontStyle('',9)
@@ -329,18 +385,17 module Redmine
329 end
385 end
330
386
331 pdf.SetFontStyle('B',9)
387 pdf.SetFontStyle('B',9)
332 pdf.RDMCell(35,5, l(:field_subject) + ":","LTB")
388 pdf.RDMCell(35,5, l(:field_subject) + ":","LT")
333 pdf.SetFontStyle('',9)
389 pdf.SetFontStyle('',9)
334 pdf.RDMCell(155,5, issue.subject,"RTB")
390 pdf.RDMMultiCell(155,5, issue.subject,"RT")
335 pdf.Ln
391
336
337 pdf.SetFontStyle('B',9)
392 pdf.SetFontStyle('B',9)
338 pdf.RDMCell(35,5, l(:field_description) + ":")
393 pdf.RDMCell(35,5, l(:field_description) + ":","LT")
339 pdf.SetFontStyle('',9)
394 pdf.SetFontStyle('',9)
340 pdf.RDMMultiCell(155,5, issue.description.to_s,"BR")
395 pdf.RDMMultiCell(155,5, issue.description.to_s,"RT")
341
396
342 pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
397 pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
343 pdf.Line(pdf.GetX, pdf.GetY, 170, pdf.GetY)
398 pdf.Line(pdf.GetX, pdf.GetY, pdf.GetX + 190, pdf.GetY)
344 pdf.Ln
399 pdf.Ln
345
400
346 if issue.changesets.any? &&
401 if issue.changesets.any? &&
@@ -373,10 +428,10 module Redmine
373 pdf.Ln
428 pdf.Ln
374 pdf.SetFontStyle('I',8)
429 pdf.SetFontStyle('I',8)
375 for detail in journal.details
430 for detail in journal.details
376 pdf.RDMCell(190,5, "- " + show_detail(detail, true))
431 pdf.RDMMultiCell(190,5, "- " + show_detail(detail, true))
377 pdf.Ln
378 end
432 end
379 if journal.notes?
433 if journal.notes?
434 pdf.Ln unless journal.details.empty?
380 pdf.SetFontStyle('',8)
435 pdf.SetFontStyle('',8)
381 pdf.RDMMultiCell(190,5, journal.notes.to_s)
436 pdf.RDMMultiCell(190,5, journal.notes.to_s)
382 end
437 end
General Comments 0
You need to be logged in to leave comments. Login now