Skip to content

Commit c8a896c

Browse files
committed
hide cmd window on Windows when running subprocess
1 parent ec22673 commit c8a896c

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

pdfkit/configuration.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ def __init__(self, wkhtmltopdf='', meta_tag_prefix='pdfkit-', environ=''):
1717
try:
1818
if not self.wkhtmltopdf:
1919
if sys.platform == 'win32':
20+
#hide cmd window
21+
startupinfo = subprocess.STARTUPINFO()
22+
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
23+
startupinfo.wShowWindow = subprocess.SW_HIDE
24+
2025
self.wkhtmltopdf = subprocess.Popen(
21-
['where.exe', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0]
26+
['where.exe', 'wkhtmltopdf'], stdout=subprocess.PIPE, startupinfo=startupinfo).communicate()[0]
2227
else:
2328
self.wkhtmltopdf = subprocess.Popen(
2429
['which', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0]

pdfkit/pdfkit.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,28 @@ def handle_error(exit_code, stderr):
154154
def to_pdf(self, path=None):
155155
args = self.command(path)
156156

157-
result = subprocess.Popen(
158-
args,
159-
stdin=subprocess.PIPE,
160-
stdout=subprocess.PIPE,
161-
stderr=subprocess.PIPE,
162-
env=self.environ
163-
)
157+
if sys.platform == 'win32':
158+
#hide cmd window
159+
startupinfo = subprocess.STARTUPINFO()
160+
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
161+
startupinfo.wShowWindow = subprocess.SW_HIDE
162+
163+
result = subprocess.Popen(
164+
args,
165+
stdin=subprocess.PIPE,
166+
stdout=subprocess.PIPE,
167+
stderr=subprocess.PIPE,
168+
env=self.environ,
169+
startupinfo=startupinfo
170+
)
171+
else:
172+
result = subprocess.Popen(
173+
args,
174+
stdin=subprocess.PIPE,
175+
stdout=subprocess.PIPE,
176+
stderr=subprocess.PIPE,
177+
env=self.environ
178+
)
164179

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

0 commit comments

Comments
 (0)