Skip to content

Commit 1b58db0

Browse files
committed
Support for passing custom OS environment to Popen
1 parent f8e07db commit 1b58db0

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

pdfkit/configuration.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# -*- coding: utf-8 -*-
2+
import os
23
import subprocess
34
import sys
45

56

67
class Configuration(object):
7-
def __init__(self, wkhtmltopdf='', meta_tag_prefix='pdfkit-'):
8+
def __init__(self, wkhtmltopdf='', meta_tag_prefix='pdfkit-', environ=''):
89
self.meta_tag_prefix = meta_tag_prefix
910

1011
self.wkhtmltopdf = wkhtmltopdf
@@ -25,3 +26,12 @@ def __init__(self, wkhtmltopdf='', meta_tag_prefix='pdfkit-'):
2526
'If this file exists please check that this process can '
2627
'read it. Otherwise please install wkhtmltopdf - '
2728
'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
29+
30+
self.environ = environ
31+
32+
if not self.environ:
33+
self.environ = os.environ
34+
35+
for key in self.environ.keys():
36+
if not isinstance(self.environ[key], str):
37+
self.environ[key] = str(self.environ[key])

pdfkit/pdfkit.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ def __init__(self, url_or_file, type_, options=None, toc=None, cover=None,
5151
if self.source.isString():
5252
self.options.update(self._find_options_in_meta(url_or_file))
5353

54+
self.environ = self.configuration.environ
55+
5456
if options is not None: self.options.update(options)
5557

5658
self.toc = {} if toc is None else toc
@@ -125,9 +127,9 @@ def command(self, path=None):
125127

126128
def to_pdf(self, path=None):
127129
args = self.command(path)
128-
130+
129131
result = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
130-
stderr=subprocess.PIPE)
132+
stderr=subprocess.PIPE, env=self.environ)
131133

132134
# If the source is a string then we will pipe it into wkhtmltopdf.
133135
# If we want to add custom CSS to file then we read input file to

0 commit comments

Comments
 (0)