# HG changeset patch
# User Toshi MARUYAMA
# Date 1321273488 -32400
# Branch pdf-image-20111115-3
# Node ID 95e8c4251b7267854c41f8fb308123c8bb730d32
# Parent  89fd70919b97193f9f94f79e3ccb85dbfb129441
pdf: lib: add "attachments" parameter at RDMwriteHTMLCell method (#3261)

diff --git a/lib/redmine/export/pdf.rb b/lib/redmine/export/pdf.rb
--- a/lib/redmine/export/pdf.rb
+++ b/lib/redmine/export/pdf.rb
@@ -115,8 +115,11 @@ module Redmine
           MultiCell(w, h, fix_text_encoding(txt), border, align, fill, ln)
         end
 
-        def RDMwriteHTMLCell(w, h, x, y, html='', border=0, ln=1, fill=0)
-          writeHTMLCell(w, h, x, y, fix_text_encoding(html), border, ln, fill)
+        def RDMwriteHTMLCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0)
+          writeHTMLCell(w, h, x, y,
+            fix_text_encoding(
+              Redmine::WikiFormatting.to_html(Setting.text_formatting, txt)),
+              border, ln, fill)
         end
 
         def Footer
@@ -358,8 +361,7 @@ module Redmine
         pdf.RDMCell(35+155, 5, l(:field_description), "LRT", 1)
         pdf.SetFontStyle('',9)
         pdf.RDMwriteHTMLCell(35+155, 5, 0, 0,
-            Redmine::WikiFormatting.to_html(
-              Setting.text_formatting, issue.description.to_s),"LRB")
+              issue.description.to_s, issue.attachments, "LRB")
 
         # for CJK
         truncate_length = ( l(:general_pdf_encoding).upcase == "UTF-8" ? 90 : 65 )
@@ -421,8 +423,7 @@ module Redmine
             unless changeset.comments.blank?
               pdf.SetFontStyle('',8)
               pdf.RDMwriteHTMLCell(190,5,0,0,
-                   Redmine::WikiFormatting.to_html(
-                     Setting.text_formatting, changeset.comments.to_s), "")
+                    changeset.comments.to_s, issue.attachments, "")
             end
             pdf.Ln
           end
@@ -446,8 +447,7 @@ module Redmine
             pdf.Ln unless journal.details.empty?
             pdf.SetFontStyle('',8)
             pdf.RDMwriteHTMLCell(190,5,0,0,
-                  Redmine::WikiFormatting.to_html(
-                    Setting.text_formatting, journal.notes.to_s), "")
+                  journal.notes.to_s, issue.attachments, "")
           end
           pdf.Ln
         end
@@ -483,8 +483,7 @@ module Redmine
         pdf.SetImageScale(1.6)
         pdf.SetFontStyle('',9)
         pdf.RDMwriteHTMLCell(190,5,0,0,
-              Redmine::WikiFormatting.to_html(
-                Setting.text_formatting, page.content.text.to_s), "TLRB")
+              page.content.text.to_s, page.attachments, "TLRB")
         if page.attachments.any?
           pdf.Ln
           pdf.SetFontStyle('B',9)
# HG changeset patch
# User Toshi MARUYAMA
# Date 1321273488 -32400
# Branch pdf-image-20111115-3
# Node ID 2b1b2e8a5a29e25bc1eef98d9fd3e21afaf12ea5
# Parent  95e8c4251b7267854c41f8fb308123c8bb730d32
[mq]: 1.diff

diff --git a/lib/redmine/export/pdf.rb b/lib/redmine/export/pdf.rb
--- a/lib/redmine/export/pdf.rb
+++ b/lib/redmine/export/pdf.rb
@@ -21,6 +21,7 @@ require 'iconv'
 require 'fpdf/chinese'
 require 'fpdf/japanese'
 require 'fpdf/korean'
+require 'core/rmagick'
 
 module Redmine
   module Export
@@ -34,6 +35,8 @@ module Redmine
         attr_accessor :footer_date
 
         def initialize(lang)
