Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``:
Expand Down
15 changes: 2 additions & 13 deletions pdfkit/configuration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
import os
import subprocess
import sys
import shutil
try:
FileNotFoundError
except NameError:
Expand All @@ -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:
Expand Down
46 changes: 46 additions & 0 deletions tests/test_configuration.py
Original file line number Diff line number Diff line change
@@ -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)