"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/lib/redmine/export/pdf.rb" (6 Apr 2020, 5348 Bytes) of package /linux/www/redmine-4.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "pdf.rb": 4.1.0_vs_4.1.1.

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