+          @@k_path_cache = Rails.root.join('tmp', 'pdf')
+          FileUtils.mkdir_p @@k_path_cache unless File::exist?(@@k_path_cache)
           set_language_if_valid lang
           pdf_encoding = l(:general_pdf_encoding).upcase
           if RUBY_VERSION < '1.9'
# HG changeset patch
# User Toshi MARUYAMA
# Date 1321273488 -32400
# Branch pdf-image-20111115-3
# Node ID a748fb90563f2b187c603959829a99189abc6f4e
# Parent  2b1b2e8a5a29e25bc1eef98d9fd3e21afaf12ea5
[mq]: 2.diff

diff --git a/lib/redmine/export/pdf.rb b/lib/redmine/export/pdf.rb
--- a/lib/redmine/export/pdf.rb
+++ b/lib/redmine/export/pdf.rb
@@ -119,12 +119,36 @@ module Redmine
         end
 
         def RDMwriteHTMLCell(w, h, x, y, txt='', attachments=[], border=0, ln=1, fill=0)
+          @attachments = attachments.sort_by(&:created_on).reverse
           writeHTMLCell(w, h, x, y,
             fix_text_encoding(
               Redmine::WikiFormatting.to_html(Setting.text_formatting, txt)),
               border, ln, fill)
         end
 
+        def getImageFilename(attrname)
+          # attrname: general_pdf_encoding string file/uri name
+          attrname_utf8 = Redmine::CodesetUtil.to_utf8(attrname, l(:general_pdf_encoding))
+          if attrname_utf8 =~ /^[^\/"]+\.(gif|jpg|jpeg|png)$/i
+            @attachments.each do |attachment|
+              # attachment.filename  : UTF-8 string name
+              # attachment.diskfile  : real server path file name
+                #
+              # Japanese Shift_JIS and Traditional Chinese Big5
+              # are not compatible with ASCII.
+              # So, downcase needs for UTF-8.
+              if attachment.filename.downcase == attrname_utf8.downcase
+                if attachment.readable? && attachment.visible?
+                  return attachment.diskfile
+                else
+                  return nil
+                end
+              end
+            end
+          end
+          nil
+        end
+
         def Footer
           SetFont(@font_for_footer, 'I', 8)
           SetY(-15)
# HG changeset patch
# User Toshi MARUYAMA
# Date 1321273514 -32400
# Branch pdf-image-20111115-3
# Node ID 21dd3591922f683920fce2bae0d3a3d9c8d1e92c
# Parent  a748fb90563f2b187c603959829a99189abc6f4e
[mq]: 3.diff

diff --git a/lib/redmine/export/pdf.rb b/lib/redmine/export/pdf.rb
--- a/lib/redmine/export/pdf.rb
+++ b/lib/redmine/export/pdf.rb
@@ -387,6 +387,9 @@ module Redmine
         pdf.SetFontStyle('B',9)
         pdf.RDMCell(35+155, 5, l(:field_description), "LRT", 1)
         pdf.SetFontStyle('',9)
+
+        # Set resize image scale
+        pdf.SetImageScale(1.6)
         pdf.RDMwriteHTMLCell(35+155, 5, 0, 0,
               issue.description.to_s, issue.attachments, "LRB")
 
# HG changeset patch
# User Toshi MARUYAMA
# Date 1321273488 -32400
# Branch pdf-image-20111115-3
# Node ID 3a0334d2c93284551eed469c80c263b0a084558e
# Parent  21dd3591922f683920fce2bae0d3a3d9c8d1e92c
[mq]: 2.diff

diff --git a/lib/redmine/export/pdf.rb b/lib/redmine/export/pdf.rb
--- a/lib/redmine/export/pdf.rb
+++ b/lib/redmine/export/pdf.rb
@@ -145,8 +145,15 @@ module Redmine
                 end
               end
             end
+            return nil
+          else
+            # check of unsafe URI
+            if attrname_utf8 =~ /[^-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]%]/n
+              return URI.encode(attrname_utf8)
+            else
+              return attrname_utf8
+            end
           end
-          nil
         end
 
         def Footer
