Skip to content

Multi-page PDFs try to render but crash image.nvim #345

@lehmacdj

Description

@lehmacdj

Opening a markdown file that references a multi-page PDF crashes with an error from magick_cli.lua.

Crash details
Error executing callback:                                                                                                                                                        
...g/plugged/image.nvim/lua/image/processors/magick_cli.lua:36: identify: unable to open image '/var/folders/pv/tgp_ylrd4dbfs93hkd5fp8d80000gn/T/nvim.devin/OY8ksj/0/MTAwMDozOjQz
OjVhNDVjOGU5YWJlZGMxNjZjNGNjNzBiYWZiMjNhZDNiM2EyMWEwYzdmNWZhODNiMWUzNGQyYTEwOGJmZDI4MzE=-source.png': No such file or directory @ error/blob.c/OpenBlob/3596.                    
                                                                                                                                                                                 
stack traceback:                                                                                                                                                                 
        [C]: in function 'error'                                                                                                                                                 
        ...g/plugged/image.nvim/lua/image/processors/magick_cli.lua:36: in function <...g/plugged/image.nvim/lua/image/processors/magick_cli.lua:35>                             
        [C]: in function 'wait'                                                                                                                                                  
        ...g/plugged/image.nvim/lua/image/processors/magick_cli.lua:50: in function 'get_format'                                                                                 
        ...g/plugged/image.nvim/lua/image/processors/magick_cli.lua:100: in function 'get_dimensions'                                                                            
        ...es/nvim.symconfig/plugged/image.nvim/lua/image/image.lua:330: in function <...es/nvim.symconfig/plugged/image.nvim/lua/image/image.lua:241>                           
        [C]: in function 'pcall'                                                                                                                                                 
        ...ymconfig/plugged/image.nvim/lua/image/utils/document.lua:202: in function ''                                                                                          
        vim/_editor.lua: in function <vim/_editor.lua:0>                                                                                                                         
        [C]: in function 'wait'                                                                                                                                                  
        ...g/plugged/image.nvim/lua/image/processors/magick_cli.lua:85: in function 'convert_to_png'                                                                             
        ...es/nvim.symconfig/plugged/image.nvim/lua/image/image.lua:326: in function <...es/nvim.symconfig/plugged/image.nvim/lua/image/image.lua:241>                           
        [C]: in function 'pcall'                                                                                                                                                 
        ...ymconfig/plugged/image.nvim/lua/image/utils/document.lua:202: in function ''                                                                                          
        vim/_editor.lua: in function <vim/_editor.lua:0>

The root cause is:

  1. magic.lua recognizes PDFs as images
  2. this causes image.nvim to attempt to render them
  3. convert_to_png doesn't handle multi-page documents the way it handles multi-frame GIFs (i.e. truncating to the first page
  4. imagemagick CLI creates separate ...-1.png, ...-2.png, etc. files for each page of the PDF
  5. therefore image.lua:330 expects a single file at the exact output path (output-source.png)

Steps to Reproduce

  1. Create a markdown file referencing a multi-page PDF:
    ![test](path/to/multipage.pdf)
  2. Open the markdown file in neovim with image.nvim enabled

Suggested Fix

I'd suggest removing pdf from image_signatures in magic.lua:15 so that is_image returns false for PDFs and they are skipped entirely, based on lack of desire to support PDFs from #338.

Truncating to the first page like we truncate to the first frame of GIFs might be a low lift way to have extremely basic support for PDFs.

As a workaround I'm returning nil for paths with the PDF file extension in my resolve_image_path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions