a
    ¡˜ëjŠ  ã                   @   s>  d dl Z d dlZd dlmZmZmZmZ d dlmZ d dl	m
Z
mZmZmZmZ d dlmZmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ G dd„ deƒZdee dœdd„Zeee dœdd„Zeee dœdd„Zee e e e f e ee e e e f dœdd„Z!eddgƒdeee" ee dœdd„ƒZ#dS )é    N)ÚBinaryIOÚListÚOptionalÚTuple)ÚPDFPageAggregator)ÚLAParamsÚLTContainerÚLTImageÚLTItemÚ
LTTextLine)ÚPDFPageInterpreterÚPDFResourceManager)ÚPDFPage)ÚPSSyntaxError)Ú	BaseModel)Úlogger)Úrequires_dependenciesc                   @   sN   e Zd ZU dZee ed< dZee ed< dZee ed< dZ	ee ed< dS )ÚPDFMinerConfigNÚline_overlapÚword_marginÚline_marginÚchar_margin)
Ú__name__Ú
__module__Ú__qualname__r   r   ÚfloatÚ__annotations__r   r   r   © r   r   ún/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/pdf_image/pdfminer_utils.pyr      s   
r   ©Úpdfminer_configc                 C   sF   t ƒ }| r| jdd�ni }tf i |¤Ž}t||d�}t||ƒ}||fS )NT)Zexclude_none)Úlaparams)r   Z
model_dumpr   r   r   )r    ZrsrcmgrZlaparams_kwargsr!   ÚdeviceÚinterpreterr   r   r   Úinit_pdfminer   s    
r$   )Úparent_objectÚreturnc                 C   s@   g }t | tƒr| | ¡ n"t | tƒr<| D ]}| t|ƒ¡ q(|S )zPRecursively extracts image objects from a given parent object in a PDF document.)Ú
isinstancer	   Úappendr   ÚextendÚextract_image_objects©r%   ZobjectsÚchildr   r   r   r*   #   s    

r*   c                 C   s@   g }t | tƒr| | ¡ n"t | tƒr<| D ]}| t|ƒ¡ q(|S )zORecursively extracts text objects from a given parent object in a PDF document.)r'   r   r(   r   r)   Úextract_text_objectsr+   r   r   r   r-   0   s    

r-   )ÚrectÚheightr&   c                 C   s(   | \}}}}|| }|| }||||fS )ab  
    Converts a PDF rectangle coordinates (x1, y1, x2, y2) to a bounding box in the specified
    coordinate system where the vertical axis is measured from the top of the page.

    Args:
        rect (Tuple[float, float, float, float]): A tuple representing a PDF rectangle
            coordinates (x1, y1, x2, y2).
        height (float): The height of the page in the specified coordinate system.

    Returns:
        Tuple[float, float, float, float]: A tuple representing the bounding box coordinates
        (x1, y1, x2, y2) with the y-coordinates adjusted to be measured from the top of the page.
    r   )r.   r/   Úx1Úy2Zx2Úy1r   r   r   Úrect_to_bbox=   s    r3   ÚpikepdfZpypdf)ÚfpÚpasswordr    c                 c   sà  ddl }ddlm} t|d�\}}t ¡ ��ž}tj |d¡}zæt	j
| |pJdd�}	t|	ƒD ]Æ\}
}z| |¡ | ¡ }W nœ t�y   t d¡ t d	|
d
 › d�¡ || |
d�}|j |¡�}| |¡ W d  ƒ n1 sÞ0    Y  tt	 
t|dƒ¡ƒ}| |¡ | ¡ }Y n0 ||fV  qXW n˜ t�yº   t d¡ t d¡ |j | ¡�}| |¡ W d  ƒ n1 �st0    Y  t	 
t|dƒ¡}	|	D ]"}| |¡ | ¡ }||fV  �q’Y n0 W d  ƒ n1 �sÒ0    Y  dS )zTOpen PDF pages using PDFMiner, handling and repairing invalid dictionary constructs.r   N)Úget_page_datar   Ztmp_fileÚ )r6   z2Detected invalid dictionary construct for PDFminerzRepairing the PDF page é   z ...)Zpage_numberÚrbzRepairing the PDF document ...)r4   Z,unstructured.partition.pdf_image.pypdf_utilsr7   r$   ÚtempfileÚTemporaryDirectoryÚosÚpathÚjoinr   Z	get_pagesÚ	enumerateZprocess_pageZ
get_resultr   r   ÚinfoZPdfÚopenÚsaveÚnext)r5   r6   r    r4   r7   r"   r#   Ztmp_dir_pathZtmp_file_pathZpagesÚiÚpageZpage_layoutZerror_page_dataZpdfr   r   r   Úopen_pdfminer_pages_generatorT   s>    

(


*
rG   )N)NN)$r=   r;   Útypingr   r   r   r   Zpdfminer.converterr   Zpdfminer.layoutr   r   r	   r
   r   Zpdfminer.pdfinterpr   r   Zpdfminer.pdfpager   Zpdfminer.psexceptionsr   Zpydanticr   Zunstructured.loggerr   Zunstructured.utilsr   r   r$   r*   r-   r   r3   ÚstrrG   r   r   r   r   Ú<module>   s.   ý
 ÿÿ