
    0i'                        d Z ddlm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	  G d de
      Z G d de
      Zdd	Zdd
Zdd	 	 	 	 	 ddZy)u  DOCX → PDF conversion — LibreOffice headless subprocess.

Reference-scripts'te PQC Gateway için aynı yaklaşım kullanıldı.
`soffice --headless --convert-to pdf` stabil, ücretsiz, format-preserving.

Önemli:
- LibreOffice başka bir instance çalışıyorsa `--headless` hâlâ çakışabilir;
  `-env:UserInstallation` ile izole profil veririz.
- Concurrent conversion için her çağrı kendi tmpdir'ini + user profile'ını
  alır — thread-safe.
- DOCX tüm stil/XML (satır numaralama, PAGE field, margins) PDF'e taşınır.
    )annotationsN)Pathc                      e Zd ZdZy)PdfConversionErroru<   soffice exit code 0 değil veya çıktı dosyası üretmedi.N__name__
__module____qualname____doc__     >/var/www/ai.patentyazar.com/app/services/docx/pdf_converter.pyr   r      s    Fr   r   c                      e Zd ZdZy)LibreOfficeNotFoundErroru   soffice binary bulunamadı.Nr   r   r   r   r   r      s    %r   r   c                    t        j                  d      xs t        j                  d      } | r| S g d}|D ]J  }t        j                  j	                  |      s#t        j
                  |t        j                        sH|c S  t        d      )uE   Platform-agnostic soffice lookup: PATH → Homebrew → macOS bundle.sofficelibreoffice)z4/Applications/LibreOffice.app/Contents/MacOS/sofficez/opt/homebrew/bin/sofficez/usr/local/bin/sofficez/usr/bin/libreofficez/usr/bin/sofficeuo   LibreOffice (soffice) bulunamadı. macOS: `brew install --cask libreoffice`; Ubuntu: `apt install libreoffice`.)shutilwhichospathisfileaccessX_OKr   )	candidateknown_pathsps      r   _find_sofficer       sy    Y'F6<<+FIK  77>>!1bgg!6H #	- r   c                 8    	 t                y# t        $ r Y yw xY w)NTF)r   r   r   r   r   soffice_availabler    6   s!    # s   
 	<   )timeout_secondsc                 K   t               }t        j                  d      5 }t        |      }|dz  }|j	                          |dt        j                         j                  dd  dz  }|j                  |        |d| d	d
ddt        |      t        |      g}	 t        j                  |t        j                  j                  t        j                  j                  d d{   }t        j                  |j                         |       d{   \  }	}
|j$                  dk7  r5|
xs dj'                  dd      }t#        d|j$                   d|dd        |j)                  d      }|j+                         s)t#        d| d|	xs dj'                  dd      dd        |j-                         cddd       S 7 7 # t         $ r}t#        d| d      |d}~ww xY w# 1 sw Y   yxY ww)uF   DOCX bytes → PDF bytes. Concurrent-safe (her çağrı izole tmpdir).	pdf_conv_)prefix
lo_profileinput_N   z.docxz-env:UserInstallation=file://z
--headlessz--convert-topdfz--outdir)stdoutstderr)timeoutzsoffice u   s içinde tamamlanamadır   r   zutf-8replace)errorszsoffice exit z: i  z.pdfu&   PDF üretildi ama beklenen yolda yok: z
. stdout:    )r   tempfileTemporaryDirectoryr   mkdiruuiduuid4hexwrite_bytesstrasynciocreate_subprocess_exec
subprocessPIPEwait_forcommunicateTimeoutErrorr   
returncodedecodewith_suffixexists
read_bytes)
docx_bytesr"   r   tmpdirtmp_pathuser_profile	docx_pathcmdprocr*   r+   excerrpdf_paths                 r   convert_docx_to_pdfrN   >   s
     oG		$	$K	8 )%F<,.

(8(8!(<'=UCC	j) +L>:M	N	
	d 77))..)).. D
 $+#3#3D4D4D4FP_#``NFF ??a=S(((CC$}T__4ERDS	{%STT ((0 $8
 C"Mc11')1LTcRSU 
 ""$S)% )%(
 a 	d$x/@@X%YZ`cc	d5)% )%sg   !G3A9G'A	G&G',GGGBG'8
G3GG	G$GG$$G''G0,G3)returnr7   )rO   bool)rD   bytesr"   intrO   rQ   )r   
__future__r   r8   r   r   r0   r3   pathlibr   RuntimeErrorr   r   r   r    rN   r   r   r   <module>rV      sk    #  	    G G&| &, 1%1% 1% 	1%r   