Skip to content

Instantly share code, notes, and snippets.

@thbar
Last active November 3, 2023 06:01
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save thbar/4943276 to your computer and use it in GitHub Desktop.
Save thbar/4943276 to your computer and use it in GitHub Desktop.
How to diff PDF files with Git

One can use MD5 or plain text diff to see differences in PDF files. If that's not enough, here's how to use diff-pdf which knows how to diff based on appearance or words:

  • brew install diff-pdf
  • edit your ~/.gitconfig to add this:
[difftool "diffpdf"]
  cmd = diff-pdf --view \"$LOCAL\" \"$REMOTE\"
  • then use with:
git difftool --tool=diffpdf your_pdf_file.pdf

I suspect there's maybe some way to force always using a specific difftool for files with a specific extension; in the mean time I'm just using a bash function for this.

Also useful in .bash_profile:

function git_diff_pdf {
  yes | git difftool --tool=diffpdf $1
}

then you can use it like:

git_diff_pdf your_pdf_file.pdf
@raphink
Copy link

raphink commented Aug 31, 2015

This is great, thank you!

@wookayin
Copy link

As of 2016, it seems that the brew formular has changed its name to diff-pdf. So I tried the following:

brew install diff-pdf

which gives the diff-pdf binary. In OS X, it is convenient to use --view option for viewing the differences in a window.

[difftool "diffpdf"]
  cmd = diff-pdf --view \"$LOCAL\" \"$REMOTE\"

and it worked nice to me.

@thbar
Copy link
Author

thbar commented May 7, 2016

@wookayin great, thanks for the feedback! I've updated the gist.

@ggrrll
Copy link

ggrrll commented Oct 22, 2018

@thbar thanks for the tool

However, in order to have it run, I changed the command to

diff-pdf --view \"$LOCAL\" \"$REMOTE\"

@thbar
Copy link
Author

thbar commented Dec 11, 2018

@ggrrll thanks! I've updated the gist.

@grubersjoe
Copy link

grubersjoe commented Jan 20, 2019

Thanks for this gist. Note: diffpdf under Linux doesn't seem to be have a --view option. So in this case the following is sufficient (the escaping of the double quotes is also not necessary in my point of view):

[difftool "diffpdf"]
    cmd = diffpdf "$LOCAL" "$REMOTE"

@seppestas
Copy link

Note: brew install diff-pdf installs https://vslavik.github.io/diff-pdf/ (GPL licensed, sources: https://github.com/vslavik/diff-pdf), not the "commercial Windows graphical user interface" DiffPDF as mentioned in this gist. See https://formulae.brew.sh/formula/diff-pdf.

@thbar
Copy link
Author

thbar commented Nov 26, 2019

@seppestas thanks for the notice! I just updated the link.

@thbar
Copy link
Author

thbar commented Nov 26, 2019

@grubersjoe I believe this is (as pointed out by @seppestas) because it's not the correct program (although named almost exactly the same). Check out this version, which installs on Linux and has the --view option: https://github.com/vslavik/diff-pdf

@grubersjoe
Copy link

Ah... I see 😄. I've mistakenly used http://www.qtrac.eu/diffpdf-foss.html. Still works though.

@westurner
Copy link

From https://nbdime.readthedocs.io/en/latest/vcs.html#manual-registration :

To associate the diff driver with a file type, add the following entry to the appropriate .gitattributes file:

*.ipynb diff=jupyternotebook

Would be great to have this support in git core.

@olberger
Copy link

Note: brew install diff-pdf installs https://vslavik.github.io/diff-pdf/ (GPL licensed, sources: https://github.com/vslavik/diff-pdf), not the "commercial Windows graphical user interface" DiffPDF as mentioned in this gist. See https://formulae.brew.sh/formula/diff-pdf.

FWIW, an earlier version of DiffPDF is still available under the GPL v2 from http://www.qtrac.eu/diffpdf-foss.html (and packaged in Debian).

@matthiasbeyer
Copy link

matthiasbeyer commented Jul 29, 2021

If you use git-annex, you can diff PDFs via

[difftool "diffpdf"]
    cmd = diffpdf \"$(cat $LOCAL)\" \"$(cat $REMOTE)\"

Rationale: The $LOCAL and $REMOTE variables point to temporary files containing the two versions. With git-annex, they contain only a path to the actual file in the .git/annex/objects store. That's why we need to cat these files to get the actual path and make them viewed in diffpdf.

Edit:

For some more ninja-coolness, you can use

[difftool "diffpdf"]
       cmd = diffpdf \"$(git rev-parse --show-toplevel)/$(cat $LOCAL)\" \"$(git rev-parse --show-toplevel)/$(cat $REMOTE)\"

which makes them paths absolute, so you can execute this from a sub-directory of your repository, too!

@sdutoit
Copy link

sdutoit commented Jun 9, 2023

With a recent version of git (I'm using 2.40.1) you can do this pretty easily using diff in both the git config and .gitattributes (official docs):

In your project's .gitattributes:

*.pdf diff=diff-pdf

In your ~/.gitconfig or your project's .git/config:

[diff "diff-pdf"]
        command = f() { diff-pdf --view "$2 $1;" } ";" f

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