1717
1818THIS_DIR = Path (__file__ ).parent .resolve ()
1919WK_PATH = str (THIS_DIR / 'bin' / 'wkhtmltopdf' )
20+ DFT_CACHE_DIR = Path (tempfile .gettempdir ()) / 'pydf_cache'
2021
2122
2223def _execute_wk (* args , input = None ):
@@ -30,7 +31,7 @@ def _execute_wk(*args, input=None):
3031 return subprocess .run (wk_args , input = input , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
3132
3233
33- def _convert_args (py_args ):
34+ def _convert_args (** py_args ):
3435 cmd_args = []
3536 for name , value in py_args .items ():
3637 if value in {None , False }:
@@ -61,13 +62,12 @@ def _set_meta_data(pdf_content, **kwargs):
6162
6263
6364class AsyncPydf :
64- def __init__ (self , * , max_processes = 20 , loop = None ):
65+ def __init__ (self , * , max_processes = 20 , loop = None , cache_dir = DFT_CACHE_DIR ):
6566 self .semaphore = asyncio .Semaphore (value = max_processes , loop = loop )
6667 self .loop = loop
67- cache_dir = Path (tempfile .gettempdir ()) / 'pydf_cache'
6868 if not cache_dir .exists ():
6969 Path .mkdir (cache_dir )
70- self .cache_dir = str ( cache_dir )
70+ self .cache_dir = cache_dir
7171
7272 async def generate_pdf (self ,
7373 html ,
@@ -77,8 +77,7 @@ async def generate_pdf(self,
7777 creator = None ,
7878 producer = None ,
7979 ** cmd_args ):
80- cmd_args .setdefault ('cache_dir' , self .cache_dir )
81- cmd_args = [WK_PATH ] + _convert_args (cmd_args )
80+ cmd_args = [WK_PATH ] + _convert_args (cache_dir = self .cache_dir , ** cmd_args )
8281 async with self .semaphore :
8382 p = await asyncio .create_subprocess_exec (
8483 * cmd_args ,
@@ -107,25 +106,25 @@ async def generate_pdf(self,
107106
108107
109108def generate_pdf (html , * ,
110- title = None ,
111- author = None ,
112- subject = None ,
113- creator = None ,
114- producer = None ,
109+ title : str = None ,
110+ author : str = None ,
111+ subject : str = None ,
112+ creator : str = None ,
113+ producer : str = None ,
115114 # from here on arguments are passed via the commandline to wkhtmltopdf
116- cache_dir = None ,
117- grayscale = False ,
118- lowquality = False ,
119- margin_bottom = None ,
120- margin_left = None ,
121- margin_right = None ,
122- margin_top = None ,
123- orientation = None ,
124- page_height = None ,
125- page_width = None ,
126- page_size = None ,
127- image_dpi = None ,
128- image_quality = None ,
115+ cache_dir : Path = DFT_CACHE_DIR ,
116+ grayscale : bool = False ,
117+ lowquality : bool = False ,
118+ margin_bottom : str = None ,
119+ margin_left : str = None ,
120+ margin_right : str = None ,
121+ margin_top : str = None ,
122+ orientation : str = None ,
123+ page_height : str = None ,
124+ page_width : str = None ,
125+ page_size : str = None ,
126+ image_dpi : str = None ,
127+ image_quality : str = None ,
129128 ** extra_kwargs ):
130129 """
131130 Generate a pdf from either a url or a html string.
@@ -158,8 +157,8 @@ def generate_pdf(html, *,
158157 :param extra_kwargs: any exotic extra options for wkhtmltopdf
159158 :return: string representing pdf
160159 """
161- if html . lstrip (). startswith (( 'http' , 'www' ) ):
162- raise ValueError ( 'pdf generation from urls is not supported' )
160+ if not cache_dir . exists ( ):
161+ Path . mkdir ( cache_dir )
163162
164163 py_args = dict (
165164 cache_dir = cache_dir ,
@@ -177,7 +176,7 @@ def generate_pdf(html, *,
177176 image_quality = image_quality ,
178177 )
179178 py_args .update (extra_kwargs )
180- cmd_args = _convert_args (py_args )
179+ cmd_args = _convert_args (** py_args )
181180
182181 p = _execute_wk (* cmd_args , input = html .encode ())
183182 pdf_content = p .stdout
0 commit comments