| 
									
										
										
										
											2009-10-10 15:00:28 +00:00
										 |  |  | # encoding: utf-8 | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2009-01-04 12:03:39 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2017-06-25 08:40:31 +00:00
										 |  |  | # Copyright (C) 2006-2017  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2009-01-04 12:03:39 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2011-05-06 10:40:12 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2009-01-04 12:03:39 +00:00
										 |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2011-05-06 10:40:12 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2009-01-04 12:03:39 +00:00
										 |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-28 10:23:46 +00:00
										 |  |  | require 'rbpdf' | 
					
						
							| 
									
										
										
										
											2013-01-12 21:27:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-04 12:03:39 +00:00
										 |  |  | module Redmine | 
					
						
							|  |  |  |   module Export | 
					
						
							|  |  |  |     module PDF | 
					
						
							| 
									
										
										
										
											2014-08-28 10:23:46 +00:00
										 |  |  |       class ITCPDF < RBPDF | 
					
						
							| 
									
										
										
										
											2011-03-30 07:32:08 +00:00
										 |  |  |         include Redmine::I18n | 
					
						
							|  |  |  |         attr_accessor :footer_date | 
					
						
							| 
									
										
										
										
											2011-05-06 10:40:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-06 17:48:19 +00:00
										 |  |  |         def initialize(lang, orientation='P') | 
					
						
							| 
									
										
										
										
											2012-07-11 00:14:56 +00:00
										 |  |  |           @@k_path_cache = Rails.root.join('tmp', 'pdf') | 
					
						
							| 
									
										
										
										
											2011-11-24 11:11:36 +00:00
										 |  |  |           FileUtils.mkdir_p @@k_path_cache unless File::exist?(@@k_path_cache) | 
					
						
							| 
									
										
										
										
											2011-05-10 01:03:28 +00:00
										 |  |  |           set_language_if_valid lang | 
					
						
							| 
									
										
										
										
											2014-08-28 10:23:46 +00:00
										 |  |  |           super(orientation, 'mm', 'A4') | 
					
						
							|  |  |  |           set_print_header(false) | 
					
						
							|  |  |  |           set_rtl(l(:direction) == 'rtl') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-20 04:15:11 +00:00
										 |  |  |           @font_for_content = l(:general_pdf_fontname) | 
					
						
							| 
									
										
										
										
											2015-12-04 05:43:21 +00:00
										 |  |  |           @monospaced_font_for_content = l(:general_pdf_monospaced_fontname) | 
					
						
							| 
									
										
										
										
											2014-10-20 04:15:11 +00:00
										 |  |  |           @font_for_footer  = l(:general_pdf_fontname) | 
					
						
							| 
									
										
										
										
											2014-08-28 10:23:46 +00:00
										 |  |  |           set_creator(Redmine::Info.app_name) | 
					
						
							|  |  |  |           set_font(@font_for_content) | 
					
						
							| 
									
										
										
										
											2011-05-06 10:40:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-28 10:23:46 +00:00
										 |  |  |           set_header_font([@font_for_content, '', 10]) | 
					
						
							|  |  |  |           set_footer_font([@font_for_content, '', 8]) | 
					
						
							| 
									
										
										
										
											2015-12-04 05:43:21 +00:00
										 |  |  |           set_default_monospaced_font(@monospaced_font_for_content) | 
					
						
							| 
									
										
										
										
											2015-06-06 07:24:13 +00:00
										 |  |  |           set_display_mode('default', 'OneColumn') | 
					
						
							| 
									
										
										
										
											2011-03-30 07:32:08 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2011-05-06 10:40:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-28 10:23:46 +00:00
										 |  |  |         def SetFontStyle(style, size) | 
					
						
							|  |  |  |           set_font(@font_for_content, style, size) | 
					
						
							| 
									
										
										
										
											2011-03-30 07:32:08 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2011-05-06 10:40:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-08 04:04:24 +00:00
										 |  |  |         def SetFont(family, style='', size=0, fontfile='') | 
					
						
							| 
									
										
										
										
											2015-02-08 04:04:35 +00:00
										 |  |  |           # FreeSerif Bold Thai font has problem. | 
					
						
							| 
									
										
										
										
											2015-12-06 12:21:25 +00:00
										 |  |  |           style.delete!('B') if family.to_s.casecmp('freeserif') == 0
 | 
					
						
							| 
									
										
										
										
											2015-02-08 04:04:35 +00:00
										 |  |  |           # DejaVuSans Italic Arabic and Persian font has problem. | 
					
						
							| 
									
										
										
										
											2015-12-06 12:21:25 +00:00
										 |  |  |           style.delete!('I') if family.to_s.casecmp('dejavusans') == 0 && current_language.to_s.casecmp("vi") != 0
 | 
					
						
							| 
									
										
										
										
											2015-12-06 12:21:36 +00:00
										 |  |  |           # DejaVuSansMono Italic Arabic font has problem | 
					
						
							| 
									
										
										
										
											2015-12-06 12:36:19 +00:00
										 |  |  |           style.delete!('I') if family.to_s.casecmp('dejavusansmono') == 0
 | 
					
						
							| 
									
										
										
										
											2015-02-08 04:04:24 +00:00
										 |  |  |           super(family, style, size, fontfile) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         alias_method :set_font, :SetFont | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 06:46:21 +00:00
										 |  |  |         def fix_text_encoding(txt) | 
					
						
							| 
									
										
										
										
											2014-08-28 10:23:54 +00:00
										 |  |  |           RDMPdfEncoding::rdm_from_utf8(txt, "UTF-8") | 
					
						
							| 
									
										
										
										
											2011-05-09 06:46:21 +00:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-20 16:22:20 +00:00
										 |  |  |         def formatted_text(text) | 
					
						
							| 
									
										
										
										
											2015-01-25 09:04:52 +00:00
										 |  |  |           Redmine::WikiFormatting.to_html(Setting.text_formatting, text) | 
					
						
							| 
									
										
										
										
											2013-01-20 16:22:20 +00:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-01 07:49:37 +00:00
										 |  |  |         def RDMCell(w ,h=0, txt='', border=0, ln=0, align='', fill=0, link='') | 
					
						
							| 
									
										
										
										
											2014-08-28 10:23:46 +00:00
										 |  |  |           cell(w, h, txt, border, ln, align, fill, link) | 
					
						
							| 
									
										
										
										
											2011-05-09 06:46:21 +00:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-30 11:47:55 +00:00
										 |  |  |         def RDMMultiCell(w, h=0, txt='', border=0, align='', fill=0, ln=1) | 
					
						
							| 
									
										
										
										
											2014-08-28 10:23:46 +00:00
										 |  |  |           multi_cell(w, h, txt, border, align, fill, ln) | 
					
						
							| 
									
										
										
										
											2011-05-09 06:46:21 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2011-05-06 10:40:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-25 09:04:52 +00:00
										 |  |  |         def RDMwriteFormattedCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0) | 
					
						
							| 
									
										
										
										
											2011-11-24 11:12:00 +00:00
										 |  |  |           @attachments = attachments | 
					
						
							| 
									
										
										
										
											2014-08-28 10:23:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |           css_tag = ' <style> | 
					
						
							|  |  |  |           table, td { | 
					
						
							|  |  |  |             border: 2px #ff0000 solid; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           th {  background-color:#EEEEEE; padding: 4px; white-space:nowrap; text-align: center;  font-style: bold;} | 
					
						
							|  |  |  |           pre { | 
					
						
							|  |  |  |             background-color: #fafafa; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           </style>'
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-25 09:04:52 +00:00
										 |  |  |           # Strip {{toc}} tags | 
					
						
							|  |  |  |           txt.gsub!(/<p>\{\{([<>]?)toc\}\}<\/p>/i, '') | 
					
						
							|  |  |  |           writeHTMLCell(w, h, x, y, css_tag + txt, border, ln, fill) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def RDMwriteHTMLCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0) | 
					
						
							|  |  |  |           txt = formatted_text(txt) | 
					
						
							|  |  |  |           RDMwriteFormattedCell(w, h, x, y, txt, attachments, border, ln, fill) | 
					
						
							| 
									
										
										
										
											2011-07-01 07:49:07 +00:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-28 10:23:46 +00:00
										 |  |  |         def get_image_filename(attrname) | 
					
						
							|  |  |  |           atta = RDMPdfEncoding.attach(@attachments, attrname, "UTF-8") | 
					
						
							| 
									
										
										
										
											2011-11-24 11:12:00 +00:00
										 |  |  |           if atta | 
					
						
							|  |  |  |             return atta.diskfile | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             return nil | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-28 10:23:46 +00:00
										 |  |  |         def get_sever_url(url) | 
					
						
							|  |  |  |           if !empty_string(url) and (url[0, 1] == '/') | 
					
						
							|  |  |  |             Setting.host_name.split('/')[0] + url | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             url | 
					
						
							| 
									
										
										
										
											2012-01-30 18:01:47 +00:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-28 10:23:46 +00:00
										 |  |  |         def Footer | 
					
						
							|  |  |  |           set_font(@font_for_footer, 'I', 8) | 
					
						
							|  |  |  |           set_x(15) | 
					
						
							|  |  |  |           if get_rtl | 
					
						
							|  |  |  |             RDMCell(0, 5, @footer_date, 0, 0, 'R') | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             RDMCell(0, 5, @footer_date, 0, 0, 'L') | 
					
						
							| 
									
										
										
										
											2012-01-30 18:01:47 +00:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2014-08-28 10:23:46 +00:00
										 |  |  |           set_x(-30) | 
					
						
							|  |  |  |           RDMCell(0, 5, get_alias_num_page() + '/' + get_alias_nb_pages(), 0, 0, 'C') | 
					
						
							| 
									
										
										
										
											2012-01-30 18:01:47 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2011-03-30 07:32:08 +00:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-09 06:44:23 +00:00
										 |  |  |       class RDMPdfEncoding | 
					
						
							| 
									
										
										
										
											2011-11-16 05:07:16 +00:00
										 |  |  |         def self.rdm_from_utf8(txt, encoding) | 
					
						
							| 
									
										
										
										
											2011-05-09 06:44:23 +00:00
										 |  |  |           txt ||= '' | 
					
						
							| 
									
										
										
										
											2011-11-16 05:07:16 +00:00
										 |  |  |           txt = Redmine::CodesetUtil.from_utf8(txt, encoding) | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |           txt.force_encoding('ASCII-8BIT') | 
					
						
							| 
									
										
										
										
											2011-05-09 06:44:23 +00:00
										 |  |  |           txt | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2011-11-24 09:43:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def self.attach(attachments, filename, encoding) | 
					
						
							|  |  |  |           filename_utf8 = Redmine::CodesetUtil.to_utf8(filename, encoding) | 
					
						
							|  |  |  |           atta = nil | 
					
						
							|  |  |  |           if filename_utf8 =~ /^[^\/"]+\.(gif|jpg|jpe|jpeg|png)$/i | 
					
						
							|  |  |  |             atta = Attachment.latest_attach(attachments, filename_utf8) | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           if atta && atta.readable? && atta.visible? | 
					
						
							|  |  |  |             return atta | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             return nil | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2011-05-09 06:44:23 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2009-01-04 12:03:39 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |