Skip to content

Commit bff2e7c

Browse files
committed
Merge pull request JazzCore#7 from mmarchini/master_jazzcore
Adding the possibility to pass multiple css files as a parameter
2 parents aaceeb6 + 103e505 commit bff2e7c

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

pdfkit/pdfkit.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class PDFKit(object):
1919
:param options: dict (optional) with wkhtmltopdf options, with or w/o '--'
2020
:param toc: dict (optional) - toc-specific wkhtmltopdf options, with or w/o '--'
2121
:param cover: str (optional) - url/filename with a cover html page
22-
:param css: str (optional) - path to css file which will be added to input string
2322
:param configuration: (optional) instance of pdfkit.configuration.Configuration()
2423
"""
2524

@@ -163,11 +162,17 @@ def _style_tag_for(self, stylesheet):
163162

164163
def _prepend_css(self, path):
165164
if self.source.isUrl() or isinstance(self.source.source, list):
166-
raise self.ImproperSourceError('CSS file can be added only to a single '
165+
raise self.ImproperSourceError('CSS files can be added only to a single '
167166
'file or string')
168167

169-
with open(path) as f:
170-
css_data = f.read()
168+
if not isinstance(path, list):
169+
path = [path]
170+
171+
css_data = []
172+
for p in path:
173+
with open(p) as f:
174+
css_data.append(f.read())
175+
css_data = "\n".join(css_data)
171176

172177
if self.source.isFile():
173178
with open(self.source.to_s()) as f:

tests/fixtures/example2.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a { color: blue; }

tests/pdfkit-tests.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,33 @@ def test_stylesheet_adding_without_head_tag(self):
263263
r._prepend_css('fixtures/example.css')
264264
self.assertIn('<style>%s</style><html>' % css, r.source.to_s())
265265

266+
def test_multiple_stylesheets_adding_to_the_head(self):
267+
#TODO rewrite this part of pdfkit.py
268+
css_files = ['fixtures/example.css', 'fixtures/example2.css']
269+
r = pdfkit.PDFKit('<html><head></head><body>Hai!</body></html>', 'string',
270+
css=css_files)
271+
272+
css=[]
273+
for css_file in css_files:
274+
with open(css_file) as f:
275+
css.append(f.read())
276+
277+
r._prepend_css(css_files)
278+
self.assertIn('<style>%s</style>' % "\n".join(css), r.source.to_s())
279+
280+
def test_multiple_stylesheet_adding_without_head_tag(self):
281+
css_files = ['fixtures/example.css', 'fixtures/example2.css']
282+
r = pdfkit.PDFKit('<html><body>Hai!</body></html>', 'string',
283+
options={'quiet': None}, css=css_files)
284+
285+
css=[]
286+
for css_file in css_files:
287+
with open(css_file) as f:
288+
css.append(f.read())
289+
290+
r._prepend_css(css_files)
291+
self.assertIn('<style>%s</style><html>' % "\n".join(css), r.source.to_s())
292+
266293
def test_stylesheet_throw_error_when_url(self):
267294
r = pdfkit.PDFKit('http://ya.ru', 'url', css='fixtures/example.css')
268295

0 commit comments

Comments
 (0)