From 76323859f6745e768245e1ebf06301d9638f3ce2 Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Sat, 21 Nov 2026 20:59:15 +0800 Subject: [PATCH] Find wkhtmltopdf without external lookup utilities --- README.rst | 2 +- pdfkit/configuration.py | 15 ++---------- tests/test_configuration.py | 46 +++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 tests/test_configuration.py diff --git a/README.rst b/README.rst index 2822f0c..5e8e4f7 100644 --- a/README.rst +++ b/README.rst @@ -156,7 +156,7 @@ Configuration Each API call takes an optional configuration parameter. This should be an instance of ``pdfkit.configuration()`` API call. It takes the configuration options as initial parameters. The available options are: -* ``wkhtmltopdf`` - the location of the ``wkhtmltopdf`` binary. By default ``pdfkit`` will attempt to locate this using ``which`` (on UNIX type systems) or ``where`` (on Windows). +* ``wkhtmltopdf`` - the location of the ``wkhtmltopdf`` binary. By default ``pdfkit`` searches ``PATH`` using Python's ``shutil.which``; no external ``which`` or ``where`` command is required. * ``meta_tag_prefix`` - the prefix for ``pdfkit`` specific meta tags - by default this is ``pdfkit-`` Example - for when ``wkhtmltopdf`` is not on ``$PATH``: diff --git a/pdfkit/configuration.py b/pdfkit/configuration.py index 11785e1..a55150b 100644 --- a/pdfkit/configuration.py +++ b/pdfkit/configuration.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- import os -import subprocess -import sys +import shutil try: FileNotFoundError except NameError: @@ -16,17 +15,7 @@ def __init__(self, wkhtmltopdf='', meta_tag_prefix='pdfkit-', environ=''): try: if not self.wkhtmltopdf: - if sys.platform == 'win32': - #hide cmd window - startupinfo = subprocess.STARTUPINFO() - startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW - startupinfo.wShowWindow = subprocess.SW_HIDE - - self.wkhtmltopdf = subprocess.Popen( - ['where.exe', 'wkhtmltopdf'], stdout=subprocess.PIPE, startupinfo=startupinfo).communicate()[0] - else: - self.wkhtmltopdf = subprocess.Popen( - ['which', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0] + self.wkhtmltopdf = shutil.which('wkhtmltopdf') or '' lines = self.wkhtmltopdf.splitlines() if len(lines) > 0: diff --git a/tests/test_configuration.py b/tests/test_configuration.py new file mode 100644 index 0000000..dc85473 --- /dev/null +++ b/tests/test_configuration.py @@ -0,0 +1,46 @@ +import os + +import pytest + +import pdfkit + + +def make_executable(directory): + name = 'wkhtmltopdf.exe' if os.name == 'nt' else 'wkhtmltopdf' + executable = directory / name + executable.write_bytes(b'placeholder for discovery only') + executable.chmod(0o755) + return executable + + +def test_find_executable_without_external_lookup_tools(tmp_path, monkeypatch): + executable = make_executable(tmp_path) + monkeypatch.setenv('PATH', str(tmp_path)) + if os.name == 'nt': + monkeypatch.setenv('PATHEXT', '.EXE') + + def reject_lookup_process(*args, **kwargs): + raise AssertionError('executable discovery must not launch a helper process') + + monkeypatch.setattr('subprocess.Popen', reject_lookup_process) + configuration = pdfkit.configuration() + + assert os.path.samefile(configuration.wkhtmltopdf, str(executable)) + + +def test_missing_executable_has_installation_guidance(tmp_path, monkeypatch): + monkeypatch.setenv('PATH', str(tmp_path)) + + with pytest.raises(IOError, match='No wkhtmltopdf executable found') as error: + pdfkit.configuration() + + assert 'Installing-wkhtmltopdf' in str(error.value) + + +def test_explicit_executable_does_not_require_path_lookup(tmp_path, monkeypatch): + executable = make_executable(tmp_path) + monkeypatch.setenv('PATH', '') + + configuration = pdfkit.configuration(wkhtmltopdf=str(executable)) + + assert configuration.wkhtmltopdf == str(executable)