a
    còf”_  ã                   @   s@  d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZmZmZ dd
lmZ ddlmZm Z m!Z! ddlm"Z# ddl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3m4Z4 ddlm5Z5m6Z6m7Z7 dZ8G dd„ dƒZ9G dd„ dƒZ:dS )é    )ÚBytesIOÚFileIOÚIOBase)ÚPath)ÚTracebackType)	ÚAnyÚDictÚIterableÚListÚOptionalÚTupleÚTypeÚUnionÚcasté   )Ú
Encryption)Ú
PageObject)Ú	PdfReader)ÚStrByteTypeÚdeprecate_with_replacementÚstr_)Ú	PdfWriter)ÚGoToActionArgumentsÚTypArgumentsÚTypFitArguments)ÚPagesAttributes)ÚPAGE_FITÚArrayObjectÚDestinationÚDictionaryObjectÚFitÚFloatObjectÚIndirectObjectÚ
NameObjectÚ
NullObjectÚNumberObjectÚOutlineItemÚTextStringObjectÚ
TreeObject)Ú	PageRangeÚPageRangeSpec)Ú
LayoutTypeÚOutlineTypeÚPagemodeTypez=close() was called and thus the writer cannot be used anymorec                   @   s$   e Zd ZdZeeeddœdd„ZdS )Ú_MergedPagez@Collect necessary information on each page that is being merged.N)ÚpagedataÚsrcÚidÚreturnc                 C   s   || _ || _d | _|| _d S ©N)r0   r/   Úout_pagedatar1   )Úselfr/   r0   r1   © r6   úJ/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/pypdf/_merger.pyÚ__init__N   s    z_MergedPage.__init__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úintr8   r6   r6   r6   r7   r.   K   s   r.   c                   @   sþ  e Zd ZdZdEeeeef ddœdd„Zd dœd	d
„Z	e
ee  e
e e
e ddœdd„ZdFeeeeef e
e e
e eddœdd„Zeeeef eee
e f dœdd„ZdGeeeef e
e edeeeef eeeef ee f eddœdd„Zeeef ddœdd„Zddœdd„Zeeef ddœdd„Ze ddœd d!„Z!e"dd"œd#d$„Z#e$e
e" dœd%d&„ƒZ%e%j&e"dd"œd'd&„ƒZ%eeeeeef f eeeef eeeef ee f eeeef  d(œd)d*„Z'ee(eeeef eeeef ee f e(d+œd,d-„Z)ddœd.d/„Z*dHe
e+e,  e
e- dd0œd1d2„Z.ee,e/f e0dd3œd4d5„Z1ee0 dd6œd7d8„Z2dIee0 e
e+e,  dd9œd:d;„Z3dJeeef e
e( e
ee  d<œd=d>„Z4dddde5feeede-e6f e
ee7e7e7f  eee8e6d?œd@dA„Z9eeddBœdCdD„Z:dS )KÚ	PdfMergerzD
    Use :class:`PdfWriter` instead.

    .. deprecated:: 5.0.0
    FÚ N)ÚstrictÚfileobjr2   c                 C   sB   t dddƒ g | _g | _tƒ | _g | _g | _d| _|| _|| _	d S )Nr>   r   ú5.0.0r   )
r   ÚinputsÚpagesr   ÚoutputÚoutlineÚnamed_destsÚid_countrA   r@   )r5   r@   rA   r6   r6   r7   r8   \   s    zPdfMerger.__init__)r2   c                 C   s   t dddƒ | S )Nr>   r   rB   )r   ©r5   r6   r6   r7   Ú	__enter__i   s    zPdfMerger.__enter__)Úexc_typeÚexcÚ	tracebackr2   c                 C   s   | j r|  | j ¡ |  ¡  dS )z*Write to the fileobj and close the merger.N)rA   ÚwriteÚclose)r5   rK   rL   rM   r6   r6   r7   Ú__exit__n   s    zPdfMerger.__exit__T)Úpage_numberrA   Úoutline_itemrD   Úimport_outliner2   c                 C   sx  |   |¡\}}t|| jd�}| j ||f¡ |dur:||_|du rRdt|jƒf}n:t|t	ƒrn| 
t|jƒ¡}nt|tƒrznt|tƒsŒtdƒ‚g }	g }
|r¬|j}
|  ||
|¡}
|rÞtt|ƒt| jƒt ¡ ƒ}|  j||
g7  _n|  j|
7  _|j}|  |||¡}|  j|7  _t|Ž D ]:}|j| }| j}|  jd7  _t|||ƒ}|	 |¡ �q|  |	¡ |  |	¡ |	| j||…< dS )aÖ  
        Merge the pages from the given file into the output file at the
        specified page number.

        Args:
            page_number: The *page number* to insert this file. File will
                be inserted after the given number.
            fileobj: A File Object or an object that supports the standard
                read and seek methods similar to a File Object. Could also be a
                string representing a path to a PDF file.
            outline_item: Optionally, you may specify an outline item
                (previously referred to as a 'bookmark') to be applied at the
                beginning of the included file by supplying the text of the outline item.
            pages: can be a :class:`PageRange<pypdf.pagerange.PageRange>`
                or a ``(start, stop[, step])`` tuple
                to merge only the specified range of pages from the source
                document into the output document.
                Can also be a list of pages to merge.
           import_outline: You may prevent the source document's
                outline (collection of outline items, previously referred to as
                'bookmarks') from being imported by specifying this as ``False``.
        )r@   Nr   z0"pages" must be a tuple of (start, stop[, step])r   )Ú_create_streamr   r@   rC   ÚappendÚ_encryptionÚlenrD   Ú
