##// END OF EJS Templates
Merged r14963 from trunk to 3.2-stable (#19017)...
Toshi MARUYAMA -
r14582:9820c3a8e398
parent child
Show More
@@ -1,156 +1,156
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # Redmine - project management software
3 # Redmine - project management software
4 # Copyright (C) 2006-2015 Jean-Philippe Lang
4 # Copyright (C) 2006-2015 Jean-Philippe Lang
5 #
5 #
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
9 # of the License, or (at your option) any later version.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
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
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.
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
20 require 'rbpdf'
20 require 'rbpdf'
21
21
22 module Redmine
22 module Redmine
23 module Export
23 module Export
24 module PDF
24 module PDF
25 class ITCPDF < RBPDF
25 class ITCPDF < RBPDF
26 include Redmine::I18n
26 include Redmine::I18n
27 attr_accessor :footer_date
27 attr_accessor :footer_date
28
28
29 def initialize(lang, orientation='P')
29 def initialize(lang, orientation='P')
30 @@k_path_cache = Rails.root.join('tmp', 'pdf')
30 @@k_path_cache = Rails.root.join('tmp', 'pdf')
31 FileUtils.mkdir_p @@k_path_cache unless File::exist?(@@k_path_cache)
31 FileUtils.mkdir_p @@k_path_cache unless File::exist?(@@k_path_cache)
32 set_language_if_valid lang
32 set_language_if_valid lang
33 super(orientation, 'mm', 'A4')
33 super(orientation, 'mm', 'A4')
34 set_print_header(false)
34 set_print_header(false)
35 set_rtl(l(:direction) == 'rtl')
35 set_rtl(l(:direction) == 'rtl')
36
36
37 @font_for_content = l(:general_pdf_fontname)
37 @font_for_content = l(:general_pdf_fontname)
38 @monospaced_font_for_content = l(:general_pdf_monospaced_fontname)
38 @monospaced_font_for_content = l(:general_pdf_monospaced_fontname)
39 @font_for_footer = l(:general_pdf_fontname)
39 @font_for_footer = l(:general_pdf_fontname)
40 set_creator(Redmine::Info.app_name)
40 set_creator(Redmine::Info.app_name)
41 set_font(@font_for_content)
41 set_font(@font_for_content)
42
42
43 set_header_font([@font_for_content, '', 10])
43 set_header_font([@font_for_content, '', 10])
44 set_footer_font([@font_for_content, '', 8])
44 set_footer_font([@font_for_content, '', 8])
45 set_default_monospaced_font(@monospaced_font_for_content)
45 set_default_monospaced_font(@monospaced_font_for_content)
46 set_display_mode('default', 'OneColumn')
46 set_display_mode('default', 'OneColumn')
47 end
47 end
48
48
49 def SetFontStyle(style, size)
49 def SetFontStyle(style, size)
50 set_font(@font_for_content, style, size)
50 set_font(@font_for_content, style, size)
51 end
51 end
52
52
53 def SetFont(family, style='', size=0, fontfile='')
53 def SetFont(family, style='', size=0, fontfile='')
54 # FreeSerif Bold Thai font has problem.
54 # FreeSerif Bold Thai font has problem.
55 style.delete!('B') if family.to_s.casecmp('freeserif') == 0
55 style.delete!('B') if family.to_s.casecmp('freeserif') == 0
56 # DejaVuSans Italic Arabic and Persian font has problem.
56 # DejaVuSans Italic Arabic and Persian font has problem.
57 style.delete!('I') if family.to_s.casecmp('dejavusans') == 0 && current_language.to_s.casecmp("vi") != 0
57 style.delete!('I') if family.to_s.casecmp('dejavusans') == 0 && current_language.to_s.casecmp("vi") != 0
58 # DejaVuSansMono Italic Arabic font has problem
58 # DejaVuSansMono Italic Arabic font has problem
59 style.delete!('I') if family.to_s.casecmp('Dejavusansmono') == 0
59 style.delete!('I') if family.to_s.casecmp('dejavusansmono') == 0
60 super(family, style, size, fontfile)
60 super(family, style, size, fontfile)
61 end
61 end
62 alias_method :set_font, :SetFont
62 alias_method :set_font, :SetFont
63
63
64 def fix_text_encoding(txt)
64 def fix_text_encoding(txt)
65 RDMPdfEncoding::rdm_from_utf8(txt, "UTF-8")
65 RDMPdfEncoding::rdm_from_utf8(txt, "UTF-8")
66 end
66 end
67
67
68 def formatted_text(text)
68 def formatted_text(text)
69 Redmine::WikiFormatting.to_html(Setting.text_formatting, text)
69 Redmine::WikiFormatting.to_html(Setting.text_formatting, text)
70 end
70 end
71
71
72 def RDMCell(w ,h=0, txt='', border=0, ln=0, align='', fill=0, link='')
72 def RDMCell(w ,h=0, txt='', border=0, ln=0, align='', fill=0, link='')
73 cell(w, h, txt, border, ln, align, fill, link)
73 cell(w, h, txt, border, ln, align, fill, link)
74 end
74 end
75
75
76 def RDMMultiCell(w, h=0, txt='', border=0, align='', fill=0, ln=1)
76 def RDMMultiCell(w, h=0, txt='', border=0, align='', fill=0, ln=1)
77 multi_cell(w, h, txt, border, align, fill, ln)
77 multi_cell(w, h, txt, border, align, fill, ln)
78 end
78 end
79
79
80 def RDMwriteFormattedCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0)
80 def RDMwriteFormattedCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0)
81 @attachments = attachments
81 @attachments = attachments
82
82
83 css_tag = ' <style>
83 css_tag = ' <style>
84 table, td {
84 table, td {
85 border: 2px #ff0000 solid;
85 border: 2px #ff0000 solid;
86 }
86 }
87 th { background-color:#EEEEEE; padding: 4px; white-space:nowrap; text-align: center; font-style: bold;}
87 th { background-color:#EEEEEE; padding: 4px; white-space:nowrap; text-align: center; font-style: bold;}
88 pre {
88 pre {
89 background-color: #fafafa;
89 background-color: #fafafa;
90 }
90 }
91 </style>'
91 </style>'
92
92
93 # Strip {{toc}} tags
93 # Strip {{toc}} tags
94 txt.gsub!(/<p>\{\{([<>]?)toc\}\}<\/p>/i, '')
94 txt.gsub!(/<p>\{\{([<>]?)toc\}\}<\/p>/i, '')
95 writeHTMLCell(w, h, x, y, css_tag + txt, border, ln, fill)
95 writeHTMLCell(w, h, x, y, css_tag + txt, border, ln, fill)
96 end
96 end
97
97
98 def RDMwriteHTMLCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0)
98 def RDMwriteHTMLCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0)
99 txt = formatted_text(txt)
99 txt = formatted_text(txt)
100 RDMwriteFormattedCell(w, h, x, y, txt, attachments, border, ln, fill)
100 RDMwriteFormattedCell(w, h, x, y, txt, attachments, border, ln, fill)
101 end
101 end
102
102
103 def get_image_filename(attrname)
103 def get_image_filename(attrname)
104 atta = RDMPdfEncoding.attach(@attachments, attrname, "UTF-8")
104 atta = RDMPdfEncoding.attach(@attachments, attrname, "UTF-8")
105 if atta
105 if atta
106 return atta.diskfile
106 return atta.diskfile
107 else
107 else
108 return nil
108 return nil
109 end
109 end
110 end
110 end
111
111
112 def get_sever_url(url)
112 def get_sever_url(url)
113 if !empty_string(url) and (url[0, 1] == '/')
113 if !empty_string(url) and (url[0, 1] == '/')
114 Setting.host_name.split('/')[0] + url
114 Setting.host_name.split('/')[0] + url
115 else
115 else
116 url
116 url
117 end
117 end
118 end
118 end
119
119
120 def Footer
120 def Footer
121 set_font(@font_for_footer, 'I', 8)
121 set_font(@font_for_footer, 'I', 8)
122 set_x(15)
122 set_x(15)
123 if get_rtl
123 if get_rtl
124 RDMCell(0, 5, @footer_date, 0, 0, 'R')
124 RDMCell(0, 5, @footer_date, 0, 0, 'R')
125 else
125 else
126 RDMCell(0, 5, @footer_date, 0, 0, 'L')
126 RDMCell(0, 5, @footer_date, 0, 0, 'L')
127 end
127 end
128 set_x(-30)
128 set_x(-30)
129 RDMCell(0, 5, get_alias_num_page() + '/' + get_alias_nb_pages(), 0, 0, 'C')
129 RDMCell(0, 5, get_alias_num_page() + '/' + get_alias_nb_pages(), 0, 0, 'C')
130 end
130 end
131 end
131 end
132
132
133 class RDMPdfEncoding
133 class RDMPdfEncoding
134 def self.rdm_from_utf8(txt, encoding)
134 def self.rdm_from_utf8(txt, encoding)
135 txt ||= ''
135 txt ||= ''
136 txt = Redmine::CodesetUtil.from_utf8(txt, encoding)
136 txt = Redmine::CodesetUtil.from_utf8(txt, encoding)
137 txt.force_encoding('ASCII-8BIT')
137 txt.force_encoding('ASCII-8BIT')
138 txt
138 txt
139 end
139 end
140
140
141 def self.attach(attachments, filename, encoding)
141 def self.attach(attachments, filename, encoding)
142 filename_utf8 = Redmine::CodesetUtil.to_utf8(filename, encoding)
142 filename_utf8 = Redmine::CodesetUtil.to_utf8(filename, encoding)
143 atta = nil
143 atta = nil
144 if filename_utf8 =~ /^[^\/"]+\.(gif|jpg|jpe|jpeg|png)$/i
144 if filename_utf8 =~ /^[^\/"]+\.(gif|jpg|jpe|jpeg|png)$/i
145 atta = Attachment.latest_attach(attachments, filename_utf8)
145 atta = Attachment.latest_attach(attachments, filename_utf8)
146 end
146 end
147 if atta && atta.readable? && atta.visible?
147 if atta && atta.readable? && atta.visible?
148 return atta
148 return atta
149 else
149 else
150 return nil
150 return nil
151 end
151 end
152 end
152 end
153 end
153 end
154 end
154 end
155 end
155 end
156 end
156 end
General Comments 0
You need to be logged in to leave comments. Login now