Skip to content

Instantly share code, notes, and snippets.

@justbur
Last active October 17, 2022 02:51
Show Gist options
  • Save justbur/afb5f605def2376eeffc643f7b7daae9 to your computer and use it in GitHub Desktop.
Save justbur/afb5f605def2376eeffc643f7b7daae9 to your computer and use it in GitHub Desktop.
Install pdf-tools on windows with msys2

Notes for compiling and setting up pdf-tools using msys2

Steps

  1. Open msys2 shell
  2. Get pdf-tools
    git clone https://github.com/politza/pdf-tools
    cd pdf-tools
        
  3. Update and install dependencies, skipping any you already have
    pacman -Syu
    pacman -S base-devel
    pacman -S mingw-w64-x86_64-toolchain
    pacman -S mingw-w64-x86_64-zlib
    pacman -S mingw-w64-x86_64-libpng
    pacman -S mingw-w64-x86_64-poppler
    pacman -S mingw-w64-x86_64-imagemagick
        
  4. Open mingw64 shell
  5. Compile pdf-tools
    make -s
        
  6. Open emacs
  7. Install
    M-x package-install-file RET pdf-tools-${VERSION}.tar RET
        
  8. Activate package
    M-x pdf-tools-install RET
        
  9. Test
    M-x pdf-info-check-epdfinfo RET
        

Problems

  1. Step 8 failed for me and it took me a while to figure out why. There were two problems to be fixed
    1. epdfinfo.exe was loading a library from git-for-windows and there was an error from this. To fix this I made sure the mingw libraries were ahead of the git-for-windows ones in my path like this
      (setenv "PATH" (concat "C:\\msys64\\mingw64\\bin;" (getenv "PATH")))
              
    2. The default encoding for newly created files was utf-8-dos, which uses line endings that epdfinfo doesn’t like. I originally had
      (prefer-coding-system 'utf-8)
              

      but needed

      (prefer-coding-system 'utf-8-unix)
              
@rhymer
Copy link

rhymer commented Apr 4, 2017

Thanks for sharing this, it's very helpful! In my case, I need to copy a dozen dll files from git-sdk-64/migw64/bin to elpa/pdf-tools-${VERSION} in order for pdf-tools to work.

@kbauer
Copy link

kbauer commented Jun 30, 2018

As a little remark: Changing the PATH to have mingw64 at its top can mess with other things, e.g. by overriding the systems default python distribution. This is especially problematic, since on Windows, sadly, Python 3 is also called python.exe, despite the convention to call it python3 on unix systems.

As I couldn't come up with how to produce a static executable (libpoppler denied being compiled that way with the pacman version), I essentially chose to bundle the executable with its dependencies:

  • After compilation, go to the pdf-tools/server directory in the mingw64 shell.
  • Verify that the executable can be started (run ./epdfinfo).
  • Collect the required dlls with
    ldd epdfinfo.exe | grep dll | grep -vi '/c/Windows' | perl -pe 's/.=> (.) (.*/$1/' | xargs -I FILE cp 'FILE' -v .
    which is erring on the side of caution. It will likely copy some of the dlls from the Emacs installation, but this way the executable is independent of future changes to the Emacs installation / differences on other systems.
  • Move the .dll and .exe files to some other directory.
  • Point M-x customize-variable pdf-info-epdfinfo-program to the executable.

This way a portable Windows executable should be obtained, without the risk of side-effects from changing the path.

Downside: My epdfinfo-directory has 23MB, instead of 1.2MB for the epdfinfo.exe alone.

@kbauer
Copy link

kbauer commented Jun 30, 2018

Some additional steps I had to do, to get the compilation to work:

# For cask: Install python2 in mingw, because global python.exe was python3
pacman -S python

# Make the mingw-shell aware of my python installation.
export PATH="/c/Program Files/Emacs/bin/:$PATH"

# Install cask. Certificate errors can be ignored with (unsafe) -k option.
curl -fsSL https://raw.githubusercontent.com/cask/cask/master/go | python
export PATH="$HOME/.cask/bin:$PATH"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment