improve rmagick feature of rfpdf plugin for PDF inline images (#3261)

Contributed by Jun NAITOH.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@7806 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Toshi MARUYAMA 2011-11-14 07:52:03 +00:00
parent f953426935
commit 6cd33a4815
2 changed files with 89 additions and 27 deletions

View File

@ -57,6 +57,11 @@ module RFPDF
end end
out['bits'] = image.channel_depth out['bits'] = image.channel_depth
File.open( TCPDF.k_path_cache + File::basename(filename), 'w'){|f|
f.binmode
f.print image.to_blob
f.close
}
out out
end end

View File

@ -2049,17 +2049,27 @@ class TCPDF
if (@images[file].nil?) if (@images[file].nil?)
#First use of image, get info #First use of image, get info
if (type == '') if (type == '')
pos = file.rindex('.'); pos = File::basename(file).rindex('.');
if (pos == 0) if (pos.nil? or pos == 0)
Error('Image file has no extension and no type was specified: ' + file); Error('Image file has no extension and no type was specified: ' + file);
end end
pos = file.rindex('.');
type = file[pos+1..-1]; type = file[pos+1..-1];
end end
type.downcase! type.downcase!
if (type == 'jpg' or type == 'jpeg') if (type == 'jpg' or type == 'jpeg')
info=parsejpg(file); info=parsejpg(file);
elsif (type == 'png') elsif (type == 'png' or type == 'gif')
info=parsepng(file); img = Magick::ImageList.new(file)
img.format = "PNG" # convert to PNG from gif
img.opacity = 0 # PNG alpha channel delete
File.open( @@k_path_cache + File::basename(file), 'w'){|f|
f.binmode
f.print img.to_blob
f.close
}
info=parsepng( @@k_path_cache + File::basename(file));
File.delete( @@k_path_cache + File::basename(file))
else else
#Allow for additional formats #Allow for additional formats
mtd='parse' + type; mtd='parse' + type;
@ -2075,15 +2085,37 @@ class TCPDF
end end
#Automatic width and height calculation if needed #Automatic width and height calculation if needed
if ((w == 0) and (h == 0)) if ((w == 0) and (h == 0))
rescale_x = (@w - @r_margin - x) / (info['w'] / (@img_scale * @k))
rescale_x = 1 if rescale_x >= 1
if (y + info['h'] * rescale_x / (@img_scale * @k) > @page_break_trigger and !@in_footer and AcceptPageBreak())
#Automatic page break
if @pages[@page+1].nil?
ws = @ws;
if (ws > 0)
@ws = 0;
out('0 Tw');
end
AddPage(@cur_orientation);
if (ws > 0)
@ws = ws;
out(sprintf('%.3f Tw', ws * @k));
end
else
@page += 1;
end
y=@t_margin;
end
rescale_y = (@page_break_trigger - y) / (info['h'] / (@img_scale * @k))
rescale_y = 1 if rescale_y >= 1
rescale = rescale_y >= rescale_x ? rescale_x : rescale_y
#Put image at 72 dpi #Put image at 72 dpi
# 2004-06-14 :: Nicola Asuni, scale factor where added # 2004-06-14 :: Nicola Asuni, scale factor where added
w = info['w'] / (@img_scale * @k); w = info['w'] * rescale / (@img_scale * @k);
h = info['h'] / (@img_scale * @k); h = info['h'] * rescale / (@img_scale * @k);
end elsif (w == 0)
if (w == 0)
w = h * info['w'] / info['h']; w = h * info['w'] / info['h'];
end elsif (h == 0)
if (h == 0)
h = w * info['h'] / info['w']; h = w * info['h'] / info['w'];
end end
out(sprintf('q %.2f 0 0 %.2f %.2f %.2f cm /I%d Do Q', w*@k, h*@k, x*@k, (@h-(y+h))*@k, info['i'])); out(sprintf('q %.2f 0 0 %.2f %.2f %.2f cm /I%d Do Q', w*@k, h*@k, x*@k, (@h-(y+h))*@k, info['i']));
@ -2846,7 +2878,7 @@ class TCPDF
if (a.empty?) if (a.empty?)
Error('Missing or incorrect image file: ' + file); Error('Missing or incorrect image file: ' + file);
end end
if (a[2]!='JPEG') if (!a[2].nil? and a[2]!='JPEG')
Error('Not a JPEG file: ' + file); Error('Not a JPEG file: ' + file);
end end
if (a['channels'].nil? or a['channels']==3) if (a['channels'].nil? or a['channels']==3)
@ -2859,9 +2891,12 @@ class TCPDF
bpc=!a['bits'].nil? ? a['bits'] : 8; bpc=!a['bits'].nil? ? a['bits'] : 8;
#Read whole file #Read whole file
data=''; data='';
open(file,'rb') do |f|
open( @@k_path_cache + File::basename(file),'rb') do |f|
data<<f.read(); data<<f.read();
end end
File.delete( @@k_path_cache + File::basename(file))
return {'w' => a[0],'h' => a[1],'cs' => colspace,'bpc' => bpc,'f'=>'DCTDecode','data' => data} return {'w' => a[0],'h' => a[1],'cs' => colspace,'bpc' => bpc,'f'=>'DCTDecode','data' => data}
end end
@ -2882,11 +2917,11 @@ class TCPDF
end end
w=freadint(f); w=freadint(f);
h=freadint(f); h=freadint(f);
bpc=f.read(1)[0]; bpc=f.read(1).unpack('C')[0];
if (bpc>8) if (bpc>8)
Error('16-bit depth not supported: ' + file); Error('16-bit depth not supported: ' + file);
end end
ct=f.read(1)[0]; ct=f.read(1).unpack('C')[0];
if (ct==0) if (ct==0)
colspace='DeviceGray'; colspace='DeviceGray';
elsif (ct==2) elsif (ct==2)
@ -2896,13 +2931,13 @@ class TCPDF
else else
Error('Alpha channel not supported: ' + file); Error('Alpha channel not supported: ' + file);
end end
if (f.read(1)[0] != 0) if (f.read(1).unpack('C')[0] != 0)
Error('Unknown compression method: ' + file); Error('Unknown compression method: ' + file);
end end
if (f.read(1)[0]!=0) if (f.read(1).unpack('C')[0] != 0)
Error('Unknown filter method: ' + file); Error('Unknown filter method: ' + file);
end end
if (f.read(1)[0]!=0) if (f.read(1).unpack('C')[0] != 0)
Error('Interlacing not supported: ' + file); Error('Interlacing not supported: ' + file);
end end
f.read(4); f.read(4);
@ -2922,9 +2957,9 @@ class TCPDF
#Read transparency info #Read transparency info
t=f.read( n); t=f.read( n);
if (ct==0) if (ct==0)
trns = t[1][0] trns = t[1].unpack('C')[0]
elsif (ct==2) elsif (ct==2)
trns = t[[1][0], t[3][0], t[5][0]] trns = t[[1].unpack('C')[0], t[3].unpack('C')[0], t[5].unpack('C')[0]]
else else
pos=t.include?(0.chr); pos=t.include?(0.chr);
if (pos!=false) if (pos!=false)
@ -3765,6 +3800,14 @@ class TCPDF
end end
end end
#
# Convert to accessible file path
# @param string :attrname image file name
#
def getImageFilename( attrname )
nil
end
# #
# Process opening tags. # Process opening tags.
# @param string :tag tag name (in upcase) # @param string :tag tag name (in upcase)
@ -3878,10 +3921,17 @@ class TCPDF
when 'img' when 'img'
if (!attrs['src'].nil?) if (!attrs['src'].nil?)
Write(@lasth, '!' + attrs['src'] + '!', '', fill); # Only generates image include a pdf if RMagick is avalaible
=begin Comment out. Because not implement image output yet. unless Object.const_defined?(:Magick)
# replace relative path with real server path Write(@lasth, attrs['src'], '', fill);
attrs['src'] = attrs['src'].gsub(@@k_path_url_cache, @@k_path_cache); return
end
file = getImageFilename(attrs['src'])
if (file.nil?)
Write(@lasth, attrs['src'], '', fill);
return
end
if (attrs['width'].nil?) if (attrs['width'].nil?)
attrs['width'] = 0; attrs['width'] = 0;
end end
@ -3889,10 +3939,17 @@ class TCPDF
attrs['height'] = 0; attrs['height'] = 0;
end end
Image(attrs['src'], GetX(),GetY(), pixelsToMillimeters(attrs['width']), pixelsToMillimeters(attrs['height'])); begin
#SetX(@img_rb_x); Image(file, GetX(),GetY(), pixelsToMillimeters(attrs['width']), pixelsToMillimeters(attrs['height']));
SetY(@img_rb_y); #SetX(@img_rb_x);
=end SetY(@img_rb_y);
rescue => err
logger.error "pdf: Image: error: #{err.message}"
Write(@lasth, attrs['src'], '', fill);
if File.file?( @@k_path_cache + File::basename(file))
File.delete( @@k_path_cache + File::basename(file))
end
end
end end
when 'ul', 'ol' when 'ul', 'ol'