
    Ng42                        d dl Z d dlZd dlZd dlZd dlmZ d dlZd dlm	Z
 	 d dlZn# e$ r dZY nw xY wd dlmZ d dlmZ d dlmZ d dlmZmZmZmZ  ej        e          Zd Z eej         j!        ej         j"         ej#        ej         j"        d          e          Z$dZ% ed	d
e&          Z' edddd          Z(d Z) G d d          Z* G d de*          Z+ G d de*          Z,d Z-d Z.d Z/d Z0dS )    N)Path)	add_input	get_inputsetup_loggingBooleanOptionalActionc                     |t           j        k    rd}n6|t           j        k    rd}n#t          dt          j        |          d          t          j        j        | ||g|R i |S )NFTz5Cannot create foreign_simple bitmap with bitmap type .)	pdfium_rFPDFBitmap_BGRxFPDFBitmap_BGRARuntimeErrorpdfium_iBitmapTypeToStrpdfium	PdfBitmapnew_foreign_simple)widthheightformatargskwargs	use_alphas         Q/var/www/html/ai-engine/env/lib/python3.11/site-packages/pypdfium2/_cli/render.py_bitmap_wrapper_foreign_simpler      sy    )))			8+	+	+		vS[SklrSsvvvwww.ufiY$YYYRXYYY    T)force_packed)nativeforeignforeign_packedforeign_simple)	path_fillpath_stroke	text_filltext_strokeC   )metavarnargstype)   d   r      )r      r,   r,   r,   r,   r,   r,   )r-   r,   r   r,   c                 0   t          | d           |                     ddt          dd           |                     dd	           |                     d
ddt          j        d           |                     ddd d           |                     ddt
          d           |                     ddt          dd            | j        	 deddit           |                     dd d!"           |                     d#d$t
          d%d&'           |                     d(t          dd)*           |                     d+t          dd,*           |                     d-d.g d/t          j        d01           |                     d2d3d45           | 	                    d6d78          }|                    d9t                                          d:d;t          j        <           |                    d=d3d>5           |                    d?d@dA dB           |                    dCdDt          dEF           | 	                    dGdH8          }|                    dIdJt          t          j        dKL           |                    dMt          j                    t          dN           |                    dOdPdQt          j        dRS           |                    dTdUdVt          j        dWS           |                    dXt          j        dYZ           | 	                    d[d\8          }|                    d]d3d^5            |j        	 dfi t            |j        	 dgi t            |j        	 dhi t            |j        	 dii t           |                    dcd3dd5           d S )jNT)pagesz--outputz-ozDOutput directory where the serially numbered images shall be placed.)r)   requiredhelpz--prefixzDCustom prefix for the images. Defaults to the input filename's stem.)r2   z--formatz-fjpgzThe image format to use.)defaultr)   r2   z--engine
