
    NgB`              0       f   d Z ddlZddlZddlZddlZddlZddlZddlmZmZm	Z	 ddl
mZmZmZmZmZmZ ddlmZ ddlmZ ddlmZmZmZ ddlmZmZmZmZ dd	lmZm Z m!Z!m"Z" d
dgZ#dgZ$ddddddddddddd e            dddddddfdee%ef         de&dee%ef         de&de&de%dede&de%de%de'de'de'de'ded ee%ef         d!e'd"eee&f         d#e'd$e'd%e&d&e'd'eej                 f.d(Z(ddddddddddddd e            dddddddfd)e)de&dee%ef         de&de&de%dede&de%de%de'de'de'de'dee%ef         d ee%ef         d!e'd"eee&f         d#e'd$e'd%e&d&e'd'eej                 f.d*Z*d+ede%de&de&de%dede%de%de%de'de'de'd!e'd"ee&ee&e&f         f         d&e'd'ee%         f d,Z+d9de%d!e'd'ee%e%ee'f         fd-Z,ded'e%fd.Z-d:d/e%d e%d'e%fd0Z.	 d;d/e%d e%d%e&d'ee&e&f         fd1Z/	 	 	 	 	 	 	 d<de%de%de%d e%d2e'd%e&de&de&d'efd3Z0	 	 	 	 	 	 	 d<d4e)de%de%d e%d2e'd%e&de&de&d'efd5Z1	 d9de%de%d6e%d#e'd7e'd'eej                 fd8Z2dS )=zq
    pdf2image is a light wrapper for the poppler-utils tools that can convert your
    PDFs into Pillow images.
    N)PopenPIPETimeoutExpired)AnyUnionTupleListDictCallable)PurePath)Image)uuid_generatorcounter_generatorThreadSafeGenerator)parse_buffer_to_pgmparse_buffer_to_ppmparse_buffer_to_jpegparse_buffer_to_png)PDFInfoNotInstalledErrorPDFPageCountErrorPDFSyntaxErrorPDFPopplerTimeoutErrorpngtiffPages   ppm   Fpdf_pathdpioutput_folder
first_page	last_pagefmtjpegoptthread_countuserpwownerpwuse_cropboxstricttransparentsingle_fileoutput_filepoppler_path	grayscalesize
paths_onlyuse_pdftocairotimeouthide_annotationsreturnc                 *   |r|dk    rd}t          | t                    r|                                 } t          |t                    r|                                }t          |t                    r|                                }t          | ||	|          d         }t	          ||          \  }}}}|p|p
|o|t
          v }t          |rdnd|          \  }}|dk    r|dk    rd	}|dk    r|d
k    rd}t          |t          j                  s9t          |t                    s$|rt          |g          }d}nt          |          }|dk     rd}||dk     rd}|||k    r|}||k    rg S 	 d}||rt          j                    }d}||z
  dz   }||k    r|}||z  }|} g }!t          |          D ]m}"t          |          }#||z  t!          |dk              z   }$t#          dt%          |          | g|| | |$z   dz
  |||#||	|
|||||          }%|r&|rt'          d          t)          d|          g|%z   }%nt)          d|          g|%z   }%| |$z   } |t!          |dk              z  }t*          j                                        }&||dz   |&                    dd          z   |&d<   d	}'t3          j                    dk    r-t7          j                    }'|'xj        t6          j        z  c_        |!                    |#tA          |%|&tB          tB          |'          f           og }(|!D ]\  })}*	 |*"                    |          \  }+},nH# tF          $ r; |*$                                 |*"                                \  }-}.tK          d          w xY wd|,v r%|r#tM          |,'                    dd                    ||(tQ          ||)|||          z  }(|( ||+          z  }(	 |rtS          j*        |           n# |rtS          j*        |           w w xY w|(S )ab  Function wrapping pdftoppm and pdftocairo

    :param pdf_path: Path to the PDF that you want to convert
    :type pdf_path: Union[str, PurePath]
    :param dpi: Image quality in DPI (default 200), defaults to 200
    :type dpi: int, optional
    :param output_folder: Write the resulting images to a folder (instead of directly in memory), defaults to None
    :type output_folder: Union[str, PurePath], optional
    :param first_page: First page to process, defaults to None
    :type first_page: int, optional
    :param last_page: Last page to process before stopping, defaults to None
    :type last_page: int, optional
    :param fmt: Output image format, defaults to "ppm"
    :type fmt: str, optional
    :param jpegopt: jpeg options `quality`, `progressive`, and `optimize` (only for jpeg format), defaults to None
    :type jpegopt: Dict, optional
    :param thread_count: How many threads we are allowed to spawn for processing, defaults to 1
    :type thread_count: int, optional
    :param userpw: PDF's password, defaults to None
    :type userpw: str, optional
    :param ownerpw: PDF's owner password, defaults to None
    :type ownerpw: str, optional
    :param use_cropbox: Use cropbox instead of mediabox, defaults to False
    :type use_cropbox: bool, optional
    :param strict: When a Syntax Error is thrown, it will be raised as an Exception, defaults to False
    :type strict: bool, optional
    :param transparent: Output with a transparent background instead of a white one, defaults to False
    :type transparent: bool, optional
    :param single_file: Uses the -singlefile option from pdftoppm/pdftocairo, defaults to False
    :type single_file: bool, optional
    :param output_file: What is the output filename or generator, defaults to uuid_generator()
    :type output_file: Any, optional
    :param poppler_path: Path to look for poppler binaries, defaults to None
    :type poppler_path: Union[str, PurePath], optional
    :param grayscale: Output grayscale image(s), defaults to False
    :type grayscale: bool, optional
    :param size: Size of the resulting image(s), uses the Pillow (width, height) standard, defaults to None
    :type size: Union[Tuple, int], optional
    :param paths_only: Don't load image(s), return paths instead (requires output_folder), defaults to False
    :type paths_only: bool, optional
    :param use_pdftocairo: Use pdftocairo instead of pdftoppm, may help performance, defaults to False
    :type use_pdftocairo: bool, optional
    :param timeout: Raise PDFPopplerTimeoutError after the given time, defaults to None
    :type timeout: int, optional
    :param hide_annotations: Hide PDF annotations in the output, defaults to False
    :type hide_annotations: bool, optional
    :raises NotImplementedError: Raised when conflicting parameters are given (hide_annotations for pdftocairo)
    :raises PDFPopplerTimeoutError: Raised after the timeout for the image processing is exceeded
    :raises PDFSyntaxError: Raised if there is a syntax error in the PDF and strict=True
    :return: A list of Pillow images, one for each page between first_page and last_page
    :rtype: List[Image.Image]
    r   r   )r.   r   
pdftocairopdftoppmr   9   NS   Fr   Tz-rz4Hide annotations flag not implemented in pdftocairo.:LD_LIBRARY_PATH Windows)envstdoutstderrstartupinfor3   zRun poppler timeout.s   Syntax Errorutf8ignore)	in_memory)+
isinstancer   as_posixpdfinfo_from_path_parse_formatTRANSPARENT_FILE_TYPES_get_poppler_versiontypesGeneratorTyper   iterr   tempfilemkdtemprangenextint_build_commandstrNotImplementedError_get_command_pathosenvironcopygetplatformsystem
subprocessSTARTUPINFOdwFlagsSTARTF_USESHOWWINDOWappendr   r   communicater   killr   r   decode_load_from_output_foldershutilrmtree)/r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   
page_count
parsed_fmtfinal_extensionparse_buffer_funcuse_pdfcairo_formatuse_pdfcairopoppler_version_majorpoppler_version_minorauto_temp_dirremindercurrent_page	processes_thread_output_filethread_page_countargsr?   rB   imagesuidprocdataerroutserrss/                                                  O/var/www/html/ai-engine/env/lib/python3.11/site-packages/pdf2image/pdf2image.pyconvert_from_pathr   %   s   Z  #,, (H%% '$$&&-** 1%..00,)) /#,,.."&'  J
 KXYK KGJ!24G 	 	B	B@J*@@  4H$4*<4 4 400 !!&;r&A&A!!&;r&A&A  k5#677 9
(A A 9  	9}--KLL+K88KaZ!^^
I
22	I	a) \ $,..M M +a/
*$$%L,!	|$$ 7	 7	A!%k!2!2 !+l :SA=N=N N!s3xx*0014"  D$  L# -N   *,EEFM)*lCCDtK (*;;LHqL)))H*//##C' 3&1BB)G)GG %& K  I--(466##z'FF##&#d4[      " 	2 	2ICE ,,W,==	cc! E E E		!--//
d,-CDDDE
 #%%&%$SZZ%A%ABBB(2!#+    ++D111)	2,  	)M-(((  	)M-((((	) Ms'   /F;O7 +MO7 AN

AO7 7Ppdf_filec                 p   t          j                    \  }}	 t          |d          5 }|                    |            |                                 t          |j        fi d|d|d|d|d|d|d|d	|d
|	d|
d|d|d|d|d|d|d|d|d|d|d|cddd           t          j        |           t          j	        |           S # 1 swxY w Y   	 t          j        |           t          j	        |           dS # t          j        |           t          j	        |           w xY w)aW  Function wrapping pdftoppm and pdftocairo.

    :param pdf_bytes: Bytes of the PDF that you want to convert
    :type pdf_bytes: bytes
    :param dpi: Image quality in DPI (default 200), defaults to 200
    :type dpi: int, optional
    :param output_folder: Write the resulting images to a folder (instead of directly in memory), defaults to None
    :type output_folder: Union[str, PurePath], optional
    :param first_page: First page to process, defaults to None
    :type first_page: int, optional
    :param last_page: Last page to process before stopping, defaults to None
    :type last_page: int, optional
    :param fmt: Output image format, defaults to "ppm"
    :type fmt: str, optional
    :param jpegopt: jpeg options `quality`, `progressive`, and `optimize` (only for jpeg format), defaults to None
    :type jpegopt: Dict, optional
    :param thread_count: How many threads we are allowed to spawn for processing, defaults to 1
    :type thread_count: int, optional
    :param userpw: PDF's password, defaults to None
    :type userpw: str, optional
    :param ownerpw: PDF's owner password, defaults to None
    :type ownerpw: str, optional
    :param use_cropbox: Use cropbox instead of mediabox, defaults to False
    :type use_cropbox: bool, optional
    :param strict: When a Syntax Error is thrown, it will be raised as an Exception, defaults to False
    :type strict: bool, optional
    :param transparent: Output with a transparent background instead of a white one, defaults to False
    :type transparent: bool, optional
    :param single_file: Uses the -singlefile option from pdftoppm/pdftocairo, defaults to False
    :type single_file: bool, optional
    :param output_file: What is the output filename or generator, defaults to uuid_generator()
    :type output_file: Any, optional
    :param poppler_path: Path to look for poppler binaries, defaults to None
    :type poppler_path: Union[str, PurePath], optional
    :param grayscale: Output grayscale image(s), defaults to False
    :type grayscale: bool, optional
    :param size: Size of the resulting image(s), uses the Pillow (width, height) standard, defaults to None
    :type size: Union[Tuple, int], optional
    :param paths_only: Don't load image(s), return paths instead (requires output_folder), defaults to False
    :type paths_only: bool, optional
    :param use_pdftocairo: Use pdftocairo instead of pdftoppm, may help performance, defaults to False
    :type use_pdftocairo: bool, optional
    :param timeout: Raise PDFPopplerTimeoutError after the given time, defaults to None
    :type timeout: int, optional
    :param hide_annotations: Hide PDF annotations in the output, defaults to False
    :type hide_annotations: bool, optional
    :raises NotImplementedError: Raised when conflicting parameters are given (hide_annotations for pdftocairo)
    :raises PDFPopplerTimeoutError: Raised after the timeout for the image processing is exceeded
    :raises PDFSyntaxError: Raised if there is a syntax error in the PDF and strict=True
    :return: A list of Pillow images, one for each page between first_page and last_page
    :rtype: List[Image.Image]
    wbr    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   N)
rP   mkstempopenwriteflushr   namerY   closeremove)r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   fhtemp_filenamefs                            r   convert_from_bytesr     s"   Z !(**B!-&& 	!GGHGGIII$  C ,m &:	
 $) C   *\ v   (K v (K (K (K  *\!" $)#$ T%& &:'(  .~)*  +, "2!1-	 	 	 	 	 	 	8 	
	-    ;	 	 	 	 	 	 	 	 	8 	
	-      	
	-    s/   D A8C D CD CD *D5ry   c           	      x   |	r|                      d           |r|                      d           |
r|t          v r|                      d           |$|                     dt          |          g           |$|                     dt          |          g           |dvr|                      d|z              |dv r&|r$|                     d	t	          |          g           |r|                      d
           |3|                      t
          j                            ||                     ||                     d|g           ||                     d|g           |r|                      d           |nt          |t                    rt          |          dk    r|d         8|                     dt          t          |d                             g           n$|                     dt          d          g           |d         8|                     dt          t          |d                             g           n|                     dt          d          g           nt          |t                    rKt          |          dk    r8|                     dt          t          |d                             g           not          |t                    st          |t                    r2|                     dt          t          |                    g           nt          d| d          | S )Nz-cropboxz-hide-annotationsz-transp-f-l)pgmr   -jpegjpgz-jpegoptz-singlefile-upw-opwz-gray   r   z-scale-to-xr   z-scale-to-yz	-scale-tozSize z is not a tuple or an integer)rc   rK   extendrV   _parse_jpegoptrY   pathjoinrG   tuplelenrT   float
ValueError)ry   r!   r"   r#   r$   r%   r-   r'   r(   r)   r+   r,   r/   r0   r4   s                  r   rU   rU     s   "   J )'((( s444IT3z??+,,,T3y>>*+++
.  C#I
o'Z!8!89::: #M""" BGLL<<===VV$%%%VW%&&& G|	D%	 	  FSYY!^^7KKCQLL(9(9:;;;;KKB01117KKCQLL(9(9:;;;;KKB01111	D%	 	  FSYY!^^[#c$q'll"3"345555	D#		 F*T5"9"9 F[#c$ii..12222DDDDEEEK    c                     |                                  } | d         dk    r
| dd          } | dv rddt          dfS | dk    rddt          dfS | d	v rd
S | dk    r|rddt          dfS ddt          dfS )Nr   .r   r   r   r   Fr   )tifr   )r   r   NTr   r   )lowerr   r   r   r   )r$   r/   s     r   rJ   rJ     s    
))++C
1v}}!""g
ou2E99
e||e0%77
o((
e||	|e0%77%,e33r   c                     g }|                                  D ]:\  }}|du rd}|du rd}|                    d                    ||                     ;d                    |          S )NTyFnz{}={},)itemsrc   formatr   )r%   partskvs       r   r   r     sq    E + +199A::AW^^Aq))****88E??r   commandc                     t          j                    dk    r| dz   } | t          j                            ||           } | S )Nr>   z.exe)r]   r^   rY   r   r   )r   r.   s     r   rX   rX     s?    I%%F"',,|W55Nr   c                    t          | |          dg} t          j                                        }||dz   |                    dd          z   |d<   t          | |t          t                    }	 |                    |          \  }}nH# t          $ r; |	                                 |                                \  }}t          d          w xY w	 |                    dd	                              d
          d                             d          d                             d          }	t          |	d                   t          |	d                   fS #  Y dS xY w)Nz-vr;   r<   r=   r?   r@   rA   rC   Run poppler poppler timeout.rD   rE   
r    r   r   r   )r      )rX   rY   rZ   r[   r\   r   r   rd   r   re   r   rf   splitrT   )
r   r.   r3   r?   r|   r}   r~   r   r   versions
             r   rL   rL     sX    !,77>G
*//

C!-!3cgg>OQS6T6T!Tc$t<<<DE$$W$55	cc E E E		%%''
d$%CDDDE
**VX..44T::1=CCCHHLRRSVWW71:GAJ//uus   0B
 
ACBE Erawdatesc                    	 t          d|          | g}||                    d|g           ||                    d|g           |r|                    dg           |r$|                    dt          |          g           |r$|                    dt          |          g           t          j                                        }	||dz   |	                    d	d
          z   |	d	<   t          ||	t          t                    }
	 |
	                    |          \  }}nH# t          $ r; |
                                 |
	                                \  }}t          d          w xY wi }|                    dd                              d          D ]}|                    d          }|d         d                    |dd                   }}|d
k    rA|t           v r!t#          |                                          n|                                ||<   d|vrt&          |S # t(          $ r t+          d          t&          $ r' t-          d|                    dd                     w xY w)a  Function wrapping poppler's pdfinfo utility and returns the result as a dictionary.

    :param pdf_path: Path to the PDF that you want to convert
    :type pdf_path: str
    :param userpw: PDF's password, defaults to None
    :type userpw: str, optional
    :param ownerpw: PDF's owner password, defaults to None
    :type ownerpw: str, optional
    :param poppler_path: Path to look for poppler binaries, defaults to None
    :type poppler_path: Union[str, PurePath], optional
    :param rawdates: Return the undecoded data strings, defaults to False
    :type rawdates: bool, optional
    :param timeout: Raise PDFPopplerTimeoutError after the given time, defaults to None
    :type timeout: int, optional
    :param first_page: First page to process, defaults to None
    :type first_page: int, optional
    :param last_page: Last page to process before stopping, defaults to None
    :type last_page: int, optional
    :raises PDFPopplerTimeoutError: Raised after the timeout for the image processing is exceeded
    :raises PDFInfoNotInstalledError: Raised if pdfinfo is not installed
    :raises PDFPageCountError: Raised if the output could not be parsed
    :return: Dictionary containing various information on the PDF
    :rtype: Dict
    pdfinfoNr   r   z	-rawdatesr   r   r;   r<   r=   r   rC   r   rD   rE   r   r   r   r   z;Unable to get page count. Is poppler installed and in PATH?zUnable to get page count.
)rX   r   rV   rY   rZ   r[   r\   r   r   rd   r   re   r   rf   r   r   PDFINFO_CONVERT_TO_INTrT   stripr   OSErrorr   r   )r   r'   r(   r.   r   r3   r"   r#   r   r?   r|   outr~   r   r   dfieldsfkeyvalues                       r   rI   rI     s   D6
$Y==xHNNFF+,,,NNFG,--- 	*NNK=))) 	4NND#j//2333 	3NND#i..1222 joo#%1C%7#''BSUW:X:X%XC!"W#d4@@@	I'''88HC 	I 	I 	IIIKKK))++JD$()GHHH	I
 ZZ1177== 	 	ES!!BAABB 0 0Cbyy 444 &&& # ! 
 
 
&I
 
 	
  
 
 
H#**VX*F*FHH
 
 	

s'   DH% D!  H% !AE&&B>H% %A	I.	pdf_bytesc           
         t          j                    \  }}		 t          |	d          5 }
|
                    |            |
                                 ddd           n# 1 swxY w Y   t          |	|||||||          t          j        |           t          j        |	           S # t          j        |           t          j        |	           w xY w)a  Function wrapping poppler's pdfinfo utility and returns the result as a dictionary.

    :param pdf_bytes: Bytes of the PDF that you want to convert
    :type pdf_bytes: bytes
    :param userpw: PDF's password, defaults to None
    :type userpw: str, optional
    :param ownerpw: PDF's owner password, defaults to None
    :type ownerpw: str, optional
    :param poppler_path: Path to look for poppler binaries, defaults to None
    :type poppler_path: Union[str, PurePath], optional
    :param rawdates: Return the undecoded data strings, defaults to False
    :type rawdates: bool, optional
    :param timeout: Raise PDFPopplerTimeoutError after the given time, defaults to None
    :type timeout: int, optional
    :param first_page: First page to process, defaults to None
    :type first_page: int, optional
    :param last_page: Last page to process before stopping, defaults to None
    :type last_page: int, optional
    :return: Dictionary containing various information on the PDF
    :rtype: Dict
    r   N)r'   r(   r.   r   r3   r"   r#   )	rP   r   r   r   r   rI   rY   r   r   )r   r'   r(   r.   r   r3   r"   r#   r   r   r   s              r   pdfinfo_from_bytesr   h  s   > !(**B!-&& 	!GGIGGIII	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 !%!	
 	
 	
 	
	-     	
	-    s.   B( *AB( A""B( %A"&B( (*CextrF   c           	         g }t          t          j        |                     D ]}|                    |          r|                    d          d         |k    r|r4|                    t          j                            | |                     l|                    t          j	        t          j                            | |                               |r|d         
                                 |S )Nr   r   )sortedrY   listdir
startswithr   rc   r   r   r   r   load)r!   r-   r   r1   rF   rz   r   s          r   rg   rg     s     FBJ}--.. & &<<$$ 	&b)9S)@)@ &bgll=!<<====ejmQ)G)GHHIII &2JOO%%%Mr   )F)N)NN)NNNFNNN)3__doc__rY   r]   rP   rM   rh   r_   r   r   r   typingr   r   r   r	   r
   r   pathlibr   PILr   pdf2image.generatorsr   r   r   pdf2image.parsersr   r   r   r   pdf2image.exceptionsr   r   r   r   rK   r   rV   rT   boolr   bytesr   rU   rJ   r   rX   rL   rI   r   rg    r   r   <module>r      s   
 
			         2 2 2 2 2 2 2 2 2 2 : : : : : : : : : : : : : : : :             W W W W W W W W W W                         ! 
 *.%~'')-" "-m mCM"m	m h'm 	m
 m 
m m m m m m m m m m  X&!m" #m$ s

%m& 'm( )m* +m, -m. 
%+/m m m md *.(6(8(8)-" "-l! l!l!	l! h'l! 	l!
 l! 
l! l! l! l! l! l! l! l! l! sH}%l!  X&!l!" #l!$ s

%l!& 'l!( )l!* +l!, -l!. 
%+/l! l! l! l!^G
GG G 	G
 
G G G G G G G G G U38_$
%G G  
#Y!G G G GT4 4s 4t 4c3RV>V8W 4 4 4 4 D S     s #      <@  #58
38_   8 X
 X
X
X
 X
 	X

 X
 X
 X
 X
 
X
 X
 X
 X
z 0! 0!0!0! 0! 	0!
 0! 0! 0! 0! 
0! 0! 0! 0!p   
 	
  
%+     r   