@@ -76,7 +76,8 @@ def generate_pdf(source, *,
7676 :param extra_kwargs: any exotic extra options for wkhtmltopdf
7777 :return: string representing pdf
7878 """
79- is_url = source .lstrip ().startswith (('http' , 'www' ))
79+ if source .lstrip ().startswith (('http' , 'www' )):
80+ raise RuntimeError ('pdf generation from urls is not supported' )
8081
8182 py_args = dict (
8283 cache_dir = cache_dir ,
@@ -114,9 +115,12 @@ def generate_pdf(source, *,
114115 ]
115116 metadata = '\n ' .join (f'/{ name } ({ value } )' for name , value in fields if value )
116117
117- def gen_pdf (src , cmd_args ):
118+ with NamedTemporaryFile (suffix = '.html' , mode = 'wb' ) as html_file :
119+ html_file .write (source .encode ())
120+ html_file .flush ()
121+ html_file .seek (0 )
118122 with NamedTemporaryFile (suffix = '.pdf' , mode = 'rb' ) as pdf_file :
119- cmd_args += [src , pdf_file .name ]
123+ cmd_args += [html_file . name , pdf_file .name ]
120124 _ , stderr , returncode = execute_wk (* cmd_args )
121125 pdf_file .seek (0 )
122126 pdf_bytes = pdf_file .read ()
@@ -130,15 +134,6 @@ def gen_pdf(src, cmd_args):
130134 pdf_bytes = re .sub (b'/Title.*\n .*\n /Producer.*' , metadata .encode (), pdf_bytes , count = 1 )
131135 return pdf_bytes
132136
133- if is_url :
134- return gen_pdf (source , cmd_args )
135-
136- with NamedTemporaryFile (suffix = '.html' , mode = 'wb' ) as html_file :
137- html_file .write (source .encode ())
138- html_file .flush ()
139- html_file .seek (0 )
140- return gen_pdf (html_file .name , cmd_args )
141-
142137
143138def _string_execute (* args ):
144139 return execute_wk (* args )[0 ].decode ().strip (' \n ' )
0 commit comments