You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issues_pdf_helper.rb 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2017 Jean-Philippe Lang
  5. #
  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. #
  11. # 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. #
  16. # 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. module Redmine
  20. module Export
  21. module PDF
  22. module IssuesPdfHelper
  23. # Returns a PDF string of a single issue
  24. def issue_to_pdf(issue, assoc={})
  25. pdf = ITCPDF.new(current_language)
  26. pdf.set_title("#{issue.project} - #{issue.tracker} ##{issue.id}")
  27. pdf.alias_nb_pages
  28. pdf.footer_date = format_date(User.current.today)
  29. pdf.add_page
  30. pdf.SetFontStyle('B',11)
  31. buf = "#{issue.project} - #{issue.tracker} ##{issue.id}"
  32. pdf.RDMMultiCell(190, 5, buf)
  33. pdf.SetFontStyle('',8)
  34. base_x = pdf.get_x
  35. i = 1
  36. issue.ancestors.visible.each do |ancestor|
  37. pdf.set_x(base_x + i)
  38. buf = "#{ancestor.tracker} # #{ancestor.id} (#{ancestor.status.to_s}): #{ancestor.subject}"
  39. pdf.RDMMultiCell(190 - i, 5, buf)
  40. i += 1 if i < 35
  41. end
  42. pdf.SetFontStyle('B',11)
  43. pdf.RDMMultiCell(190 - i, 5, issue.subject.to_s)
  44. pdf.SetFontStyle('',8)
  45. pdf.RDMMultiCell(190, 5, "#{format_time(issue.created_on)} - #{issue.author}")
  46. pdf.ln
  47. left = []
  48. left << [l(:field_status), issue.status]
  49. left << [l(:field_priority), issue.priority]
  50. left << [l(:field_assigned_to), issue.assigned_to] unless issue.disabled_core_fields.include?('assigned_to_id')
  51. left << [l(:field_category), issue.category] unless issue.disabled_core_fields.include?('category_id')
  52. left << [l(:field_fixed_version), issue.fixed_version] unless issue.disabled_core_fields.include?('fixed_version_id')
  53. right = []
  54. right << [l(:field_start_date), format_date(issue.start_date)] unless issue.disabled_core_fields.include?('start_date')
  55. right << [l(:field_due_date), format_date(issue.due_date)] unless issue.disabled_core_fields.include?('due_date')
  56. right << [l(:field_done_ratio), "#{issue.done_ratio}%"] unless issue.disabled_core_fields.include?('done_ratio')
  57. right << [l(:field_estimated_hours), l_hours(issue.estimated_hours)] unless issue.disabled_core_fields.include?('estimated_hours')
  58. right << [l(:label_spent_time), l_hours(issue.total_spent_hours)] if User.current.allowed_to?(:view_time_entries, issue.project)
  59. rows = left.size > right.size ? left.size : right.size
  60. while left.size < rows
  61. left << nil
  62. end
  63. while right.size < rows
  64. right << nil
  65. end
  66. custom_field_values = issue.visible_custom_field_values.reject {|value| value.custom_field.full_width_layout?}
  67. half = (custom_field_values.size / 2.0).ceil
  68. custom_field_values.each_with_index do |custom_value, i|
  69. (i < half ? left : right) << [custom_value.custom_field.name, show_value(custom_value, false)]
  70. end
  71. if pdf.get_rtl
  72. border_first_top = 'RT'
  73. border_last_top = 'LT'
  74. border_first = 'R'
  75. border_last = 'L'
  76. else
  77. border_first_top = 'LT'
  78. border_last_top = 'RT'
  79. border_first = 'L'
  80. border_last = 'R'
  81. end
  82. rows = left.size > right.size ? left.size : right.size
  83. rows.times do |i|
  84. heights = []
  85. pdf.SetFontStyle('B',9)
  86. item = left[i]
  87. heights << pdf.get_string_height(35, item ? "#{item.first}:" : "")
  88. item = right[i]
  89. heights << pdf.get_string_height(35, item ? "#{item.first}:" : "")
  90. pdf.SetFontStyle('',9)
  91. item = left[i]
  92. heights << pdf.get_string_height(60, item ? item.last.to_s : "")
  93. item = right[i]
  94. heights << pdf.get_string_height(60, item ? item.last.to_s : "")
  95. height = heights.max
  96. item = left[i]
  97. pdf.SetFontStyle('B',9)
  98. pdf.RDMMultiCell(35, height, item ? "#{item.first}:" : "", (i == 0 ? border_first_top : border_first), '', 0, 0)
  99. pdf.SetFontStyle('',9)
  100. pdf.RDMMultiCell(60, height, item ? item.last.to_s : "", (i == 0 ? border_last_top : border_last), '', 0, 0)
  101. item = right[i]
  102. pdf.SetFontStyle('B',9)
  103. pdf.RDMMultiCell(35, height, item ? "#{item.first}:" : "", (i == 0 ? border_first_top : border_first), '', 0, 0)
  104. pdf.SetFontStyle('',9)
  105. pdf.RDMMultiCell(60, height, item ? item.last.to_s : "", (i == 0 ? border_last_top : border_last), '', 0, 2)
  106. pdf.set_x(base_x)
  107. end
  108. pdf.SetFontStyle('B',9)
  109. pdf.RDMCell(35+155, 5, l(:field_description), "LRT", 1)
  110. pdf.SetFontStyle('',9)
  111. # Set resize image scale
  112. pdf.set_image_scale(1.6)
  113. text = textilizable(issue, :description,
  114. :only_path => false,
  115. :edit_section_links => false,
  116. :headings => false,
  117. :inline_attachments => false
  118. )
  119. pdf.RDMwriteFormattedCell(35+155, 5, '', '', text, issue.attachments, "LRB")
  120. custom_field_values = issue.visible_custom_field_values.select {|value| value.custom_field.full_width_layout?}
  121. custom_field_values.each do |value|
  122. text = show_value(value, false)
  123. next if text.blank?
  124. pdf.SetFontStyle('B',9)
  125. pdf.RDMCell(35+155, 5, value.custom_field.name, "LRT", 1)
  126. pdf.SetFontStyle('',9)
  127. pdf.RDMwriteHTMLCell(35+155, 5, '', '', text, issue.attachments, "LRB")
  128. end
  129. unless issue.leaf?
  130. truncate_length = (!is_cjk? ? 90 : 65)
  131. pdf.SetFontStyle('B',9)
  132. pdf.RDMCell(35+155,5, l(:label_subtask_plural) + ":", "LTR")
  133. pdf.ln
  134. issue_list(issue.descendants.visible.sort_by(&:lft)) do |child, level|
  135. buf = "#{child.tracker} # #{child.id}: #{child.subject}".
  136. truncate(truncate_length)
  137. level = 10 if level >= 10
  138. pdf.SetFontStyle('',8)
  139. pdf.RDMCell(35+135,5, (level >=1 ? " " * level : "") + buf, border_first)
  140. pdf.SetFontStyle('B',8)
  141. pdf.RDMCell(20,5, child.status.to_s, border_last)
  142. pdf.ln
  143. end
  144. end
  145. relations = issue.relations.select { |r| r.other_issue(issue).visible? }
  146. unless relations.empty?
  147. truncate_length = (!is_cjk? ? 80 : 60)
  148. pdf.SetFontStyle('B',9)
  149. pdf.RDMCell(35+155,5, l(:label_related_issues) + ":", "LTR")
  150. pdf.ln
  151. relations.each do |relation|
  152. buf = relation.to_s(issue) {|other|
  153. text = ""
  154. if Setting.cross_project_issue_relations?
  155. text += "#{relation.other_issue(issue).project} - "
  156. end
  157. text += "#{other.tracker} ##{other.id}: #{other.subject}"
  158. text
  159. }
  160. buf = buf.truncate(truncate_length)
  161. pdf.SetFontStyle('', 8)
  162. pdf.RDMCell(35+155-60, 5, buf, border_first)
  163. pdf.SetFontStyle('B',8)
  164. pdf.RDMCell(20,5, relation.other_issue(issue).status.to_s, "")
  165. pdf.RDMCell(20,5, format_date(relation.other_issue(issue).start_date), "")
  166. pdf.RDMCell(20,5, format_date(relation.other_issue(issue).due_date), border_last)
  167. pdf.ln
  168. end
  169. end
  170. pdf.RDMCell(190,5, "", "T")
  171. pdf.ln
  172. if issue.changesets.any? &&
  173. User.current.allowed_to?(:view_changesets, issue.project)
  174. pdf.SetFontStyle('B',9)
  175. pdf.RDMCell(190,5, l(:label_associated_revisions), "B")
  176. pdf.ln
  177. for changeset in issue.changesets
  178. pdf.SetFontStyle('B',8)
  179. csstr = "#{l(:label_revision)} #{changeset.format_identifier} - "
  180. csstr += format_time(changeset.committed_on) + " - " + changeset.author.to_s
  181. pdf.RDMCell(190, 5, csstr)
  182. pdf.ln
  183. unless changeset.comments.blank?
  184. pdf.SetFontStyle('',8)
  185. pdf.RDMwriteHTMLCell(190,5,'','',
  186. changeset.comments.to_s, issue.attachments, "")
  187. end
  188. pdf.ln
  189. end
  190. end
  191. if assoc[:journals].present?
  192. pdf.SetFontStyle('B',9)
  193. pdf.RDMCell(190,5, l(:label_history), "B")
  194. pdf.ln
  195. assoc[:journals].each do |journal|
  196. pdf.SetFontStyle('B',8)
  197. title = "##{journal.indice} - #{format_time(journal.created_on)} - #{journal.user}"
  198. title << " (#{l(:field_private_notes)})" if journal.private_notes?
  199. pdf.RDMCell(190,5, title)
  200. pdf.ln
  201. pdf.SetFontStyle('I',8)
  202. details_to_strings(journal.visible_details, true).each do |string|
  203. pdf.RDMMultiCell(190,5, "- " + string)
  204. end
  205. if journal.notes?
  206. pdf.ln unless journal.details.empty?
  207. pdf.SetFontStyle('',8)
  208. text = textilizable(journal, :notes,
  209. :only_path => false,
  210. :edit_section_links => false,
  211. :headings => false,
  212. :inline_attachments => false
  213. )
  214. pdf.RDMwriteFormattedCell(190,5,'','', text, issue.attachments, "")
  215. end
  216. pdf.ln
  217. end
  218. end
  219. if issue.attachments.any?
  220. pdf.SetFontStyle('B',9)
  221. pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
  222. pdf.ln
  223. for attachment in issue.attachments
  224. pdf.SetFontStyle('',8)
  225. pdf.RDMCell(80,5, attachment.filename)
  226. pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
  227. pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
  228. pdf.RDMCell(65,5, attachment.author.name,0,0,"R")
  229. pdf.ln
  230. end
  231. end
  232. pdf.output
  233. end
  234. # Returns a PDF string of a list of issues
  235. def issues_to_pdf(issues, project, query)
  236. pdf = ITCPDF.new(current_language, "L")
  237. title = query.new_record? ? l(:label_issue_plural) : query.name
  238. title = "#{project} - #{title}" if project
  239. pdf.set_title(title)
  240. pdf.alias_nb_pages
  241. pdf.footer_date = format_date(User.current.today)
  242. pdf.set_auto_page_break(false)
  243. pdf.add_page("L")
  244. # Landscape A4 = 210 x 297 mm
  245. page_height = pdf.get_page_height # 210
  246. page_width = pdf.get_page_width # 297
  247. left_margin = pdf.get_original_margins['left'] # 10
  248. right_margin = pdf.get_original_margins['right'] # 10
  249. bottom_margin = pdf.get_footer_margin
  250. row_height = 4
  251. # column widths
  252. table_width = page_width - right_margin - left_margin
  253. col_width = []
  254. unless query.inline_columns.empty?
  255. col_width = calc_col_width(issues, query, table_width, pdf)
  256. table_width = col_width.inject(0, :+)
  257. end
  258. # use full width if the description or last_notes are displayed
  259. if table_width > 0 && (query.has_column?(:description) || query.has_column?(:last_notes))
  260. col_width = col_width.map {|w| w * (page_width - right_margin - left_margin) / table_width}
  261. table_width = col_width.inject(0, :+)
  262. end
  263. # title
  264. pdf.SetFontStyle('B',11)
  265. pdf.RDMCell(190, 8, title)
  266. pdf.ln
  267. # totals
  268. totals = query.totals.map {|column, total| "#{column.caption}: #{total}"}
  269. if totals.present?
  270. pdf.SetFontStyle('B',10)
  271. pdf.RDMCell(table_width, 6, totals.join(" "), 0, 1, 'R')
  272. end
  273. totals_by_group = query.totals_by_group
  274. render_table_header(pdf, query, col_width, row_height, table_width)
  275. previous_group = false
  276. result_count_by_group = query.result_count_by_group
  277. issue_list(issues) do |issue, level|
  278. if query.grouped? &&
  279. (group = query.group_by_column.group_value(issue)) != previous_group
  280. pdf.SetFontStyle('B',10)
  281. group_label = group.blank? ? 'None' : group.to_s.dup
  282. group_label << " (#{result_count_by_group[group]})"
  283. pdf.bookmark group_label, 0, -1
  284. pdf.RDMCell(table_width, row_height * 2, group_label, 'LR', 1, 'L')
  285. pdf.SetFontStyle('',8)
  286. totals = totals_by_group.map {|column, total| "#{column.caption}: #{total[group]}"}.join(" ")
  287. if totals.present?
  288. pdf.RDMCell(table_width, row_height, totals, 'LR', 1, 'L')
  289. end
  290. previous_group = group
  291. end
  292. # fetch row values
  293. col_values = fetch_row_values(issue, query, level)
  294. # make new page if it doesn't fit on the current one
  295. base_y = pdf.get_y
  296. max_height = get_issues_to_pdf_write_cells(pdf, col_values, col_width)
  297. space_left = page_height - base_y - bottom_margin
  298. if max_height > space_left
  299. pdf.add_page("L")
  300. render_table_header(pdf, query, col_width, row_height, table_width)
  301. base_y = pdf.get_y
  302. end
  303. # write the cells on page
  304. issues_to_pdf_write_cells(pdf, col_values, col_width, max_height)
  305. pdf.set_y(base_y + max_height)
  306. if query.has_column?(:description) && issue.description?
  307. pdf.set_x(10)
  308. pdf.set_auto_page_break(true, bottom_margin)
  309. pdf.RDMwriteHTMLCell(0, 5, 10, '', issue.description.to_s, issue.attachments, "LRBT")
  310. pdf.set_auto_page_break(false)
  311. end
  312. if query.has_column?(:last_notes) && issue.last_notes.present?
  313. pdf.set_x(10)
  314. pdf.set_auto_page_break(true, bottom_margin)
  315. pdf.RDMwriteHTMLCell(0, 5, 10, '', issue.last_notes.to_s, [], "LRBT")
  316. pdf.set_auto_page_break(false)
  317. end
  318. end
  319. if issues.size == Setting.issues_export_limit.to_i
  320. pdf.SetFontStyle('B',10)
  321. pdf.RDMCell(0, row_height, '...')
  322. end
  323. pdf.output
  324. end
  325. def is_cjk?
  326. case current_language.to_s.downcase
  327. when 'ja', 'zh-tw', 'zh', 'ko'
  328. true
  329. else
  330. false
  331. end
  332. end
  333. # fetch row values
  334. def fetch_row_values(issue, query, level)
  335. query.inline_columns.collect do |column|
  336. s = if column.is_a?(QueryCustomFieldColumn)
  337. cv = issue.visible_custom_field_values.detect {|v| v.custom_field_id == column.custom_field.id}
  338. show_value(cv, false)
  339. else
  340. value = issue.send(column.name)
  341. case column.name
  342. when :subject
  343. value = " " * level + value
  344. when :attachments
  345. value = value.to_a.map {|a| a.filename}.join("\n")
  346. end
  347. if value.is_a?(Date)
  348. format_date(value)
  349. elsif value.is_a?(Time)
  350. format_time(value)
  351. elsif value.is_a?(Float)
  352. sprintf "%.2f", value
  353. else
  354. value
  355. end
  356. end
  357. s.to_s
  358. end
  359. end
  360. # calculate columns width
  361. def calc_col_width(issues, query, table_width, pdf)
  362. # calculate statistics
  363. # by captions
  364. pdf.SetFontStyle('B',8)
  365. margins = pdf.get_margins
  366. col_padding = margins['cell']
  367. col_width_min = query.inline_columns.map {|v| pdf.get_string_width(v.caption) + col_padding}
  368. col_width_max = Array.new(col_width_min)
  369. col_width_avg = Array.new(col_width_min)
  370. col_min = pdf.get_string_width('OO') + col_padding * 2
  371. if table_width > col_min * col_width_avg.length
  372. table_width -= col_min * col_width_avg.length
  373. else
  374. col_min = pdf.get_string_width('O') + col_padding * 2
  375. if table_width > col_min * col_width_avg.length
  376. table_width -= col_min * col_width_avg.length
  377. else
  378. ratio = table_width / col_width_avg.inject(0, :+)
  379. return col_width = col_width_avg.map {|w| w * ratio}
  380. end
  381. end
  382. word_width_max = query.inline_columns.map {|c|
  383. n = 10
  384. c.caption.split.each {|w|
  385. x = pdf.get_string_width(w) + col_padding
  386. n = x if n < x
  387. }
  388. n
  389. }
  390. # by properties of issues
  391. pdf.SetFontStyle('',8)
  392. k = 1
  393. issue_list(issues) {|issue, level|
  394. k += 1
  395. values = fetch_row_values(issue, query, level)
  396. values.each_with_index {|v,i|
  397. n = pdf.get_string_width(v) + col_padding * 2
  398. col_width_max[i] = n if col_width_max[i] < n
  399. col_width_min[i] = n if col_width_min[i] > n
  400. col_width_avg[i] += n
  401. v.split.each {|w|
  402. x = pdf.get_string_width(w) + col_padding
  403. word_width_max[i] = x if word_width_max[i] < x
  404. }
  405. }
  406. }
  407. col_width_avg.map! {|x| x / k}
  408. # calculate columns width
  409. ratio = table_width / col_width_avg.inject(0, :+)
  410. col_width = col_width_avg.map {|w| w * ratio}
  411. # correct max word width if too many columns
  412. ratio = table_width / word_width_max.inject(0, :+)
  413. word_width_max.map! {|v| v * ratio} if ratio < 1
  414. # correct and lock width of some columns
  415. done = 1
  416. col_fix = []
  417. col_width.each_with_index do |w,i|
  418. if w > col_width_max[i]
  419. col_width[i] = col_width_max[i]
  420. col_fix[i] = 1
  421. done = 0
  422. elsif w < word_width_max[i]
  423. col_width[i] = word_width_max[i]
  424. col_fix[i] = 1
  425. done = 0
  426. else
  427. col_fix[i] = 0
  428. end
  429. end
  430. # iterate while need to correct and lock coluns width
  431. while done == 0
  432. # calculate free & locked columns width
  433. done = 1
  434. ratio = table_width / col_width.inject(0, :+)
  435. # correct columns width
  436. col_width.each_with_index do |w,i|
  437. if col_fix[i] == 0
  438. col_width[i] = w * ratio
  439. # check if column width less then max word width
  440. if col_width[i] < word_width_max[i]
  441. col_width[i] = word_width_max[i]
  442. col_fix[i] = 1
  443. done = 0
  444. elsif col_width[i] > col_width_max[i]
  445. col_width[i] = col_width_max[i]
  446. col_fix[i] = 1
  447. done = 0
  448. end
  449. end
  450. end
  451. end
  452. ratio = table_width / col_width.inject(0, :+)
  453. col_width.map! {|v| v * ratio + col_min}
  454. col_width
  455. end
  456. def render_table_header(pdf, query, col_width, row_height, table_width)
  457. # headers
  458. pdf.SetFontStyle('B',8)
  459. pdf.set_fill_color(230, 230, 230)
  460. base_x = pdf.get_x
  461. base_y = pdf.get_y
  462. max_height = get_issues_to_pdf_write_cells(pdf, query.inline_columns, col_width, true)
  463. # write the cells on page
  464. issues_to_pdf_write_cells(pdf, query.inline_columns, col_width, max_height, true)
  465. pdf.set_xy(base_x, base_y + max_height)
  466. # rows
  467. pdf.SetFontStyle('',8)
  468. pdf.set_fill_color(255, 255, 255)
  469. end
  470. # returns the maximum height of MultiCells
  471. def get_issues_to_pdf_write_cells(pdf, col_values, col_widths, head=false)
  472. heights = []
  473. col_values.each_with_index do |column, i|
  474. heights << pdf.get_string_height(col_widths[i], head ? column.caption : column)
  475. end
  476. return heights.max
  477. end
  478. # Renders MultiCells and returns the maximum height used
  479. def issues_to_pdf_write_cells(pdf, col_values, col_widths, row_height, head=false)
  480. col_values.each_with_index do |column, i|
  481. pdf.RDMMultiCell(col_widths[i], row_height, head ? column.caption : column.strip, 1, '', 1, 0)
  482. end
  483. end
  484. # Draw lines to close the row (MultiCell border drawing in not uniform)
  485. #
  486. # parameter "col_id_width" is not used. it is kept for compatibility.
  487. def issues_to_pdf_draw_borders(pdf, top_x, top_y, lower_y,
  488. col_id_width, col_widths, rtl=false)
  489. col_x = top_x
  490. pdf.line(col_x, top_y, col_x, lower_y) # id right border
  491. col_widths.each do |width|
  492. if rtl
  493. col_x -= width
  494. else
  495. col_x += width
  496. end
  497. pdf.line(col_x, top_y, col_x, lower_y) # columns right border
  498. end
  499. pdf.line(top_x, top_y, top_x, lower_y) # left border
  500. pdf.line(top_x, lower_y, col_x, lower_y) # bottom border
  501. end
  502. end
  503. end
  504. end
  505. end