Skip to content

Windows Binaries Generation

Jorj X. McKie edited this page Feb 13, 2018 · 9 revisions

This is 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

As described in the installation chapter of PyMuPDF.

I then unzip mupdf-x.xx.x-source.tar.gz (I am using 7zip) repeatedly until I get a folder named mupdf-x.xx.x-source. Go to file /include/mupdf/fitz/config.h and apply the changes recommended in our installation chapter. This will reduce your final binary very significantly! Now go to folder /platform/win32.

Generate MuPDF

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 project files to version 12.0 (MuPDF comes 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 Wiki.

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 will probably irritate your antivirus program, so you may consider to temporarily deactivate it.

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

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 creating a file named python++.bat with the following content:

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 %*

Now 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 2.5 MB if combined with the recommended limited font support above.

Adapt MuPDF Generation for x64

If you are using Python versions with a different bitness 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.

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

  • Executable Directories: C:\Program Files (x86)\Windows Kits\10\bin\x64; ...
  • Include Directories: same as x86
  • 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.

The x64 version of _fitz.pyd is several hundred KB larger than the x86 one, but below 5 MB if you follow our recommendations. Again, you may consider to further reduce its size with UPX.

Clone this wiki locally