
    .i!+                         d dl Z d dlZd dlZd dlmZ ddlmZ ddlmZ d dl	Z	d dl
Z
	 e e n# e$ r eZeZY nw xY w G d de          ZdS )    N)OrderedDict   )Source)Configurationc                       e Zd ZdZ G d de          Z	 	 ddZd ZddZdd	Z	e
d
             ZddZd Zd Zd Zd Zd ZdS )PDFKita,  
    Main class that does all generation routine.

    :param url_or_file: str - either a URL, a path to a file or a string containing HTML
                       to convert
    :param type_: str - either 'url', 'file' or 'string'
    :param options: dict (optional) with wkhtmltopdf options, with or w/o '--'
    :param toc: dict (optional) - toc-specific wkhtmltopdf options, with or w/o '--'
    :param cover: str (optional) - url/filename with a cover html page
    :param configuration: (optional) instance of pdfkit.configuration.Configuration()
    c                       e Zd ZdZd Zd ZdS )PDFKit.ImproperSourceErrorz!Wrong source type for stylesheetsc                     || _         d S Nmsg)selfr   s     ]/var/www/html/Pagina-Ingenieria-Requisitos/venv/lib/python3.11/site-packages/pdfkit/pdfkit.py__init__z#PDFKit.ImproperSourceError.__init__#   s    DHHH    c                     | j         S r   r   )r   s    r   __str__z"PDFKit.ImproperSourceError.__str__&   s	    8Or   N)__name__
__module____qualname____doc__r   r    r   r   ImproperSourceErrorr
       s8        //	 	 		 	 	 	 	r   r   NFc
                 T   t          ||          | _        |t                      n|| _        	 | j        j                            d          | _        n!# t          $ r | j        j        | _        Y nw xY wt                      | _        | j        	                                r-| j        
                    |                     |                     | j        j        | _        || j        
                    |           |i n|| _        || _        || _        |	| _        || _        g | _        d S )Nutf-8)r   sourcer   configurationwkhtmltopdfdecodeAttributeErrorr   optionsisStringupdate_find_options_in_metaenvirontoccovercover_firstverbosecssstylesheets)
r   url_or_filetype_r"   r'   r(   r+   r   r)   r*   s
             r   r   zPDFKit.__init__)   s,    [%001>1Fmooo#0 		>#1=DDWMMD 	> 	> 	>#1=D	> #}};!! 	IL : :; G GHHH)1L(((22#
&s   $A A10A1c              #     K   |                      |          D ]k\  }}|V  t          |t          t          f          rBt	          |          dk    r|d         r|d         s
J d            |d         V  |d         V  g|V  ldS )z
        Generator of args parts based on options specification.

        Note: Empty parts will be filtered out at _command generator
           r   r   zFOption value can only be either a string or a (tuple, list) of 2 itemsN)_normalize_options