engine_clsc                 P    t           t          d|                                          S )N)pilz	numpy+cv2)	PILEngineNumpyCV2Enginelower)ks    r   <lambda>zattach.<locals>.<lambda>M   s    HHS r   z(The saver engine to use (pil, numpy+cv2))destr)   r2   z--scale   zDefine the resolution of the output images. By default, one PDF point (1/72in) is rendered to 1x1 pixel. This factor scales the number of pixels that represent one point.z
--rotationr   )r   Z      i  z'Rotate pages by 90, 180 or 270 degrees.)r4   r)   choicesr2   --fill-colorr2   zColor the bitmap will be filled with before rendering. It shall be given in RGBA format as a sequence of integers ranging from 0 to 255. Defaults to white.z--optimize-mode)lcdprintz3The rendering optimisation mode. None if not given.)rA   r2   z--cropr&   )r   r   r   r   z/Amount to crop from (left, bottom, right, top).)r(   r)   r4   r2   z--draw-annotsz1Whether annotations may be shown (default: true).)actionr4   r2   z--draw-formsz+Whether forms may be shown (default: true).z--no-antialias+)textimagepathz&Item types that shall not be smoothed.)r(   r4   rA   r)   r2   z--force-halftone
store_truez)Always use halftone for image stretching.)rE   r2   zBitmap optionsz&Bitmap config, including pixel format.)titledescriptionz--bitmap-makerr   zThe bitmap maker to use.)rA   r4   r2   r)   z--grayscalez0Whether to render in grayscale mode (no colors).z--byteorderrev_byteorderc                 <    ddd|                                           S )NFT)bgrrgb)r:   )vs    r   r<   zattach.<locals>.<lambda>   s    t44QWWYY? r   z;Whether to use BGR or RGB byteorder (default: conditional).z--x-channelprefer_bgrxz@Whether to prefer BGRx/RGBx over BGR/RGB (default: conditional).)r=   rE   r2   Parallelizationz.Options for rendering with multiple processes.z--linear?zRender non-parallel if page count is less or equal to the specified value (default is conditional). If this flag is given without a value, then render linear regardless of document length.)r(   r)   constr2   z--processeszXThe maximum number of parallel rendering processes. Defaults to the number of CPU cores.z--parallel-strategy)spawn
forkserverforkrV   zQThe process start method to use. ('fork' is discouraged due to stability issues.))rA   r4   r)   r2   z--parallel-libmpftrZ   zRThe parallelization module to use (mp = multiprocessing, ft = concurrent.futures).z--parallel-mapzLThe map function to use (backend specific, the default is an iterative map).)r)   r2   zForced color schemez[Options for using pdfium's forced color scheme renderer. Deprecated, considered not useful.z--sample-themezWUse a dark background sample theme as base. Explicit color params override selectively.--path-fill--path-stroke--text-fill--text-strokez--fill-to-strokezqOnly draw borders around fill areas using the `path_stroke` color, instead of filling with the `path_fill` color.)rB   )r\   )r]   )r^   )r_   )r   add_argumentr   strr:   floatint	ColorOptsr   add_argument_groupBitmapMakerskeysmathinfos	cpu_count)parserbitmapparallelcolor_schemes       r   attachrp   8   sm   fD!!!!
DU	     U     Dy)	     SS9	      |	     #8     F  m   
 "D    
 @     &B	     &<	     +y7     :     && > '  F ##%%)y     A    
 ??L	     &Q	     ((!F )  H  N     ,..i	     1yb     yc     y]     ,,%s -  L h    
 L 
   L 
   L 
   L 
    C      r   c                        e Zd Zd Zd Zd ZdS )SavingEnginec                     || _         d S N_path_parts)self
path_partss     r   __init__zSavingEngine.__init__   s    %r   c                 B    | j         \  }}}}|| |dz   d| dd| z  S )Nr>   0dr	   ru   )rw   i
output_dirprefixn_digitsr   s         r   	_get_pathzSavingEngine._get_path   sC    /3/?,
FHfvBqsBXBBBBB&BBBBr   c                     |                      |          }|                     ||           t                              d|dz    d|j                    d S )NzWrote page r>   z as )r   _saving_hookloggerinfoname)rw   rm   r}   out_paths       r   __call__zSavingEngine.__call__   sX    >>!$$(F+++:!A#::8=::;;;;;r   N)__name__
__module____qualname__ry   r   r    r   r   rr   rr      sD        & & &C C C< < < < <r   rr   c                       e Zd Zd ZdS )r8   c                 T    |                                                     |           d S rt   )to_pilsaverw   r   rm   s      r   r   zPILEngine._saving_hook   s$    X&&&&&r   Nr   r   r   r   r   r   r   r8   r8      s#        ' ' ' ' 'r   r8   c                       e Zd Zd ZdS )r9   c                 n    t          j        t          |          |                                           d S rt   )cv2imwritera   to_numpyr   s      r   r   zNumpyCV2Engine._saving_hook   s*    CMM6??#4#455555r   Nr   r   r   r   r9   r9      s#        6 6 6 6 6r   r9   c                     | r
 |              t                               dt          j                                t	          j        ||d          }|r|                                 |||fad S )NzInitializing data for process T)password	autoclose)r   r   rj   getpidr   PdfDocument
init_formsProcObjs)
extra_initinputr   may_init_formsr   enginepdfs          r   _render_parallel_initr     sw     

KK>>>???