isinstancer)   ÚindicesÚlistÚtupleÚ	TypeErrorrF   Ú_trim_outliner&   r'   r%   rH   r    ÚfitZnamed_destinationsÚ_trim_destsrG   Úranger.   Ú_associate_dests_to_pagesÚ!_associate_outline_items_to_pages)r5   rQ   rA   rR   rD   rS   ÚstreamÚencryption_objÚreaderZsrcpagesrF   Zoutline_item_typÚdestsZtrimmed_destsÚiÚpager1   Úmpr6   r6   r7   Úmergey   sL    


ý


zPdfMerger.merge)rA   r2   c                 C   s¦   d }t |ttfƒrt|dƒ}n€t |tƒrf|jr4|j}|j ¡ }|j d¡ t	|j 
¡ ƒ}|j |¡ n8t|dƒr–t|dƒr–| d¡ | 
¡ }t	|ƒ}ntdƒ‚||fS )NÚrbr   ÚseekÚreadzÝPdfMerger.merge requires an object that PdfReader can parse. Typically, that is a Path or a string representing a Path, a file object, or an object implementing .seek and .read. Passing a PdfReader directly works as well.)rX   Ústrr   r   r   rV   rc   Útellrl   r   rm   ÚhasattrÚNotImplementedError)r5   rA   rd   rc   Z	orig_tellZfile_contentr6   r6   r7   rT   Ð   s$    	



ÿzPdfMerger._create_stream)rA   rR   rD   rS   r2   c                 C   s   |   t| jƒ||||¡ dS )a¤  
        Identical to the :meth:`merge()<merge>` method, but assumes you want to
        concatenate all pages onto the end of the file instead of specifying a
        position.

        Args:
            fileobj: A File Object or an object that supports the standard
                read and seek methods similar to a File Object. Could also be a
                string representing a path to a PDF file.
            outline_item: Optionally, you may specify an outline item
                (previously referred to as a 'bookmark') to be applied at the
                beginning of the included file by supplying the text of the outline item.
            pages: can be a :class:`PageRange<pypdf.pagerange.PageRange>`
                or a ``(start, stop[, step])`` tuple
                to merge only the specified range of pages from the source
                document into the output document.
                Can also be a list of pages to append.
            import_outline: You may prevent the source document's
                outline (collection of outline items, previously referred to as
                'bookmarks') from being imported by specifying this as ``False``.
        N)rj   rW   rD   )r5   rA   rR   rD   rS   r6   r6   r7   rU   ó   s    zPdfMerger.appendc                 C   s’   | j du rttƒ‚| jD ]H}| j  |j¡ tttt	f | j j
 ¡ ƒ}| j  |tj d  ¡ ¡|_q|  ¡  |  ¡  | j  |¡\}}|rŽ| ¡  dS )zÃ
        Write all data that has been merged to the given output file.

        Args:
            fileobj: Output file. Can be a filename or any kind of
                file-like object.
        Néÿÿÿÿ)rE   ÚRuntimeErrorÚERR_CLOSED_WRITERrD   Zadd_pager/   r   r   rn   r   Z_pagesÚ
get_objectZget_referenceÚPAZKIDSr4   Ú_write_destsÚ_write_outlinerN   rO   )r5   rA   rh   Z	pages_objZmy_fileZret_fileobjr6   r6   r7   rN     s    

ÿzPdfMerger.writec                 C   s.   g | _ | jD ]\}}| ¡  qg | _d| _dS )zHShut all file descriptors (input and output) and clear all memory usage.N)rD   rC   rO   rE   )r5   Zfile_descriptorÚ_readerr6   r6   r7   rO   2  s
    
