walle / gimli

Utility for converting markup files to pdf files
https://github.com/walle/gimli
MIT License
538 stars 44 forks source link

Relative images does not work #5

Closed walle closed 13 years ago

walle commented 13 years ago

There seems to be a bug in PDFKit that does not include relative images in the pdf.

walle commented 13 years ago

This is going in release 0.1.5.

nicknovitski commented 13 years ago

This seems to expand relative paths based on the program's working directory, not the source file location: njay@67246185fc0e013fc436

I've tried making MarkupFile inject absolute paths based on file locations into image tags by default, but it's not working for some reason.

walle commented 13 years ago

Yes, that's the standard behavior. So if you run gimli in a directory with a markup file and a directory with images you can reference them as images/image.png etc.

But perhaps a better way would be referencing them relative from the file. It has the same behavior, but works from anywhere. Nice catch!

I think the best solution would be adding the path to the file to the convert_image_urls method and setting the base_path to that when retrieving the absolute url. Something like this:

def convert_image_urls(html, filename)
  dir_string = ::File.dirname(::File.expand_path(filename))
  html.scan(/<img[^>]+src="([^"]+)"/).each do |url|
    html.gsub!(url[0], ::File.expand_path(url[0], dir_string)) unless url[0] =~ /^https?/
  end
  html
end

This is just from the top of my head, but it should work. Perhaps this should be a new issue?

walle commented 13 years ago

I'll reopen this so we can add the solution where images is loaded relative to the file instead of working directory.

walle commented 13 years ago

This should be fixed in commit b1b7841. Will be present in the next release.

walle commented 13 years ago

Fixed in 0.1.9