kramdown / converter-pdf

kramdown-converter-pdf uses Prawn to convert a kramdown document to PDF
Other
7 stars 8 forks source link

images provided as links #7

Open Try2Code opened 2 years ago

Try2Code commented 2 years ago

hi!

I would like to know if there is a way to convert markdown to pdf with inline images like

![compilation process](https://miro.medium.com/max/1036/1*wHKe6W4opLmk6pb7sxZz6w.png)

At the moment the converter seems to assume local files

gems/kramdown-converter-pdf-1.0.5/lib/kramdown/converter/pdf.rb:158:in `initialize': No such file or directory @ rb_sysopen - ./https://miro.medium.com/max/1036/1*wHKe6W4opLmk6pb7sxZz6w.png (Errno::ENOENT)
gmfvpereira commented 2 years ago

This gems does work well with URL. But there's a rescue and retry that would hide the first error, which is the one you're probably trying to fix @Try2Code .

See the code:

img_dirs = @options.fetch(:image_directories, []) + ["."]
path_or_url = img.attr["src"]
begin
  image_obj, image_info = @pdf.build_image_object(open(path_or_url))
rescue StandardError => e
  puts "ERROR: #{e}"
  raise if img_dirs.empty?
  path_or_url = File.join(img_dirs.shift, img.attr["src"])
  retry
end

On the first try your open call is likely failing. Then it rescue the error, change the path_or_url variable joining the img_dirs content and try again. Given that img_dirs will always have a "." it sounds like the error is related to the fact that ConverterPdf is looking locally for a URL, but that's false. Looking locally by joining the img_dirs is just a fallback in case the URL fails.

I had the same problem, and adding a little bit of extra logging I could figure out what was my issue. In my case, the external image was returning a 404.

begin
  image_obj, image_info = @pdf.build_image_object(open(path_or_url))
rescue StandardError => e
  puts "ERROR: #{e}"  # Extra logging
  raise if img_dirs.empty?
  path_or_url = File.join(img_dirs.shift, img.attr["src"])
  retry
end