pdf.rb
154 lines
| 5.1 KiB
| text/x-ruby
|
RubyLexer
|
r2796 | # encoding: utf-8 | ||
# | ||||
|
r2224 | # Redmine - project management software | ||
|
r13490 | # Copyright (C) 2006-2015 Jean-Philippe Lang | ||
|
r2224 | # | ||
# 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. | ||||
|
r5543 | # | ||
|
r2224 | # 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. | ||||
|
r5543 | # | ||
|
r2224 | # 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. | ||||
|
r13004 | require 'rbpdf' | ||
|
r10947 | |||
|
r2224 | module Redmine | ||
module Export | ||||
module PDF | ||||
|
r13004 | class ITCPDF < RBPDF | ||
|
r5138 | include Redmine::I18n | ||
attr_accessor :footer_date | ||||
|
r5543 | |||
|
r10721 | def initialize(lang, orientation='P') | ||
|
r9790 | @@k_path_cache = Rails.root.join('tmp', 'pdf') | ||
|
r7794 | FileUtils.mkdir_p @@k_path_cache unless File::exist?(@@k_path_cache) | ||
|
r5603 | set_language_if_valid lang | ||
|
r13004 | super(orientation, 'mm', 'A4') | ||
set_print_header(false) | ||||
set_rtl(l(:direction) == 'rtl') | ||||
|
r13077 | @font_for_content = l(:general_pdf_fontname) | ||
|
r14528 | @monospaced_font_for_content = l(:general_pdf_monospaced_fontname) | ||
|
r13077 | @font_for_footer = l(:general_pdf_fontname) | ||
|
r13004 | set_creator(Redmine::Info.app_name) | ||
set_font(@font_for_content) | ||||
|
r5543 | |||
|
r13004 | set_header_font([@font_for_content, '', 10]) | ||
set_footer_font([@font_for_content, '', 8]) | ||||
|
r14528 | set_default_monospaced_font(@monospaced_font_for_content) | ||
|
r13917 | set_display_mode('default', 'OneColumn') | ||
|
r5138 | end | ||
|
r5543 | |||
|
r13004 | def SetFontStyle(style, size) | ||
set_font(@font_for_content, style, size) | ||||
|
r5138 | end | ||
|
r5543 | |||
|
r13594 | def SetFont(family, style='', size=0, fontfile='') | ||
|
r13595 | # FreeSerif Bold Thai font has problem. | ||
|
r14529 | style.delete!('B') if l(:general_pdf_fontname) == 'freeserif' | ||
|
r13595 | # DejaVuSans Italic Arabic and Persian font has problem. | ||
|
r14529 | style.delete!('I') if l(:general_pdf_fontname) == 'DejaVuSans' && current_language.to_s.casecmp("vi") != 0 | ||
|
r13594 | super(family, style, size, fontfile) | ||
end | ||||
alias_method :set_font, :SetFont | ||||
|
r5596 | def fix_text_encoding(txt) | ||
|
r13005 | RDMPdfEncoding::rdm_from_utf8(txt, "UTF-8") | ||
|
r5596 | end | ||
|
r10997 | def formatted_text(text) | ||
|
r13562 | Redmine::WikiFormatting.to_html(Setting.text_formatting, text) | ||
|
r10997 | end | ||
|
r6018 | def RDMCell(w ,h=0, txt='', border=0, ln=0, align='', fill=0, link='') | ||
|
r13004 | cell(w, h, txt, border, ln, align, fill, link) | ||
|
r5596 | end | ||
|
r6015 | def RDMMultiCell(w, h=0, txt='', border=0, align='', fill=0, ln=1) | ||
|
r13004 | multi_cell(w, h, txt, border, align, fill, ln) | ||
|
r5596 | end | ||
|
r5543 | |||
|
r13562 | def RDMwriteFormattedCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0) | ||
|
r7795 | @attachments = attachments | ||
|
r13004 | |||
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>' | ||||
|
r13562 | # 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) | ||||
|
r6017 | end | ||
|
r13004 | def get_image_filename(attrname) | ||
atta = RDMPdfEncoding.attach(@attachments, attrname, "UTF-8") | ||||
|
r7795 | if atta | ||
return atta.diskfile | ||||
else | ||||
return nil | ||||
end | ||||
end | ||||
|
r13004 | def get_sever_url(url) | ||
if !empty_string(url) and (url[0, 1] == '/') | ||||
Setting.host_name.split('/')[0] + url | ||||
else | ||||
url | ||||
|
r8612 | end | ||
end | ||||
|
r13004 | 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') | ||||
|
r8612 | end | ||
|
r13004 | set_x(-30) | ||
RDMCell(0, 5, get_alias_num_page() + '/' + get_alias_nb_pages(), 0, 0, 'C') | ||||
|
r8612 | end | ||
|
r5138 | end | ||
|
r5593 | class RDMPdfEncoding | ||
|
r7698 | def self.rdm_from_utf8(txt, encoding) | ||
|
r5593 | txt ||= '' | ||
|
r7698 | txt = Redmine::CodesetUtil.from_utf8(txt, encoding) | ||
|
r13100 | txt.force_encoding('ASCII-8BIT') | ||
|
r5593 | txt | ||
end | ||||
|
r7792 | |||
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 | ||||
|
r5593 | end | ||
|
r2224 | end | ||
end | ||||
end | ||||