+
    4i'                        R t ^ RIHt ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIH	t	  ! R R]
4      t ! R R]
4      tR R	 ltR
 R ltR^</R R lltR# )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                      ] tR t^tRtRtR# )PdfConversionErroru<   soffice exit code 0 değil veya çıktı dosyası üretmedi. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       a/Users/zincirx/Documents/projelerim/patentyazar/backend-python/app/services/docx/pdf_converter.pyr   r      s    Fr   r   c                      ] tR t^tRtRtR# )LibreOfficeNotFoundErroru   soffice binary bulunamadı.r   Nr   r   r   r   r   r      s    %r   r   c                   V ^8  d   QhRR/# )   returnstrr   )formats   "r   __annotate__r       s      s r   c                 Z   \         P                  ! R4      ;'       g    \         P                  ! R4      p V '       d   V # . ROpV FY  p\        P                  P	                  V4      '       g   K*  \        P
                  ! V\        P                  4      '       g   KW  Vu # 	  \        R4      h)uE   Platform-agnostic soffice lookup: PATH → Homebrew → macOS bundle.sofficelibreofficeuo   LibreOffice (soffice) bulunamadı. macOS: `brew install --cask libreoffice`; Ubuntu: `apt install libreoffice`.)z4/Applications/LibreOffice.app/Contents/MacOS/sofficez/opt/homebrew/bin/sofficez/usr/local/bin/sofficez/usr/bin/libreofficez/usr/bin/soffice)shutilwhichospathisfileaccessX_OKr   )	candidateknown_pathsps      r   _find_sofficer%       s}    Y'FF6<<+FIK 77>>!1bgg!6!6H  #	- r   c                   V ^8  d   QhRR/# )r   r   boolr   )r   s   "r   r   r   6   s      4 r   c                 @     \        4        R #   \         d     R# i ; i)TF)r%   r   r   r   r   soffice_availabler)   6   s!    # s   
 timeout_secondsc               $    V ^8  d   QhRRRRRR/# )r   
docx_bytesbytesr*   intr   r   )r   s   "r   r   r   >   s(     1% 1%1% 1% 	1%r   c               h  "   \        4       p\        P                  ! RR7      ;_uu_ 4       p\        V4      pVR,          pVP	                  4        VR\
        P                  ! 4       P                  R,           R2,          pVP                  V 4       VRV 2RR	R
R\        V4      \        V4      .p \        P                  ! VR\        P                  P                  R\        P                  P                  / G Rj  xL
 p\        P                  ! VP                  4       VR7      G Rj  xL
 w  rTP$                  ^ 8w  d@   T
;'       g    RP'                  RRR7      p\#        RTP$                   RTR,           24      hTP)                  R4      pTP+                  4       '       g4   \#        RT RT	;'       g    RP'                  RRR7      R,           24      hTP-                  4       uuRRR4       #  L L  \          d   p\#        RT R24      ThRp?ii ; i  + '       g   i     R# ; i5i)uF   DOCX bytes → PDF bytes. Concurrent-safe (her çağrı izole tmpdir).	pdf_conv_)prefix
lo_profileinput_:N   Nz.docxz-env:UserInstallation=file://z
--headlessz--convert-topdfz--outdirstdoutstderrN)timeoutzsoffice u   s içinde tamamlanamadır   zutf-8replace)errorszsoffice exit z: :Ni  Nz.pdfu&   PDF üretildi ama beklenen yolda yok: z
. stdout: :N   N)r%   tempfileTemporaryDirectoryr   mkdiruuiduuid4hexwrite_bytesr   asynciocreate_subprocess_exec
subprocessPIPEwait_forcommunicateTimeoutErrorr   
returncodedecodewith_suffixexists
read_bytes)r,   r*   r   tmpdirtmp_pathuser_profile	docx_pathcmdprocr6   r7   excerrpdf_paths   &$            r   convert_docx_to_pdfrX   >   s     oG		$	$K	8	8F<,.

(8(8(<'=UCC	j) +L>:M	N	
	d 77)).. )).. D
 $+#3#3D4D4D4FP_#``NF ??a==S(((CC$}T__4ERD	{%STT ((0  $8
 C"MMc11')1LTRSU 
 ""$S 
9	8(
 a 	d$x/@@X%YZ`cc	d5 
9	8	8sy   *H2BH3AG;>G7?-G;,G9-G;3HAH*H<0H,H27G;9G;;HHHHH/	)	H2)r   
__future__r   rC   r   r   r<   r?   pathlibr   RuntimeErrorr   r   r%   r)   rX   r   r   r   <module>r\      sY    #  	    G G&| &,1% 1% 1%r   