o
    ¦<:eò  ã                   @   s²   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m	Z	 ddl
T dd	lmZ dd
lmZ ddl
mZmZmZmZmZ G dd„ de	ƒZedkrWeddƒZee ¡ ƒ dS dS )é    N)ÚPath)Ú	PdfReader)Ú	Converteré   )ÚBaseé   )Ú*)ÚWrongConverterError)ÚFontDetector)Úrun_shell_commandÚextract_text_dataÚcreate_txt_fileÚextract_figure_from_imgÚconvert_pdf_to_docxc                       sL   e Zd Zd‡ fdd„Zdd„ Zdd„ Zd	d
„ Zdedef‡ fdd„Z‡  Z	S )ÚPdfConverterÚreturnNc                    s   t ƒ  ¡  d S )N)ÚsuperÚ__init__)Úself©Ú	__class__© úg/var/www/html/file-convertor/convertpro/convertor/docconvertor/fileconverter/converter/pdf_converter.pyr      s   zPdfConverter.__init__c                 C   s.   t | jƒ t| jƒ}|jd  ¡ dkrdS dS )Nr   Ú FT)ÚprintÚinput_file_pathr   ÚpagesÚextract_text)r   Ú
pdf_readerr   r   r   Ú_check_if_scanned_pdf   s
   

z"PdfConverter._check_if_scanned_pdfc                 C   s’   | j }|dkrt| jƒ}t|| j| jƒS |dv r t| j| j|ƒS || jv rG| dd¡}d|› d| j› dt| jƒj	› d�}t
d	|› �ƒ t|ƒS d S )
Nz.txt)z.docxz.docÚ.r   z?soffice --infilter="writer_pdf_import" --headless --convert-to z "z" --outdir "ú"zShell Command: )Úoutput_file_formatr   r   r   Úinput_file_namer   Ú_supported_formatsÚreplacer   Úparentr   r   )r   ÚformatÚ	text_dataÚshell_commandr   r   r   Ú_convert    s   

ÿ
"üzPdfConverter._convertc           
      C   sœ   t ƒ }tjdd��)}t| j|ƒ}t|ƒ\}}t||ƒ}t|ƒ}t|||ƒ}t	|| jƒ}W d   ƒ n1 s5w   Y  || _|  
¡ \}}	td|› �ƒ ||	fS )Nr    )ÚdirzOCR File Location: )r
   ÚtempfileÚTemporaryDirectoryÚextract_images_from_pdfr   r   Úcreate_hocr_fileÚcreate_background_imagesÚcombine_text_imageÚmerge_pdf_filesr*   r   )
r   Úfont_detectorÚtmp_dirÚimg_locationsÚfigure_locationÚhocr_file_locationsÚbackground_img_locationsÚpdf_file_locationsÚfile_locationÚstatusr   r   r   Ú_run_ocr/   s   
ÿø	zPdfConverter._run_ocrr   r"   c                    sL   t ƒ  ||¡ | jdkrtd| jƒ‚| j| jkrdS |  ¡ r"|  ¡ S |  ¡ S )Nú.pdfr   )z3Output file format is the same as input file formatF)r   Úinitialize_detailsÚinput_file_formatr	   r"   r   r<   r*   )r   r   r"   r   r   r   Úconvert?   s   
zPdfConverter.convert)r   N)
Ú__name__Ú
__module__Ú__qualname__r   r   r*   r<   Ústrr@   Ú__classcell__r   r   r   r   r      s    r   Ú__main__zFinal Report.docxr=   )r,   ÚloggingÚpathlibr   ÚPyPDF2r   Úpdf2docxr   Úbaser   Úutils.document_converterÚutils.errorsr	   Úutils.font_detectionr
   r   r   r   r   r   r   rA   Úpr   r@   r   r   r   r   Ú<module>   s    6
þ