Search code examples
pdfpandoc

failed to load an image when converting html to pdf with pandoc


I use jekyll to generate html files having an image like:

<img src="/assets/images/view.png" alt="" />

When I generate a PDF with pandoc with that HTML, it shows:

Warning: Failed to load file:///assets/images/view.png (ignore)

The resulting PDF doesn't contain the image.

I think that's because the image's path is absolute, it loads from the file system, absolute path. I have tried --resource-path=assets/images/ but doesn't help. Does anyone know how to load images successfully under this case?


Solution

  • There might be a simpler solution, but I'd solve this by using a Lua filter that fixes the image path:

    function Image (img)
      -- remove leading slash from image paths
      img.src = img.src:gsub('^/', '')
      return img
    end
    

    Save this to a file fix-img.lua and pass the file to pandoc via the --lua-filter option.