zPdfMerger.close)Úinfosr2   c                 C   s"   | j du rttƒ‚| j  |¡ dS )zò
        Add custom metadata to the output.

        Args:
            infos: a Python dictionary where each key is a field
                and each value is your new metadata.
                An example is ``{'/Title': 'My title'}``
        N)rE   rs   rt   Úadd_metadata)r5   rz   r6   r6   r7   r{   ;  s    	
zPdfMerger.add_metadata)Úlayoutr2   c                 C   s"   | j du rttƒ‚| j  |¡ dS )a  
        Set the page layout.

        Args:
            layout: The page layout to be used

        .. list-table:: Valid ``layout`` arguments
           :widths: 50 200

           * - /NoLayout
             - Layout explicitly not specified
           * - /SinglePage
             - Show one page at a time
           * - /OneColumn
             - Show one column at a time
           * - /TwoColumnLeft
             - Show pages in two columns, odd-numbered pages on the left
           * - /TwoColumnRight
             - Show pages in two columns, odd-numbered pages on the right
           * - /TwoPageLeft
             - Show two pages at a time, odd-numbered pages on the left
           * - /TwoPageRight
             - Show two pages at a time, odd-numbered pages on the right
        N)rE   rs   rt   Z_set_page_layout)r5   r|   r6   r6   r7   Úset_page_layoutH  s    
zPdfMerger.set_page_layout)Úmoder2   c                 C   s
   || _ dS ©aW  
        Set the page mode.

        Args:
            mode: The page mode to use.

        .. list-table:: Valid ``mode`` arguments
           :widths: 50 200

           * - /UseNone
             - Do not show outline or thumbnails panels
           * - /UseOutlines
             - Show outline (aka bookmarks) panel
           * - /UseThumbs
             - Show page thumbnails panel
           * - /FullScreen
             - Fullscreen view
           * - /UseOC
             - Show Optional Content Group (OCG) panel
           * - /UseAttachments
             - Show attachments panel
        N)Ú	page_mode©r5   r~   r6   r6   r7   Úset_page_modee  s    zPdfMerger.set_page_modec                 C   s   | j du rttƒ‚| j jS r   ©rE   rs   rt   r€   rI   r6   r6   r7   r€   ~  s    
zPdfMerger.page_modec                 C   s   | j d u rttƒ‚|| j _d S r3   rƒ   r�   r6   r6   r7   r€   š  s    
)Úpdfrf   rD   r2   c           	      C   s’   g }t |tƒr|n
tt|Ž ƒ}| ¡ D ]f\}}|D ]X}|j|  ¡ |d  ¡ kr2|d  ¡ |tdƒ< t|ƒt|d ƒks|J ‚| |¡  q&q2q&|S )z 
        Remove named destinations that are not a part of the specified page set.

        Args:
            pdf:
            dests:
            pages:
        ú/Pageú/Title)	rX   rZ   r`   ÚitemsrD   ru   r#   r   rU   )	r5   r„   rf   rD   Z	new_destsÚlstÚkeyÚobjÚjr6   r6   r7   r_      s    
zPdfMerger._trim_dests)r„   rF   rD   r2   c                 C   sÒ   g }d}t |tƒr|n
tt|Ž ƒ}t|ƒD ]¢\}}t |tƒrp|  |||¡}	|	rÌ|sd| ||d  ¡ | |	¡ q*d}|D ]R}
|d du rŠqx|j|
  ¡ |d  ¡ krx|d  ¡ |tdƒ< | |¡ d} q*qxq*|S )zÒ
        Remove outline item entries that are not a part of the specified page set.

        Args:
            pdf:
            outline:
            pages:

        Returns:
            An outline type
        Tr   Fr…   N)	rX   rZ   r`   Ú	enumerater]   rU   rD   ru   r#   )r5   r„   rF   rD   Znew_outlineZprev_header_addedrˆ   rg   rR   Úsubr‹   r6   r6   r7   r]   ¹  s,    
ÿ
zPdfMerger._trim_outlinec                 C   sv   | j d u rttƒ‚| jD ]X}d }d|v r\t| jƒD ](\}}|j|d kr2|j|tdƒ<  q\q2|d ur| j  	|¡ qd S )Nr…   )
rE   rs   rt   rG   rŒ   rD   r1   r4   r#   Zadd_named_destination_object)r5   Ú
named_destÚ
page_indexrh   r6   r6   r7   rw   â  s    

zPdfMerger._write_dests)rF   Úparentr2   c                 C   sº   | j d u rttƒ‚|d u r | j}|d us0J dƒ‚d }|D ]|}t|tƒrT|  ||¡ q8d }d|v r’t| jƒD ]&\}}|j	|d krj|  
||¡  q’qj|d ur8|d= |d= | j  ||¡}q8d S )Núhint for mypyr…   ú/Type)rE   rs   rt   rF   rX   rZ   rx   rŒ   rD   r1   Ú_write_outline_item_on_pageZadd_outline_item_dict)r5   rF   r�   Z
last_addedrR   Zpage_norh   r6   r6   r7   rx   ð  s&    

