Skip to content

Windows Binaries Generation

Jorj X. McKie edited this page Jun 10, 2016 · 9 revisions

This a "How To" install PyMuPDF and MuPDF using Visual Studio.

I am using this procedure to create the binary setup files for PyMuPDF offered in the opetional material repo for PyMuPDF.

Download MuPDF 1.9

As described in the installation chapter of PyMuPDF. Currently (as of May 10th), only version 1.9a can be seen there. But under the archive directory earlier versions are available, too.

I then unzip mupdf-1.9-source.tar.gz (I am using 7zip) until I get a folder named mupdf-1.9-source. Inside this folder, look for directory platform/win32.

Generate MuPDF 1.9

Double click on mupdf.sln in this folder to open it with Visual Studio. The Visual Studio version I am using is VS Community 2013 (version 2015 should work too, but you need to use the platform toolset Visual Studio 2013 (v120) in it to compile MuPDF).

VS will automatically upgrade the solution and all 13 project files to version 12.0 (they come with version 8.0).

Now we need to decide whether we generate for x86 or x64 bitness. Let us assume x86. For a x64 version, see the paragraph at the end of this Uncyclo.

Start Configuration Manager in the BUILD menu and set Active solution configuration to Release and Active solution platform to Win32. Verify that all check boxes in the Build column are checked. Close configuration manager.

Now update the VC++ directories for all projects using their Property Pages. You need a Windows SDK to successfully proceed. For my Windows 10, I am using Windows 10 SDK. For every project, prefix the following 3 directories with the respective SDK directory (using my configuration example - it will be a little different names for Windows 7 and 8):

  • Executable Directories: C:\Program Files (x86)\Windows Kits\10\bin\x86; ...
  • Include Directories: C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\shared;C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\um; ...
  • Library Directories: C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10240.0\um\x86; ...

Now generate the solution. In this process, the project generated will create .exe files and execute them. This may irritate your antivirus program, so you may consider to temporarily deactivate it.

If all worked well you will find the required library files libfonts.lib (17.5 MB), libmupdf.lib (9.3 MB) and libthirdparty.lib (13.1 MB) (and many more!) in directory ...\platform\win32\Release.

If you use these library files as described so far, your PyMuPDF binary will be a quite large animal of 17+ MB (!). This is because MuPDF 1.9 includes support for many, many dozens of fonts from all over the world. If you do not need that or do not like that (I don't), you can reduce their number like so:

  • edit file fitz.h in directory mupdf-1.9-source/include/mupdf

  • insert 2 lines as follows

    #ifndef MUDPF_FITZ_H
    #define MUDPF_FITZ_H

    #define NOTO_SMALL /* use a NOTO fonts subset only */
    #define TOFU_CJK /* exclude Android specific fonts */

    ...

  • save and rebuild the solution as described above. Your PyMuPDF binary created in the next step will now become only around 8 MB large. Don't get confused: the *.lib file sizes will not have changed.

Download and Generate PyMuPDF

After downloading and unzipping, first check that all your include and library directory specs are in order in your setup.py file. E.g. either copy the *.lib files from above into the a directory named in setup.py or change the script. Similar treatment for include directories.

Second, make sure that your Python version uses the same compiler / linker version as the one used in your Visual Studio.

Now generate by invoking python setup.py install.

Done.

Fixing Problems

A common problem is error: Unable to find vcvarsall.bat. An easy fix is using the following python++.bat file:

PUSHD "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC"
call vcvarsall.bat x86
SET DISTUTILS_USE_SDK=1
SET MSSdk=1
POPD
python %*

Do your setup with the command python++ setup.py install.

Reducing Binary's Size with UPX

Another option to reduce the binary's _fitz.pyd file size is using UPX. This can be done like so:

python++ setup.py build
pushd "...\build\lib.win32-2.7\fitz"
upx -9 *.pyd
popd
python setup.py install

This will cause the setup files to be only created / compiled (Python 2.7 used as an example) at first. Then a compress is performed and the result finally copied to the Python site-packages directory.

UPX will compress _fitz.pyd by more than 50%, currently yielding a file size of about 4 MB if combined with the recommended limited font support above.

Adapt MuPDF Generation for x64

If you are using Python versions with a different biness in parallel, start with a fresh, separate copy of MuPDF's sources. Now Proceed as detailed out above. Use the Configuration Manager to specify "Release" and "x64" as Active Solution Configuration and Active Solution Platform, respectively. Projects libfonts and jni have no dropdown option "x64" - you will have to create it with <New...>.

Use the following directory prefixes on the 13 projects' Property Pages:

  • Executable Directories: C:\Program Files (x86)\Windows Kits\10\bin\x64; ...
  • Include Directories: same as above
  • Library Directories: C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10240.0\um\x64; ...

With these specifications you will find the *.lib files in directory ...\platform\win32\x64\Release.

Do not forget to modify fitz.h if you want to reduce fonts support.

The x64 version of _fitz.pyd is about 300 KB larger than the x86 one.

Clone this wiki locally