From 9a6cd95e5fe2826d40861229aaa0431b76e302e7 Mon Sep 17 00:00:00 2001 From: Suyash Behera Date: Thu, 17 Sep 2020 15:44:42 +0530 Subject: [PATCH] load zlib before liblept on windows (#633) fixes #631 --- src/ocrmypdf/leptonica.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/ocrmypdf/leptonica.py b/src/ocrmypdf/leptonica.py index 808846df..336c00ff 100644 --- a/src/ocrmypdf/leptonica.py +++ b/src/ocrmypdf/leptonica.py @@ -58,6 +58,24 @@ if not _libpath: --------------------------------------------------------------------- """ ) +if os.name == 'nt': + # On Windows, recent versions of libpng require zlib. We have to make sure + # the zlib version being loaded is the same one that libpng was built with. + # This tries to import zlib from Tesseract's installation folder, falling back + # to find_library() if liblept is being loaded from somewhere else. + # Loading zlib from other places could cause a version mismatch + _zlib_path = os.path.join(os.path.dirname(_libpath), 'zlib1.dll') + if not os.path.exists(_zlib_path): + _zlib_path = find_library('zlib') + try: + zlib = ffi.dlopen(_zlib_path) + except ffi.error as e: + raise MissingDependencyError( + """ + Could not load the zlib library. It could be that Tesseract is not installed properly, + we can't find the installation on your system PATH environment variable. + """ + ) from e try: lept = ffi.dlopen(_libpath) lept.setMsgSeverity(lept.L_SEVERITY_WARNING)