##// END OF EJS Templates
improve rmagick feature of rfpdf plugin for PDF inline images (#3261)...
Toshi MARUYAMA -
r7686:6cd33a48159e
parent child
Show More
@@ -57,6 +57,11 module RFPDF
57 end
57 end
58
58
59 out['bits'] = image.channel_depth
59 out['bits'] = image.channel_depth
60 File.open( TCPDF.k_path_cache + File::basename(filename), 'w'){|f|
61 f.binmode
62 f.print image.to_blob
63 f.close
64 }
60
65
61 out
66 out
62 end
67 end
@@ -2049,17 +2049,27 class TCPDF
2049 if (@images[file].nil?)
2049 if (@images[file].nil?)
2050 #First use of image, get info
2050 #First use of image, get info
2051 if (type == '')
2051 if (type == '')
2052 pos = file.rindex('.');
2052 pos = File::basename(file).rindex('.');
2053 if (pos == 0)
2053 if (pos.nil? or pos == 0)
2054 Error('Image file has no extension and no type was specified: ' + file);
2054 Error('Image file has no extension and no type was specified: ' + file);
2055 end
2055 end
2056 pos = file.rindex('.');
2056 type = file[pos+1..-1];
2057 type = file[pos+1..-1];
2057 end
2058 end
2058 type.downcase!
2059 type.downcase!
2059 if (type == 'jpg' or type == 'jpeg')
2060 if (type == 'jpg' or type == 'jpeg')
2060 info=parsejpg(file);
2061 info=parsejpg(file);
2061 elsif (type == 'png')
2062 elsif (type == 'png' or type == 'gif')
2062 info=parsepng(file);
2063 img = Magick::ImageList.new(file)
2064 img.format = "PNG" # convert to PNG from gif
2065 img.opacity = 0 # PNG alpha channel delete
2066 File.open( @@k_path_cache + File::basename(file), 'w'){|f|
2067 f.binmode
2068 f.print img.to_blob
2069 f.close
2070 }
2071 info=parsepng( @@k_path_cache + File::basename(file));
2072 File.delete( @@k_path_cache + File::basename(file))
2063 else
2073 else
2064 #Allow for additional formats
2074 #Allow for additional formats
2065 mtd='parse' + type;
2075 mtd='parse' + type;
@@ -2075,15 +2085,37 class TCPDF
2075 end
2085 end
2076 #Automatic width and height calculation if needed
2086 #Automatic width and height calculation if needed
2077 if ((w == 0) and (h == 0))
2087 if ((w == 0) and (h == 0))
2088 rescale_x = (@w - @r_margin - x) / (info['w'] / (@img_scale * @k))
2089 rescale_x = 1 if rescale_x >= 1
2090 if (y + info['h'] * rescale_x / (@img_scale * @k) > @page_break_trigger and !@in_footer and AcceptPageBreak())
2091 #Automatic page break
2092 if @pages[@page+1].nil?
2093 ws = @ws;
2094 if (ws > 0)
2095 @ws = 0;
2096 out('0 Tw');
2097 end
2098 AddPage(@cur_orientation);
2099 if (ws > 0)
2100 @ws = ws;
2101 out(sprintf('%.3f Tw', ws * @k));
2102 end
2103 else
2104 @page += 1;
2105 end
2106 y=@t_margin;
2107 end
2108 rescale_y = (@page_break_trigger - y) / (info['h'] / (@img_scale * @k))
2109 rescale_y = 1 if rescale_y >= 1
2110 rescale = rescale_y >= rescale_x ? rescale_x : rescale_y
2111
2078 #Put image at 72 dpi
2112 #Put image at 72 dpi
2079 # 2004-06-14 :: Nicola Asuni, scale factor where added
2113 # 2004-06-14 :: Nicola Asuni, scale factor where added
2080 w = info['w'] / (@img_scale * @k);
2114 w = info['w'] * rescale / (@img_scale * @k);
2081 h = info['h'] / (@img_scale * @k);
2115 h = info['h'] * rescale / (@img_scale * @k);
2082 end
2116 elsif (w == 0)
2083 if (w == 0)
2084 w = h * info['w'] / info['h'];
2117 w = h * info['w'] / info['h'];
2085 end
2118 elsif (h == 0)
2086 if (h == 0)
2087 h = w * info['h'] / info['w'];
2119 h = w * info['h'] / info['w'];
2088 end
2120 end
2089 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']));
2121 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
2846 if (a.empty?)
2878 if (a.empty?)
2847 Error('Missing or incorrect image file: ' + file);
2879 Error('Missing or incorrect image file: ' + file);
2848 end
2880 end
2849 if (a[2]!='JPEG')
2881 if (!a[2].nil? and a[2]!='JPEG')
2850 Error('Not a JPEG file: ' + file);
2882 Error('Not a JPEG file: ' + file);
2851 end
2883 end
2852 if (a['channels'].nil? or a['channels']==3)
2884 if (a['channels'].nil? or a['channels']==3)
@@ -2859,9 +2891,12 class TCPDF
2859 bpc=!a['bits'].nil? ? a['bits'] : 8;
2891 bpc=!a['bits'].nil? ? a['bits'] : 8;
2860 #Read whole file
2892 #Read whole file
2861 data='';
2893 data='';
2862 open(file,'rb') do |f|
2894
2895 open( @@k_path_cache + File::basename(file),'rb') do |f|
2863 data<<f.read();
2896 data<<f.read();
2864 end
2897 end
2898 File.delete( @@k_path_cache + File::basename(file))
2899
2865 return {'w' => a[0],'h' => a[1],'cs' => colspace,'bpc' => bpc,'f'=>'DCTDecode','data' => data}
2900 return {'w' => a[0],'h' => a[1],'cs' => colspace,'bpc' => bpc,'f'=>'DCTDecode','data' => data}
2866 end
2901 end
2867
2902
@@ -2882,11 +2917,11 class TCPDF
2882 end
2917 end
2883 w=freadint(f);
2918 w=freadint(f);
2884 h=freadint(f);
2919 h=freadint(f);
2885 bpc=f.read(1)[0];
2920 bpc=f.read(1).unpack('C')[0];
2886 if (bpc>8)
2921 if (bpc>8)
2887 Error('16-bit depth not supported: ' + file);
2922 Error('16-bit depth not supported: ' + file);
2888 end
2923 end
2889 ct=f.read(1)[0];
2924 ct=f.read(1).unpack('C')[0];
2890 if (ct==0)
2925 if (ct==0)
2891 colspace='DeviceGray';
2926 colspace='DeviceGray';
2892 elsif (ct==2)
2927 elsif (ct==2)
@@ -2896,13 +2931,13 class TCPDF
2896 else
2931 else
2897 Error('Alpha channel not supported: ' + file);
2932 Error('Alpha channel not supported: ' + file);
2898 end
2933 end
2899 if (f.read(1)[0] != 0)
2934 if (f.read(1).unpack('C')[0] != 0)
2900 Error('Unknown compression method: ' + file);
2935 Error('Unknown compression method: ' + file);
2901 end
2936 end
2902 if (f.read(1)[0]!=0)
2937 if (f.read(1).unpack('C')[0] != 0)
2903 Error('Unknown filter method: ' + file);
2938 Error('Unknown filter method: ' + file);
2904 end
2939 end
2905 if (f.read(1)[0]!=0)
2940 if (f.read(1).unpack('C')[0] != 0)
2906 Error('Interlacing not supported: ' + file);
2941 Error('Interlacing not supported: ' + file);
2907 end
2942 end
2908 f.read(4);
2943 f.read(4);
@@ -2922,9 +2957,9 class TCPDF
2922 #Read transparency info
2957 #Read transparency info
2923 t=f.read( n);
2958 t=f.read( n);
2924 if (ct==0)
2959 if (ct==0)
2925 trns = t[1][0]
2960 trns = t[1].unpack('C')[0]
2926 elsif (ct==2)
2961 elsif (ct==2)
2927 trns = t[[1][0], t[3][0], t[5][0]]
2962 trns = t[[1].unpack('C')[0], t[3].unpack('C')[0], t[5].unpack('C')[0]]
2928 else
2963 else
2929 pos=t.include?(0.chr);
2964 pos=t.include?(0.chr);
2930 if (pos!=false)
2965 if (pos!=false)
@@ -3766,6 +3801,14 class TCPDF
3766 end
3801 end
3767
3802
3768 #
3803 #
3804 # Convert to accessible file path
3805 # @param string :attrname image file name
3806 #
3807 def getImageFilename( attrname )
3808 nil
3809 end
3810
3811 #
3769 # Process opening tags.
3812 # Process opening tags.
3770 # @param string :tag tag name (in upcase)
3813 # @param string :tag tag name (in upcase)
3771 # @param string :attr tag attribute (in upcase)
3814 # @param string :attr tag attribute (in upcase)
@@ -3878,10 +3921,17 class TCPDF
3878
3921
3879 when 'img'
3922 when 'img'
3880 if (!attrs['src'].nil?)
3923 if (!attrs['src'].nil?)
3881 Write(@lasth, '!' + attrs['src'] + '!', '', fill);
3924 # Only generates image include a pdf if RMagick is avalaible
3882 =begin Comment out. Because not implement image output yet.
3925 unless Object.const_defined?(:Magick)
3883 # replace relative path with real server path
3926 Write(@lasth, attrs['src'], '', fill);
3884 attrs['src'] = attrs['src'].gsub(@@k_path_url_cache, @@k_path_cache);
3927 return
3928 end
3929 file = getImageFilename(attrs['src'])
3930 if (file.nil?)
3931 Write(@lasth, attrs['src'], '', fill);
3932 return
3933 end
3934
3885 if (attrs['width'].nil?)
3935 if (attrs['width'].nil?)
3886 attrs['width'] = 0;
3936 attrs['width'] = 0;
3887 end
3937 end
@@ -3889,10 +3939,17 class TCPDF
3889 attrs['height'] = 0;
3939 attrs['height'] = 0;
3890 end
3940 end
3891
3941
3892 Image(attrs['src'], GetX(),GetY(), pixelsToMillimeters(attrs['width']), pixelsToMillimeters(attrs['height']));
3942 begin
3893 #SetX(@img_rb_x);
3943 Image(file, GetX(),GetY(), pixelsToMillimeters(attrs['width']), pixelsToMillimeters(attrs['height']));
3894 SetY(@img_rb_y);
3944 #SetX(@img_rb_x);
3895 =end
3945 SetY(@img_rb_y);
3946 rescue => err
3947 logger.error "pdf: Image: error: #{err.message}"
3948 Write(@lasth, attrs['src'], '', fill);
3949 if File.file?( @@k_path_cache + File::basename(file))
3950 File.delete( @@k_path_cache + File::basename(file))
3951 end
3952 end
3896 end
3953 end
3897
3954
3898 when 'ul', 'ol'
3955 when 'ul', 'ol'
General Comments 0
You need to be logged in to leave comments. Login now