zPdfMerger._write_outline)rR   rh   r2   c                 C   sò   t t|d ƒ}t|jƒt|ƒg}tjtjftj	tjftj
tjftjtjftjtjtjdftjtjtjtjtjfi}| |d¡D ]B}||v r¬t|| tƒs¬| t|| ƒ¡ n| tdƒ¡ ||= q~tttjƒtdƒttjƒt|ƒiƒ|tdƒ< d S )Nr’   z/Zoomr6   r   z/GoToz/A)r   rn   r%   r1   r#   r   ZFIT_Hr   ZTOPZFIT_BHZFIT_VÚLEFTZFIT_BVZXYZZFIT_RZBOTTOMÚRIGHTÚgetrX   r$   rU   r!   r   r   ÚSÚDr   )r5   rR   rh   Zoi_typeÚargsZfit2arg_keysZarg_keyr6   r6   r7   r“     s4    



üú
ÿþÿz%PdfMerger._write_outline_item_on_page)rD   r2   c                 C   sv   | j D ]j}d }|d }t|tƒr"q|D ]}| ¡ |j ¡ kr&|j}q&|d u r`td|d › d�ƒ‚t|ƒ|tdƒ< qd S )Nr…   zUnresolved named destination 'r†   ú')rG   rX   r%   ru   r/   r1   Ú
ValueErrorr#   )r5   rD   rŽ   r�   Únprh   r6   r6   r7   ra   -  s    

ÿz#PdfMerger._associate_dests_to_pages)rD   rF   r2   c                 C   s–   |d u r| j }|d usJ dƒ‚|D ]n}t|tƒr>|  ||¡ q"d }|d }t|tƒrVq"|D ]}| ¡ |j ¡ krZ|j}qZ|d ur"t|ƒ|tdƒ< q"d S )Nr‘   r…   )	rF   rX   rZ   rb   r%   ru   r/   r1   r#   )r5   rD   rF   rR   r�   Zoutline_item_pageÚpr6   r6   r7   rb   ?  s     

z+PdfMerger._associate_outline_items_to_pages)rR   Úrootr2   c                 C   sz   |d u r| j }t|ƒD ]^\}}t|tƒrH|  ||¡}|rt|g|   S q||ksjttttf |d ƒ|kr|g  S qd S )Nr†   )rF   rŒ   rX   rZ   Úfind_outline_itemr   r   r   )r5   rR   rž   rg   Zoi_enumÚresr6   r6   r7   rŸ   W  s    
ÿþzPdfMerger.find_outline_item)ÚtitlerQ   r�   ÚcolorÚboldÚitalicr^   r2   c           	   
   C   s.   | j }|du rttƒ‚| |||d||||¡S )aQ  
        Add an outline item (commonly referred to as a "Bookmark") to this PDF file.

        Args:
            title: Title to use for this outline item.
            page_number: Page number this outline item will point to.
            parent: A reference to a parent outline item to create nested
                outline items.
            color: Color of the outline item's font as a red, green, blue tuple
                from 0.0 to 1.0
            bold: Outline item font is bold
            italic: Outline item font is italic
            fit: The fit of the destination page.
        N)rE   rs   rt   Úadd_outline_item)	r5   r¡   rQ   r�   r¢   r£   r¤   r^   Úwriterr6   r6   r7   r¥   o  s    øzPdfMerger.add_outline_item)r¡   rQ   r2   c                 C   s,   t t|ƒt|ƒtjdd�ƒ}| j |¡ dS )z¢
        Add a destination to the output.

        Args:
            title: Title to use
            page_number: Page number this destination points at.
        i:  )ÚtopN)r   r'   r%   r    Zfit_horizontallyrG   rU   )r5   r¡   rQ   Údestr6   r6   r7   Úadd_named_destination•  s    
ýzPdfMerger.add_named_destination)Fr?   )NNT)NNT)NN)N)N);r9   r:   r;   r<   Úboolr   r   r   r8   rJ   r   r   ÚBaseExceptionr   rP   r=   r   rn   r*   rj   r   r   r   rT   r)   r
   rU   rN   rO   r   r   r{   r+   r}   r-   r‚   Úpropertyr€   Úsetterr_   r,   r]   rw   r	   r&   r(   rx   r   r.   r“   ra   rb   rŸ   r   r"   Úfloatr    r¥   r©   r6   r6   r6   r7   r>   U   sÆ    ÿþ
û   úùXþ&   ù"ÿø 	"û"û)  ý
üþ" ÿþ ý

üø÷(ür>   N);Úior   r   r   Úpathlibr   Útypesr   Útypingr   r   r	   r
   r   r   r   r   r   rV   r   Z_pager   ry   r   Ú_utilsr   r   r   Z_writerr   Ú	constantsr   r   r   r   rv   Zgenericr   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   Z	pageranger)   r*   r+   r,   r-   rt   r.   r>   r6   r6   r6   r7   Ú<module>   s    ,<
