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.

pdf.rb 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # frozen_string_literal: true
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2019 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. require 'rbpdf'
  20. module Redmine
  21. module Export
  22. module PDF
  23. class ITCPDF < RBPDF
  24. include Redmine::I18n
  25. attr_accessor :footer_date
  26. def initialize(lang, orientation='P')
  27. set_language_if_valid lang
  28. super(orientation, 'mm', 'A4')
  29. set_print_header(false)
  30. set_rtl(l(:direction) == 'rtl')
  31. @font_for_content = l(:general_pdf_fontname)
  32. @monospaced_font_for_content = l(:general_pdf_monospaced_fontname)
  33. @font_for_footer = l(:general_pdf_fontname)
  34. set_creator(Redmine::Info.app_name)
  35. set_font(@font_for_content)
  36. set_header_font([@font_for_content, '', 10])
  37. set_footer_font([@font_for_content, '', 8])
  38. set_default_monospaced_font(@monospaced_font_for_content)
  39. set_display_mode('default', 'OneColumn')
  40. end
  41. def SetFontStyle(style, size)
  42. set_font(@font_for_content, style, size)
  43. end
  44. def SetFont(family, style='', size=0, fontfile='')
  45. style = +style
  46. # FreeSerif Bold Thai font has problem.
  47. style.delete!('B') if family.to_s.casecmp('freeserif') == 0
  48. # DejaVuSans Italic Arabic and Persian font has problem.
  49. style.delete!('I') if family.to_s.casecmp('dejavusans') == 0 && current_language.to_s.casecmp("vi") != 0
  50. # DejaVuSansMono Italic Arabic font has problem
  51. style.delete!('I') if family.to_s.casecmp('dejavusansmono') == 0
  52. super(family, style, size, fontfile)
  53. end
  54. alias_method :set_font, :SetFont
  55. def fix_text_encoding(txt)
  56. RDMPdfEncoding::rdm_from_utf8(txt, "UTF-8")
  57. end
  58. def formatted_text(text)
  59. Redmine::WikiFormatting.to_html(Setting.text_formatting, text)
  60. end
  61. def RDMCell(w, h=0, txt='', border=0, ln=0, align='', fill=0, link='')
  62. cell(w, h, txt, border, ln, align, fill, link)
  63. end
  64. def RDMMultiCell(w, h=0, txt='', border=0, align='', fill=0, ln=1)
  65. multi_cell(w, h, txt, border, align, fill, ln)
  66. end
  67. def RDMwriteFormattedCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0)
  68. @attachments = attachments
  69. css_tag = ' <style>
  70. table, td {
  71. border: 2px #ff0000 solid;
  72. }
  73. th { background-color:#EEEEEE; padding: 4px; white-space:nowrap; text-align: center; font-style: bold;}
  74. pre {
  75. background-color: #fafafa;
  76. }
  77. </style>'
  78. # Strip {{toc}} tags
  79. txt = txt.gsub(/<p>\{\{((<|&lt;)|(>|&gt;))?toc\}\}<\/p>/i, '')
  80. writeHTMLCell(w, h, x, y, css_tag + txt, border, ln, fill)
  81. end
  82. def RDMwriteHTMLCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0)
  83. txt = formatted_text(txt)
  84. RDMwriteFormattedCell(w, h, x, y, txt, attachments, border, ln, fill)
  85. end
  86. def get_image_filename(attrname)
  87. atta = RDMPdfEncoding.attach(@attachments, attrname, "UTF-8")
  88. if atta
  89. return atta.diskfile
  90. else
  91. return nil
  92. end
  93. end
  94. def get_sever_url(url)
  95. if !empty_string(url) and (url[0, 1] == '/')
  96. Setting.host_name.split('/')[0] + url
  97. else
  98. url
  99. end
  100. end
  101. def Footer
  102. set_font(@font_for_footer, 'I', 8)
  103. set_x(15)
  104. if get_rtl
  105. RDMCell(0, 5, @footer_date, 0, 0, 'R')
  106. else
  107. RDMCell(0, 5, @footer_date, 0, 0, 'L')
  108. end
  109. set_x(-30)
  110. RDMCell(0, 5, get_alias_num_page + '/' + get_alias_nb_pages, 0, 0, 'C')
  111. end
  112. end
  113. class RDMPdfEncoding
  114. def self.rdm_from_utf8(txt, encoding)
  115. txt ||= ''
  116. Redmine::CodesetUtil.from_utf8(txt, encoding).b
  117. end
  118. def self.attach(attachments, filename, encoding)
  119. filename_utf8 = Redmine::CodesetUtil.to_utf8(filename, encoding)
  120. atta = nil
  121. if /^[^\/"]+\.(gif|jpg|jpe|jpeg|png)$/i.match?(filename_utf8)
  122. atta = Attachment.latest_attach(attachments, filename_utf8)
  123. end
  124. if atta && atta.readable? && atta.visible?
  125. return atta
  126. else
  127. return nil
  128. end
  129. end
  130. end
  131. end
  132. end
  133. end