UX
F
F
FC  VV$HHHr   c                 H    ||          } |j         di |} |||            d S )Nr   )render)r}   r   r   r   pagerm   s         r   _render_jobr     s:    q6DT[""6""F
F61r   c                 *    t          | gt          R   d S rt   )r   r   )r}   s    r   _render_parallel_jobr     s     .X......r   c                     t            j                  }t          |          t          fd j        D                       st          d          t           j                  t          t           j                            k    rt          d           j         j        j	         d _         j
         j        rdnd _
         j         j        dk    rd	nd
 _         j        /t          d k    r j        dk    rt            _        nt"           _         j         j        t"          u  _         j         j        t"          u o
 j        dk     _        t)                      } j        r |j        di t,            |j        di  fdt.          D              t          |          dk    rt1          j        di |nd }t)           j         j         j         j         j
        | j         j         j          j         j!         j         j        tD           j#                           } j$        D ]
}d|d| <   tJ          &                     j        j'         d j         d j         d j                    t          tQ                              } j)         j        | j        f}                     |          }t           j                   j        k    r8tJ          &                    d            j        D ]}	tU          |	|||           d S tJ          &                    d           tW          j,         j-                  }
t)          |
j.        dft_          j0        tb          j2        |
          df          }| j3                 \  }} j4        r j4        } j-        dv rtj          nd }t)          tl          ||j7         j8         j        ||f          }ts           j:        t           j                            } ||fi |5 }tw          ||          } |tx           j                  D ]}	 d d d            d S # 1 swxY w Y   d S )N)r   c              3   8   K   | ]}d |cxk    ok     nc V  dS )r   Nr   ).0r}   pdf_lens     r   	<genexpr>zmain.<locals>.<genexpr>#  s?      44AqA444444r   z*Out-of-bounds page indices are prohibited.z&Duplicate page indices are prohibited._)r   r   r   r,   r.   r3         pngc                 R    i | ]#}t          |          |t          |          $S r   )getattr)r   fr   s     r   
<dictcomp>zmain.<locals>.<dictcomp>@  s4    RRRqAQAQR74++RRRr   r   )scalerotationcrop	grayscale
fill_colorro   fill_to_strokeoptimize_modedraw_annotsmay_draw_formsforce_halftonerM   rR   bitmap_makerT	no_smoothz
, Format: z, rev_byteorder: z, prefer_bgrx zLinear rendering ...zParallel rendering ...imap)
mp_contextmaprY   )rV   rW   )initializerinitargsr   )=r   
draw_formslenallr0   
ValueErrorsetr   r   stemr   sample_themelinearr   r5   r   r9   r8   rM   rR   dictupdateSampleThemeCsFieldsr   PdfColorSchemer   r   r   r   r   r   r   r   rf   r   no_antialiasr   r   r   ra   outputr   rZ   get_contextparallel_strategyPool	functoolspartialr[   ProcessPoolExecutorparallel_libparallel_mapr   r   _inputr   min	processesr   r   )r   r   	cs_kwargscsr   r)   r   rx   r   r}   ctxpool_backends	pool_ctormap_attrr   pool_kwargsn_procspoolmap_funcr   r   s   `                   @r   mainr     s    DT_
5
5
5C #hhG444444444 GEFFF
4:#c$*oo....ABBB{+++,0,=W..CW{;%//aaQ $;;4;%//,DOO'DO !!_	9?i7PDK5<PI (	'';'''ISSRRRRXRRRSSS/29~~/A/A		+	+	+	+	+tB
=yN_,*&,*&#D$56  F  ! * *%)!4!!"" KK4?+  J  Jt{  J  JUYUg  J  Jw{  xH  J  J  K  K  K3w<<  H+t{HdkBJ__Z((F
4:$+%%*+++ 	0 	0A3////	0 	0
 	,---nT344(F##B$:sKKKUS
 
 
 ,D,=>	8 	)(H'+'=AX'X'Xmm^b
/"CJtPVX^_
 
 

 dnc$*oo66Yw..+.. 	$tX..HX2DJ??  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   *QQQ)1rj   rh   loggingr   pathlibr   multiprocessingrZ   concurrent.futuresfuturesr[   r   ImportErrorpypdfium2._helpers_helpersr   pypdfium2.internalinternalr   pypdfium2.rawrawr
   pypdfium2._cli._parsersr   r   r   r   	getLoggerr   r   r   r   r   
new_nativenew_foreignr   rf   r   rc   rd   r   rp   rr   r8   r9   r   r   r   r   r   r   r   <module>r      s   
			                      JJJJ   
CCC $ # # # # # % % % % % %                        
	8	$	$Z Z Z t(*&Y&v'7'CRVWWW3	   DDAC000	d&&&&  o o od< < < < < < < <' ' ' ' ' ' ' '6 6 6 6 6l 6 6 6
% % %  / / /` ` ` ` `s   ' 11