o
    0׾gB`                  0   @   s  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(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+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.d/Z+dCde%d!e'd'ee%e%ee'f fd0d1Z,ded'e%fd2d3Z-dDd4e%d e%d'e%fd5d6Z.	dEd4e%d e%d%e&d'ee&e&f fd7d8Z/							dFde%de%de%d e%d9e'd%e&de&de&d'efd:d;Z0							dFd<e)de%de%d e%d9e'd%e&de&de&d'efd=d>Z1	dCde%de%d?e%d#e'd@e'd'eej fdAdBZ2dS )Gzq
    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           /      C   sf  |r|dkrd}t | tr|  } t |tr| }t |tr#| }t| ||	|dd }t||\}}}}|p?|p?|o?|tv }t|rEdnd|d\}}|dkrV|dkrVd	}|dkr`|d
kr`d}t |tjsyt |t	sy|rut
|g}d}nt|}|dk rd}|d	u s|dk rd}|d	u s||kr|}||krg S zd}|d	u r|rt }d}|| d }||kr|}|| }|} g }!t|D ]}"t|}#|| t|dk }$tdt|| g|| | |$ d |||#||	|
|||||}%|r|rtdtd|g|% }%ntd|g|% }%| |$ } |t|dk8 }tj }&|d	ur'|d |&dd |&d< d	}'t dkr<t }'|' jtjO  _|!|#t |%|&t!t!|'df qg }(|!D ]N\})}*z
|*j"|d\}+},W n t#ys   |*$  |*" \}-}.t%dw d|,v r|rt&|,'dd|d	ur|(t(||)|||d7 }(qO|(||+7 }(qOW |rt)*| |(S |rt)*| w w )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r2   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.   r/   r0   r1   r2   r3   
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=   r@   imagesuidprocdataerroutserrs r   ]/var/www/html/backend_erp/backend_erp_env/lib/python3.10/site-packages/pdf2image/pdf2image.pyconvert_from_path%   s   M









	

r   pdf_filec              	   C   s  t  \}}z|t|db}||  |  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|W  d   W t| t	| S 1 sqw   Y  W t| t	| dS t| t	| 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.   r/   r0   r1   r2   r3   N)
rN   mkstempopenwriteflushr   namerW   closeremove)r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   fhtemp_filenamefr   r   r   convert_from_bytes  st   M
	



r   rw   c                 C   s  |	r|  d |r|  d |
r|tv r|  d |d ur&| dt|g |d ur3| dt|g |dvr>|  d|  |dv rM|rM| d	t|g |rT|  d
 |d urb|  tj|| |d urm| d|g |d urx| d|g |r|  d |d u r	 | S t|t	rt
|dkr|d d ur| dtt|d g n	| dtdg |d d ur| dtt|d g | S | dtdg | S t|t	rt
|dkr| dtt|d g | S t|tst|tr| dtt|g | S td| d)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)ra   rI   extendrT   _parse_jpegoptrW   pathjoinrE   tuplelenrR   float
ValueError)rw   r    r!   r"   r#   r$   r,   r&   r'   r(   r*   r+   r.   r/   r3   r   r   r   rS     sV   





rS   c                 C   sx   |   } | 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r6|r6d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.   r   r   r   rH     s   rH   c                 C   sJ   g }|   D ]\}}|du rd}|du rd}|d|| qd|S )NTyFnz{}={},)itemsra   formatr   )r$   partskvr   r   r   r     s   
r   commandc                 C   s.   t  dkr
| d } |d urtj|| } | S )Nr<   z.exe)r[   r\   rW   r   r   )r   r-   r   r   r   rV     s
   rV   c           
      C   s   t | |dg} tj }|d ur|d |dd |d< t| |ttd}z
|j|d\}}W n tyC   |	  | \}}t
dw z |dd	d
d dd d}	t|	d t|	d fW S    Y dS )Nz-vr9   r:   r;   r=   r>   r?   rA   Run poppler poppler timeout.rB   rC   
r    r   r   r   )r      )rV   rW   rX   rY   rZ   r   r   rb   r   rc   r   rd   splitrR   )
r   r-   r2   r=   rz   r{   r|   r}   r~   versionr   r   r   rJ     s"   
&rJ   rawdatesc              	   C   s  zt d|| g}|dur|d|g |dur|d|g |r&|dg |r1|dt|g |r<|dt|g tj }	|durQ|d |	d	d
 |	d	< t||	ttd}
z
|
j	|d\}}W n t
yx   |
  |
	 \}}tdw i }|dddD ])}|d}|d d|dd }}|d
kr|tv rt| n| ||< qd|vrt|W S  ty   td ty   td|dd 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   r9   r:   r;   r   rA   r   rB   rC   r   r   r   r   z;Unable to get page count. Is poppler installed and in PATH?zUnable to get page count.
)rV   r   rT   rW   rX   rY   rZ   r   r   rb   r   rc   r   rd   r   r   PDFINFO_CONVERT_TO_INTrR   stripr   OSErrorr   r   )r   r&   r'   r-   r   r2   r!   r"   r   r=   rz   outr|   r}   r~   dfieldsfkeyvaluer   r   r   rG     sZ   "

rG   	pdf_bytesc              
   C   s   t  \}}	z5t|	d}
|
|  |
  W d   n1 s w   Y  t|	|||||||dW t| t|	 S t| t|	 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   r2   r!   r"   )	rN   r   r   r   r   rG   rW   r   r   )r   r&   r'   r-   r   r2   r!   r"   r   r   r   r   r   r   pdfinfo_from_bytesh  s(   



r   extrD   c              	   C   s|   g }t t| D ]2}||r;|dd |kr;|r&|tj| | q	|t	tj| | |r;|d 
  q	|S )Nr   r   )sortedrW   listdir
startswithr   ra   r   r   r   r   load)r    r,   r   r0   rD   rx   r   r   r   r   re     s   re   )F)N)NN)NNNFNNN)3__doc__rW   r[   rN   rK   rf   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   rI   r   rT   rR   boolr   bytesr   rS   rH   r   rV   rJ   rG   r   re   r   r   r   r   <module>   s    

	



 s
	




o	

$J

	
]	
8