o
    f42                     @   sT  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
 zd dlZW n ey1   dZY n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eZdd Zeej j!ej j"ej#ej j"ddedZ$d	Z%ed
de&dZ'eddddd	Z(dd Z)G dd dZ*G dd de*Z+G dd de*Z,dd Z-dd Z.dd Z/dd  Z0dS )!    N)Path)	add_input	get_inputsetup_loggingBooleanOptionalActionc                 O   sT   |t jkrd}n|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_alpha r   N/home/ubuntu/webapp/venv/lib/python3.10/site-packages/pypdfium2/_cli/render.py_bitmap_wrapper_foreign_simple   s   

r   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                 C   sX  t | dd | jddtddd | jddd	 | jd
ddtjdd | jdddd dd | jddtdd | jddtddd | j	dfddit | jd d!d"d# | jd$d%td&d'd( | jd)tdd*d+ | jd,tdd-d+ | jd.d/g d0tjd1d2 | jd3d4d5d6 | j	d7d8d9}|jd:t
 d;d<tjd= |jd>d4d?d6 |jd@dAdBd dCd |jdDdEtdFdG | j	dHdId9}|jdJdKttjdLdM |jdNt tdOd |jdPdQdRtjdSdT |jdUdVdWtjdXdT |jdYtjdZd[ | j	d\d]d9}|jd^d4d_d6 |j	`dgi t |j	adhi t |j	bdii t |j	cdji t |jddd4ded6 d S )kNT)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.)r/   z--formatz-fjpgzThe image format to use.)defaultr'   r/   z--engine
engine_clsc                 S   s   t td|   S )N)pilz	numpy+cv2)	PILEngineNumpyCV2Enginelower)kr   r   r   <lambda>M       zattach.<locals>.<lambda>z(The saver engine to use (pil, numpy+cv2))destr'   r/   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.)r1   r'   choicesr/   --fill-colorr/   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.)r>   r/   z--cropr$   )r   r   r   r   z/Amount to crop from (left, bottom, right, top).)r&   r'   r1   r/   z--draw-annotsz1Whether annotations may be shown (default: true).)actionr1   r/   z--draw-formsz+Whether forms may be shown (default: true).z--no-antialias+)textimagepathz&Item types that shall not be smoothed.)r&   r1   r>   r'   r/   z--force-halftone
store_truez)Always use halftone for image stretching.)rB   r/   zBitmap optionsz&Bitmap config, including pixel format.)titledescriptionz--bitmap-makerr   zThe bitmap maker to use.)r>   r1   r/   r'   z--grayscalez0Whether to render in grayscale mode (no colors).z--byteorderrev_byteorderc                 S   s   ddd|    S )NFT)bgrrgb)r6   )vr   r   r   r8      r9   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:   rB   r/   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'   constr/   z--processeszXThe maximum number of parallel rendering processes. Defaults to the number of CPU cores.z--parallel-strategy)spawn
forkserverforkrR   zQThe process start method to use. ('fork' is discouraged due to stability issues.))r>   r1   r'   r/   z--parallel-libmpftrV   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'   r/   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.)r?   )rX   )rY   )rZ   )r[   )r   add_argumentr   strr6   floatint	ColorOptsr   add_argument_groupBitmapMakerskeysmathinfos	cpu_count)parserbitmapparallelcolor_schemer   r   r   attach8   sd  
rl   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )SavingEnginec                 C   s
   || _ d S N_path_parts)self
path_partsr   r   r   __init__   s   
zSavingEngine.__init__c                 C   s2   | j \}}}}|| |d d| dd|  S )Nr;   0dr   ro   )rq   i
output_dirprefixn_digitsr   r   r   r   	_get_path   s   $zSavingEngine._get_pathc                 C   s6   |  |}| || td|d  d|j  d S )NzWrote page r;   z as )rz   _saving_hookloggerinfoname)rq   ri   rv   out_pathr   r   r   __call__   s   
 zSavingEngine.__call__N)__name__
__module____qualname__rs   rz   r   r   r   r   r   rm      s    rm   c                   @      e Zd Zdd ZdS )r4   c                 C   s   |  | d S rn   )to_pilsaverq   r   ri   r   r   r   r{      s   zPILEngine._saving_hookNr   r   r   r{   r   r   r   r   r4          r4   c                   @   r   )r5   c                 C   s   t t||  d S rn   )cv2imwriter]   to_numpyr   r   r   r   r{      s   zNumpyCV2Engine._saving_hookNr   r   r   r   r   r5      r   r5   c                 C   sH   | r|   t dt   tj||dd}|r|  |||fad S )NzInitializing data for process T)password	autoclose)r|   r}   rf   getpidr   PdfDocument
init_formsProcObjs)
extra_initinputr   may_init_formsr   enginepdfr   r   r   _render_parallel_init  s   r   c                 C   s&   ||  }|j di |}|||  d S )Nr   )render)rv   r   r   r   pageri   r   r   r   _render_job  s   r   c                 C   s   t | gtR   d S rn   )r   r   )rv   r   r   r   _render_parallel_job  s   r   c                    s:  t   jd}t|tfdd jD stdt jtt jkr+td jd u r8 jj	 d _ j
d u rE jrBdnd _
 jd u rT jd	krQd
nd _ jd u ritd krf jdkrft _nt _ jd u rt jtu  _ jd u r jtu o jdk _t } jr|jdi t |jdi  fddtD  t|dkrtjdi |nd }t j j j j j
| j j j  j j! j jt" j# d} j$D ]	}d|d| < qt%& jj' d j d j d j  tt(} j) j| jf} |}t j jkr&t%&d  jD ]
}	t*|	||| qd S t%&d t+, j-}
t|
j.dft/j0t1j2|
ddfd}| j3 \}} j4rP j4} j-dv rXt5nd }tt6||j7 j8 j||fd}t9 j:t j}||fi |}t;||}|t< jD ]}qW d    d S 1 sw   Y  d S )N)r   c                 3   s(    | ]}d |  ko k n  V  qdS )r   Nr   ).0rv   )pdf_lenr   r   	<genexpr>#  s   & zmain.<locals>.<genexpr>z*Out-of-bounds page indices are prohibited.z&Duplicate page indices are prohibited._)r   r   r   r*   r,   r0         pngc                    s"   i | ]}t  |r|t  |qS r   )getattr)r   f)r   r   r   
<dictcomp>@  s   " zmain.<locals>.<dictcomp>r   )scalerotationcrop	grayscale
fill_colorrk   fill_to_strokeoptimize_modedraw_annotsmay_draw_formsforce_halftonerJ   rN   bitmap_makerT	no_smoothz
, Format: z, rev_byteorder: z, prefer_bgrx zLinear rendering ...zParallel rendering ...imap)
mp_contextmaprU   )rR   rS   )initializerinitargsr   )=r   
draw_formslenallr-   
ValueErrorsetrx   r   stemr   sample_themelinearr   r2   r   r5   r4   rJ   rN   dictupdateSampleThemeCsFieldsr   PdfColorSchemer   r   r   r   r   r   r   r   rb   r   no_antialiasr|   r}   r   r]   outputr   rV   get_contextparallel_strategyPool	functoolspartialrW   ProcessPoolExecutorparallel_libparallel_mapr   r   _inputr   min	processesr   r   )r   r   	cs_kwargscsr   r'   ry   rr   r   rv   ctxpool_backends	pool_ctormap_attrr   pool_kwargsn_procspoolmap_funcr   r   )r   r   r   main  s   





 
*




$r   )1rf   rd   loggingr   pathlibr   multiprocessingrV   concurrent.futuresfuturesrW   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   rb   r   r_   r`   r   rl   rm   r4   r5   r   r   r   r   r   r   r   r   <module>   sR   
	 3