isinstancelisttuplelen)r   optsoptkeyoptvals       r   _genargszPDFKit._genargsD   s       #55d;; 	 	NFFLLL&4-00 6{{a''F1I'&)''  FN'''QiQi	 	r   c              #     K   | j         r|                     | j                    | j        V  | j        s| j                            ddi           |                     | j                  D ]}|r|V  	| j        r| j        rdV  | j        V  | j	        r'dV  |                     | j	                  D ]}|r|V  	| j        r| j        sdV  | j        V  | j
                                        s| j
                                        rdV  nNt          | j
        j
        t                    r| j
                                        V  n| j
        j
        D ]}|V  |r|V  dS dV  dS )z0
        Generator of all command parts
        --quiet r(   r'   -N)r+   _prepend_cssr   r*   r"   r$   r9   r(   r)   r'   r   r#   	isFileObjr2   
basestringto_s)r   pathargpartss       r   _commandzPDFKit._commandT   s      8 	(dh'''| 	1LB000}}T\22 	 	G : 	$* 	MMM*8 	"KKK==22 " " "!MMM: 	d. 	MMM* ;!! 	T[%:%:%<%< 	IIII$+,j99 k&&((((((+  AGGGG  	JJJJJIIIIIr   c                 F    t          |                     |                    S r   )r3   rE   )r   rB   s     r   commandzPDFKit.command   s    DMM$''(((r   c                 j   | dk    rd S |                                 }t          |          dk    r2|                                 d                                         dk    rd S d|v rt          d|z            d|v rt          d|z             |pd	}t          d
                    | |                    )Nr   r   Donezcannot connect to X serverz%s
You will need to run wkhtmltopdf within a "virtual" X server.
Go to the link below for more information
https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-serverErrorzwkhtmltopdf reported an error:
zUnknown Errorz5wkhtmltopdf exited with non-zero code {0}. error:
{1})
splitlinesr5   stripIOErrorformat)	exit_codestderrstderr_lines	error_msgs       r   handle_errorzPDFKit.handle_error   s    >>F((**
 |q  V%6%6%8%8%<%B%B%D%D%N%NF'611 n qww x x x
 f<vEFFF-o	NUUV_ajkklllr   c                    |                      |          }t          j        dk    r|t          j                    }|xj        t          j        z  c_        t          j        |_        t          j	        |t          j
        t          j
        t          j
        | j        |          }n<t          j	        |t          j
        t          j
        t          j
        | j                  }| j                                        s | j                                        r4| j        r-| j                                                            d          }nM| j                                        r2| j        j                                                            d          }nd }|                    |          \  }}|p|pd}|                    dd          }|j        }|                     ||           d	|vrt          j                            |           |s|S 	 t5          j        |d
          5 }	|	                    d          }
|
dk    r%t9          dd                    |          z            	 d d d            dS # 1 swxY w Y   d S # t8          t<          f$ r.}t9          dd                    |          d|d          d }~ww xY w)Nwin32)stdinstdoutrQ   envstartupinfo)rW   rX   rQ   rY   r   )inputr   replace)errorsr;   encoding   r<   zBCommand failed: %s
Check whhtmltopdf output without 'quiet' option TzCommand failed: z1
Check whhtmltopdf output without 'quiet' option
)rG   sysplatform
subprocessSTARTUPINFOdwFlagsSTARTF_USESHOWWINDOWSW_HIDEwShowWindowPopenPIPEr&   r   r#   isFiler+   rA   encoder?   readcommunicater    
returncoderT   rX   writecodecsopenrN   joinOSError)r   rB   argsrZ   resultr[   rX   rQ   rP   ftextes               r   to_pdfzPDFKit.to_pdf   s   ||D!!<7""$022K:#BB&0&8K#% o!!L'  FF  % o!!L  F ;!! 	dk&8&8&:&: 	tx 	K$$&&--g66EE[""$$ 	K&++--44W==EEE++%+88(6(Swy99%	)V,,, D  JV$$$ 	M	7TG444 vvayy2::! #+-0XXd^^#< = = =                   ! 	7 	7 	7'#&88D>>>>1116 7 7 7	7s=   I> "AI1$I> 1I55I> 8I59I> >J=)J88J=c              #   l  K   t          |                                          D ]\  }}d|vrd|                     |          z  }n|                     |          }t          |t           t          f          r|D ]}||fV  	_t          |t
                    rdn|}||rt          |          n|fV  dS )ah   Generator of 2-tuples (option-key, option-value).
        When options spec is a list, generate a 2-tuples per list item.

        :param options: dict {option name: value}

        returns:
          iterator (option-key, option-value)
          - option names lower cased and prepended with
          '--' if necessary. Non-empty values cast to str
        z--z--%sr<   N)r3   items_normalize_argr2   r4   boolunicode)r   r"   keyvaluenormalized_keyr8   normalized_values          r   r1   zPDFKit._normalize_options   s       w}}// 	V 	VJC3!'$*=*=c*B*B!B!%!4!4S!9!9%$// V# 3 3F)6222223 *4E$)?)?#J22U %E'Tw/?'@'@'@uUUUUU	V 	Vr   c                 *    |                                 S r   )lower)r   args     r   r~   zPDFKit._normalize_arg   s    yy{{r   c                     d|z  S )Nz<style>%s</style>r   )r   
stylesheets     r   _style_tag_forzPDFKit._style_tag_for   s    "Z//r   c                    | j                                         st          | j         j         t                    r|                     d          t          |t                    s|g}g }|D ]W}t          j        |d          5 }|                    |                                           d d d            n# 1 swxY w Y   Xd	                    |          }| j         
                                rt          j        | j                                         d          5 }|                                }d d d            n# 1 swxY w Y   t          |                    d|                     |          dz             d          | _         d S | j                                         rd| j                                         v rO| j                                                             d|                     |          dz             | j         _         d S |                     |          | j                                         z   | j         _         d S d S )Nz6CSS files can be added only to a single file or stringzUTF-8r^   
z</head>string)r   isUrlr2   r3   r   rr   rs   appendrn   rt   rl   rA   r   r\   r   r#   )r   rB   css_dataprx   inps         r   r>   zPDFKit._prepend_css   s   ; 	=*T[-?"F"F 	=** ,< = = = $%% 	6D 	* 	*AQ111 *Q)))* * * * * * * * * * * * * * *99X&&; 	XT[--//'BBB affhh               It':':8'D'Dy'PQQ DKKK [!!## 	XDK,,....%)[%5%5%7%7%?%?t228<<yH&J &J""" &*%8%8%B%BT[EUEUEWEW%W"""	X 	Xs$   (B66B:	=B:	D>>EEc                    t          |t          j                  s|j        j        dk    r|                                }i }t          j        d|          D ]j}t          j        d| j	        j
        z  |          rFt          j        d| j	        j
        z  |          d         }t          j        d|          d         ||<   k|S )zReads 'content' and extracts options encoded in HTML meta tags

        :param content: str or file-like object - contains HTML to parse

        returns:
          dict: {config option: value}
        StreamReaderWriterz<meta [^>]*>zname=["']%szname=["']%s([^"']*)r   zcontent=["']([^"']*))r2   ioIOBase	__class__r   rn   refindallsearchr   meta_tag_prefix)r   contentfoundxnames        r   r%   zPDFKit._find_options_in_meta  s     w	** 	%$-1EEEllnnGNG44 	I 	IAy$*<*LLaPP Iz"9"&"4"D#EFGI IIJL j)A1EEaHdr   )NNNNNFFr   )r   r   r   r   	Exceptionr   r   r9   rE   rG   staticmethodrT   r{   r1   r~   r   r>   r%   r   r   r   r   r      s"       
 
    i    JNJO   6   . . . .`) ) ) ) m m \m0?7 ?7 ?7 ?7BV V V2  0 0 0X X X8    r   r   )r   rd   rb   collectionsr   r   r   r   r   r   rr   r@   r   	NameErrorstrobjectr   r   r   r   <module>r      s    				     



 # # # # # #       ( ( ( ( ( ( 				 JGG   JGGG
] ] ] ] ]V ] ] ] ] ]s   - 	99