From 86672c2c661fc1c969a24826f24d60b04e2f6180 Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Fri, 20 Nov 2026 22:42:33 +0800 Subject: [PATCH] Support filesystem path objects in PDFKit inputs and commands --- README.rst | 9 +++++ pdfkit/configuration.py | 3 +- pdfkit/pdfkit.py | 4 +-- pdfkit/source.py | 10 ++++++ tests/pdfkit-tests.py | 78 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 2822f0c..98a83e9 100644 --- a/README.rst +++ b/README.rst @@ -64,6 +64,15 @@ You can pass a list with multiple URLs or files: pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf') pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf') +File paths can also be ``pathlib.Path`` objects, including entries in an input +list and the output, cover, CSS, and configured ``wkhtmltopdf`` paths: + +.. code-block:: python + + from pathlib import Path + + pdfkit.from_file(Path('test.html'), Path('out.pdf')) + Also you can pass an opened file: .. code-block:: python diff --git a/pdfkit/configuration.py b/pdfkit/configuration.py index 11785e1..9da2f9d 100644 --- a/pdfkit/configuration.py +++ b/pdfkit/configuration.py @@ -2,6 +2,7 @@ import os import subprocess import sys +from .source import _normalize_path try: FileNotFoundError except NameError: @@ -12,7 +13,7 @@ class Configuration(object): def __init__(self, wkhtmltopdf='', meta_tag_prefix='pdfkit-', environ=''): self.meta_tag_prefix = meta_tag_prefix - self.wkhtmltopdf = wkhtmltopdf + self.wkhtmltopdf = _normalize_path(wkhtmltopdf) try: if not self.wkhtmltopdf: diff --git a/pdfkit/pdfkit.py b/pdfkit/pdfkit.py index 88b3c92..d7d14b5 100644 --- a/pdfkit/pdfkit.py +++ b/pdfkit/pdfkit.py @@ -3,7 +3,7 @@ import subprocess import sys from collections import OrderedDict -from .source import Source +from .source import Source, _normalize_path from .configuration import Configuration import io import codecs @@ -123,7 +123,7 @@ def _command(self, path=None): yield '-' def command(self, path=None): - return list(self._command(path)) + return [_normalize_path(arg) for arg in self._command(path)] @staticmethod def handle_error(exit_code, stderr): diff --git a/pdfkit/source.py b/pdfkit/source.py index 3005bbd..bdf332d 100644 --- a/pdfkit/source.py +++ b/pdfkit/source.py @@ -7,12 +7,22 @@ unicode = type(u'') +def _normalize_path(path): + if hasattr(path, '__fspath__'): + return os.fsdecode(path) + return path + + class Source(object): def __init__(self, url_or_file, type_): self.source = url_or_file self.type = type_ if self.type == 'file': + if isinstance(self.source, list): + self.source = [_normalize_path(path) for path in self.source] + else: + self.source = _normalize_path(self.source) self.checkFiles() def isUrl(self): diff --git a/tests/pdfkit-tests.py b/tests/pdfkit-tests.py index 969afbd..f888ad1 100644 --- a/tests/pdfkit-tests.py +++ b/tests/pdfkit-tests.py @@ -4,6 +4,8 @@ import sys import codecs import unittest +from pathlib import Path +from unittest.mock import Mock, patch if sys.version_info[0] == 2 and sys.version_info[1] == 7: @@ -17,6 +19,82 @@ import pdfkit +class TestPathLikeFiles(unittest.TestCase): + """Path handling without requiring a wkhtmltopdf installation.""" + + def setUp(self): + self.html = Path(TESTS_ROOT) / 'fixtures' / 'example.html' + self.css = Path(TESTS_ROOT) / 'fixtures' / 'example.css' + self.configuration = pdfkit.configuration(wkhtmltopdf=sys.executable) + + def test_single_input_path(self): + kit = pdfkit.PDFKit(self.html, 'file', configuration=self.configuration) + self.assertEqual(kit.command()[-2:], [str(self.html), '-']) + + def test_mixed_input_paths(self): + paths = [self.html, str(self.html)] + kit = pdfkit.PDFKit(paths, 'file', configuration=self.configuration) + self.assertEqual(kit.command()[-3:], [str(self.html), str(self.html), '-']) + self.assertIsInstance(paths[0], Path) + + def test_custom_path_protocol(self): + class FilePath(object): + def __fspath__(self): + return os.fsencode(str(self.html)) + + path = FilePath() + path.html = self.html + kit = pdfkit.PDFKit(path, 'file', configuration=self.configuration) + self.assertEqual(kit.command()[-2:], [str(self.html), '-']) + + def test_missing_input_path(self): + with self.assertRaises(IOError): + pdfkit.PDFKit(self.html / 'missing', 'file', + configuration=self.configuration) + + def test_path_with_css(self): + kit = pdfkit.PDFKit(self.html, 'file', css=self.css, + configuration=self.configuration) + self.assertEqual(kit.command()[-2:], ['-', '-']) + self.assertIn('