##// END OF EJS Templates
pdf: remove styles by fontname instated of Arabic/Persian/Thai locale (#10681, #18586)...
Toshi MARUYAMA -
r13595:e622ce669177
parent child
Show More
@@ -1,150 +1,152
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 @font_for_footer = l(:general_pdf_fontname)
38 @font_for_footer = l(:general_pdf_fontname)
39 set_creator(Redmine::Info.app_name)
39 set_creator(Redmine::Info.app_name)
40 set_font(@font_for_content)
40 set_font(@font_for_content)
41
41
42 set_header_font([@font_for_content, '', 10])
42 set_header_font([@font_for_content, '', 10])
43 set_footer_font([@font_for_content, '', 8])
43 set_footer_font([@font_for_content, '', 8])
44 set_default_monospaced_font(@font_for_content)
44 set_default_monospaced_font(@font_for_content)
45 end
45 end
46
46
47 def SetFontStyle(style, size)
47 def SetFontStyle(style, size)
48 set_font(@font_for_content, style, size)
48 set_font(@font_for_content, style, size)
49 end
49 end
50
50
51 def SetFont(family, style='', size=0, fontfile='')
51 def SetFont(family, style='', size=0, fontfile='')
52 style.delete!('B') if current_language.to_s.downcase == 'th' # FreeSerif Bold Thai font has problem.
52 # FreeSerif Bold Thai font has problem.
53 style.delete!('I') if current_language.to_s.downcase =~ /^(fa|ar)$/ # DejaVuSans Italic Arabic and Persian font has problem.
53 style.delete!('B') if l(:general_pdf_fontname) == 'freeserif'
54 # DejaVuSans Italic Arabic and Persian font has problem.
55 style.delete!('I') if l(:general_pdf_fontname) == 'DejaVuSans'
54 super(family, style, size, fontfile)
56 super(family, style, size, fontfile)
55 end
57 end
56 alias_method :set_font, :SetFont
58 alias_method :set_font, :SetFont
57
59
58 def fix_text_encoding(txt)
60 def fix_text_encoding(txt)
59 RDMPdfEncoding::rdm_from_utf8(txt, "UTF-8")
61 RDMPdfEncoding::rdm_from_utf8(txt, "UTF-8")
60 end
62 end
61
63
62 def formatted_text(text)
64 def formatted_text(text)
63 Redmine::WikiFormatting.to_html(Setting.text_formatting, text)
65 Redmine::WikiFormatting.to_html(Setting.text_formatting, text)
64 end
66 end
65
67
66 def RDMCell(w ,h=0, txt='', border=0, ln=0, align='', fill=0, link='')
68 def RDMCell(w ,h=0, txt='', border=0, ln=0, align='', fill=0, link='')
67 cell(w, h, txt, border, ln, align, fill, link)
69 cell(w, h, txt, border, ln, align, fill, link)
68 end
70 end
69
71
70 def RDMMultiCell(w, h=0, txt='', border=0, align='', fill=0, ln=1)
72 def RDMMultiCell(w, h=0, txt='', border=0, align='', fill=0, ln=1)
71 multi_cell(w, h, txt, border, align, fill, ln)
73 multi_cell(w, h, txt, border, align, fill, ln)
72 end
74 end
73
75
74 def RDMwriteFormattedCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0)
76 def RDMwriteFormattedCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0)
75 @attachments = attachments
77 @attachments = attachments
76
78
77 css_tag = ' <style>
79 css_tag = ' <style>
78 table, td {
80 table, td {
79 border: 2px #ff0000 solid;
81 border: 2px #ff0000 solid;
80 }
82 }
81 th { background-color:#EEEEEE; padding: 4px; white-space:nowrap; text-align: center; font-style: bold;}
83 th { background-color:#EEEEEE; padding: 4px; white-space:nowrap; text-align: center; font-style: bold;}
82 pre {
84 pre {
83 background-color: #fafafa;
85 background-color: #fafafa;
84 }
86 }
85 </style>'
87 </style>'
86
88
87 # Strip {{toc}} tags
89 # Strip {{toc}} tags
88 txt.gsub!(/<p>\{\{([<>]?)toc\}\}<\/p>/i, '')
90 txt.gsub!(/<p>\{\{([<>]?)toc\}\}<\/p>/i, '')
89 writeHTMLCell(w, h, x, y, css_tag + txt, border, ln, fill)
91 writeHTMLCell(w, h, x, y, css_tag + txt, border, ln, fill)
90 end
92 end
91
93
92 def RDMwriteHTMLCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0)
94 def RDMwriteHTMLCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0)
93 txt = formatted_text(txt)
95 txt = formatted_text(txt)
94 RDMwriteFormattedCell(w, h, x, y, txt, attachments, border, ln, fill)
96 RDMwriteFormattedCell(w, h, x, y, txt, attachments, border, ln, fill)
95 end
97 end
96
98
97 def get_image_filename(attrname)
99 def get_image_filename(attrname)
98 atta = RDMPdfEncoding.attach(@attachments, attrname, "UTF-8")
100 atta = RDMPdfEncoding.attach(@attachments, attrname, "UTF-8")
99 if atta
101 if atta
100 return atta.diskfile
102 return atta.diskfile
101 else
103 else
102 return nil
104 return nil
103 end
105 end
104 end
106 end
105
107
106 def get_sever_url(url)
108 def get_sever_url(url)
107 if !empty_string(url) and (url[0, 1] == '/')
109 if !empty_string(url) and (url[0, 1] == '/')
108 Setting.host_name.split('/')[0] + url
110 Setting.host_name.split('/')[0] + url
109 else
111 else
110 url
112 url
111 end
113 end
112 end
114 end
113
115
114 def Footer
116 def Footer
115 set_font(@font_for_footer, 'I', 8)
117 set_font(@font_for_footer, 'I', 8)
116 set_x(15)
118 set_x(15)
117 if get_rtl
119 if get_rtl
118 RDMCell(0, 5, @footer_date, 0, 0, 'R')
120 RDMCell(0, 5, @footer_date, 0, 0, 'R')
119 else
121 else
120 RDMCell(0, 5, @footer_date, 0, 0, 'L')
122 RDMCell(0, 5, @footer_date, 0, 0, 'L')
121 end
123 end
122 set_x(-30)
124 set_x(-30)
123 RDMCell(0, 5, get_alias_num_page() + '/' + get_alias_nb_pages(), 0, 0, 'C')
125 RDMCell(0, 5, get_alias_num_page() + '/' + get_alias_nb_pages(), 0, 0, 'C')
124 end
126 end
125 end
127 end
126
128
127 class RDMPdfEncoding
129 class RDMPdfEncoding
128 def self.rdm_from_utf8(txt, encoding)
130 def self.rdm_from_utf8(txt, encoding)
129 txt ||= ''
131 txt ||= ''
130 txt = Redmine::CodesetUtil.from_utf8(txt, encoding)
132 txt = Redmine::CodesetUtil.from_utf8(txt, encoding)
131 txt.force_encoding('ASCII-8BIT')
133 txt.force_encoding('ASCII-8BIT')
132 txt
134 txt
133 end
135 end
134
136
135 def self.attach(attachments, filename, encoding)
137 def self.attach(attachments, filename, encoding)
136 filename_utf8 = Redmine::CodesetUtil.to_utf8(filename, encoding)
138 filename_utf8 = Redmine::CodesetUtil.to_utf8(filename, encoding)
137 atta = nil
139 atta = nil
138 if filename_utf8 =~ /^[^\/"]+\.(gif|jpg|jpe|jpeg|png)$/i
140 if filename_utf8 =~ /^[^\/"]+\.(gif|jpg|jpe|jpeg|png)$/i
139 atta = Attachment.latest_attach(attachments, filename_utf8)
141 atta = Attachment.latest_attach(attachments, filename_utf8)
140 end
142 end
141 if atta && atta.readable? && atta.visible?
143 if atta && atta.readable? && atta.visible?
142 return atta
144 return atta
143 else
145 else
144 return nil
146 return nil
145 end
147 end
146 end
148 end
147 end
149 end
148 end
150 end
149 end
151 end
150 end
152 end
General Comments 0
You need to be logged in to leave comments. Login now