Skip to content

Commit

Permalink
Fix issues with jpg images
Browse files Browse the repository at this point in the history
inspired by fran-worley#1
  • Loading branch information
fran-worley committed Apr 26, 2016
1 parent 5c0c6fb commit 65142a4
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions lib/htmltoword/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,18 @@ def transform_and_replace(source, stylesheet_path, file, remove_ns = false)
def local_images(source)
source.css('img').each_with_index do |image,i|
filename = image['data-filename'] ? image['data-filename'] : image['src'].split("/").last
ext = ext_from_filename(filename)
ext = File.extname(filename).delete(".").downcase

@image_files << { filename: "image#{i+1}.#{ext}", url: image['src'], ext: ext }
end
end

#get extension from filename and clean to match content_types
def ext_from_filename(filename)
ext = File.extname(filename).delete(".").downcase

case ext
when "jpg" then "jpeg"
when "tif" then "tiff"
else ext
end
def content_type_from_extension(ext)
ext == "jpg" ? "jpeg" : ext
end

#add a method to document.rb to inject the required content_types into the file...
#inject the required content_types into the [content_types].xml file...
def inject_image_content_types(source)
doc = Nokogiri::XML(source)

Expand All @@ -145,7 +139,7 @@ def inject_image_content_types(source)

#inject missing extensions into document
missing_exts.each do |ext|
doc.at_css("Types").add_child( "<Default Extension='#{ext}' ContentType='image/#{ext}'/>")
doc.at_css("Types").add_child( "<Default Extension='#{ext}' ContentType='image/#{content_type_from_extension(ext)}'/>")
end

#return the amended source to be saved into the zip
Expand Down

0 comments on commit 65142a4

Please sign in to comment.