a
    ¢…¢f w  ã                   @   sl  d dl Z 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mZ ddlmZ ddl m!Z! ddl m"Z# ddl m$Z$m%Z% ddl&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6 ddlm7Z7m8Z8m9Z9m:Z:m;Z; dZ<G dd„ dƒZ=G dd„ dƒZ>G dd„ de>ƒZ?dS )é    N)ÚBytesIOÚFileIOÚIOBase)ÚPath)ÚTracebackType)	ÚAnyÚDictÚIterableÚListÚOptionalÚTupleÚTypeÚUnionÚcasté   )Ú
Encryption)Ú
PageObject)Ú	PdfReader)ÚStrByteTypeÚdeprecation_bookmarkÚdeprecation_with_replacementÚstr_)Ú	PdfWriter)ÚGoToActionArguments)ÚPagesAttributes)ÚTypArgumentsÚTypFitArguments)ÚPAGE_FITÚArrayObjectÚDestinationÚDictionaryObjectÚFitÚFloatObjectÚIndirectObjectÚ
NameObjectÚ
NullObjectÚNumberObjectÚOutlineItemÚTextStringObjectÚ
TreeObject)Ú	PageRangeÚPageRangeSpec)ÚFitTypeÚ
LayoutTypeÚOutlineTypeÚPagemodeTypeÚZoomArgTypez=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)r3   r2   Úout_pagedatar4   )Úselfr2   r3   r4   © r9   úG/var/www/sistema_ama/venv/lib/python3.9/site-packages/PyPDF2/_merger.pyÚ__init__Q   s    z_MergedPage.__init__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úintr;   r9   r9   r9   r:   r1   N   s   r1   c                   @   s,  e Zd ZdZedd�dYe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eddd�dZee eeeef ee ee eee ddœdd„ƒZeeeef eeee f dœdd„Zeddd�d[e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eef dd!œd$d%„Z!e"dd&œd'd(„Z#e"dd&œd)d*„Z$e%dd+œd,d-„Z&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  d0œd1d2„Z(ee)eeeef eeeef ee f e)d3œd4d5„Z*dd
œd6d7„Z+edd�d\ee,e-  ee. dd8œd9d:„ƒZ/edd;�ee-e0f e1dd<œd=d>„ƒZ2ee1 dd?œd@dA„Z3edd�d]ee1 ee,e-  ddBœdCdD„ƒZ4edd;�d^eeef ee) eee  dEœdFdG„ƒZ5edd;�d_eeef ee) eee  dEœdHdI„ƒZ6ddddde7dfeee ede.e8f eee9e9e9f  eee:ee e8dJœ	dKdL„Z;d`eeede.e8f eee9e9e9f  eee<e=e8dNœ	dOdP„Z>daeeede.e8f eee9e9e9f  eee<e=e8dNœ	dQdR„Z?eeddSœdTdU„Z@dbeee ee ddVœdWdX„ZAdS )cÚ	PdfMergeraI  
    Initialize a ``PdfMerger`` object.

    ``PdfMerger`` merges multiple PDFs into a single PDF.
    It can concatenate, slice, insert, or any combination of the above.

    See the functions :meth:`merge()<merge>` (or :meth:`append()<append>`)
    and :meth:`write()<write>` for usage information.

    :param bool strict: Determines whether user should be warned of all
            problems and also causes some correctable problems to be fatal.
            Defaults to ``False``.
    :param fileobj: Output file. Can be a filename or any kind of
            file-like object.
    Úoutline)Z	bookmarksFÚ N)ÚstrictÚfileobjr5   c                 C   s6   g | _ g | _tƒ | _g | _g | _d| _|| _|| _d S )Nr   )	ÚinputsÚpagesr   ÚoutputrB   Únamed_destsÚid_countrE   rD   )r8   rD   rE   r9   r9   r:   r;   i   s    zPdfMerger.__init__)r5   c                 C   s   | S r6   r9   )r8   r9   r9   r:   Ú	__enter__v   s    zPdfMerger.__enter__)Úexc_typeÚexcÚ	tracebackr5   c                 C   s   | j r|  | j ¡ |  ¡  dS )z*Write to the fileobj and close the merger.N)rE   ÚwriteÚclose)r8   rL   rM   rN   r9   r9   r:   Ú__exit__z   s    zPdfMerger.__exit__Úoutline_itemÚimport_outline)ÚbookmarkZimport_bookmarksT)Úpage_numberrE   rR   rG   rS   Úpositionr5   c                 C   sÚ  |dur>|du r6|}d}d}t  |› d|› d�t¡ ntdƒ‚|du rNtdƒ‚|du r^tdƒ‚|  |¡\}	}
t|	| jd	�}| j |	|f¡ |
dur˜|
|_	|du r°d
t
|jƒf}n:t|tƒrÌ| t
|jƒ¡}nt|tƒrØn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|||ƒ}| |¡ �qx|  |¡ |   |¡ || j||…< dS )a¾  
        Merge the pages from the given file into the output file at the
        specified page number.

        :param int page_number: The *page number* to insert this file. File will
            be inserted after the given number.

        :param 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.

        :param str 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.

        :param pages: can be a :class:`PageRange<PyPDF2.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.

        :param bool 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``.
        NrV   rU   zG is deprecated as an argument and will be removed in PyPDF2=4.0.0. Use ú insteadzCThe argument position of merge is deprecated. Use page_number only.úpage_number may not be Nonezfileobj may not be None)rD   r   z0"pages" must be a tuple of (start, stop[, step])r   )!ÚwarningsÚwarnÚDeprecationWarningÚ
ValueErrorÚ_create_streamr   rD   rF   ÚappendÚ_encryptionÚlenrG   Ú
isinstancer*   ÚindicesÚlistÚtupleÚ	TypeErrorrB   Ú_trim_outliner'   r(   r&   rJ   r!   ÚfitZnamed_destinationsÚ_trim_destsrI   Úranger1   Ú_associate_dests_to_pagesÚ!_associate_outline_items_to_pages)r8   rU   rE   rR   rG   rS   rV   Úold_termÚnew_termÚstreamÚencryption_objÚreaderZsrcpagesrB   Zoutline_item_typÚdestsZtrimmed_destsÚiÚpager4   Úmpr9   r9   r:   Úmerge…   sp    #ÿûÿ


ý


zPdfMerger.merge)rE   r5   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.)ra   Ústrr   r   r   r_   rn   Útellrw   r   rx   ÚhasattrÚNotImplementedError)r8   rE   ro   rn   Z	orig_tellZfilecontentr9   r9   r:   r]   û   s$    	



ÿzPdfMerger._create_stream)rE   rR   rG   rS   r5   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.

        :param 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.

        :param str 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.

        :param pages: can be a :class:`PageRange<PyPDF2.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.

        :param bool 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)ru   r`   rG   )r8   rE   rR   rG   rS   r9   r9   r:   r^     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.

        :param fileobj: Output file. Can be a filename or any kind of
            file-like object.
        Néÿÿÿÿ)rH   ÚRuntimeErrorÚERR_CLOSED_WRITERrG   Zadd_pager2   r   r   ry   r   Z_pagesÚ
get_objectZget_referenceÚPAZKIDSr7   Ú_write_destsÚ_write_outlinerO   rP   )r8   rE   rs   Z	pages_objZmy_fileZret_fileobjr9   r9   r:   rO   A  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)rG   rF   rP   rH   )r8   ÚfoÚ_readerr9   r9   r:   rP   a  s
    
zPdfMerger.close)Úinfosr5   c                 C   s"   | j du rttƒ‚| j  |¡ dS )zá
        Add custom metadata to the output.

        :param dict infos: a Python dictionary where each key is a field
            and each value is your new metadata.
            Example: ``{u'/Title': u'My title'}``
        N)rH   r~   r   Úadd_metadata©r8   r†   r9   r9   r:   r‡   j  s    
zPdfMerger.add_metadatac                 C   s   t ddƒ |  |¡ dS )zW
        .. deprecated:: 1.28.0

            Use :meth:`add_metadata` instead.
        ÚaddMetadatar‡   N)r   r‡   rˆ   r9   r9   r:   r‰   v  s    
zPdfMerger.addMetadata)Úlayoutr5   c                 C   s   t ddƒ |  |¡ dS )zZ
        .. deprecated:: 1.28.0

            Use :meth:`set_page_layout` instead.
        ÚsetPageLayoutÚset_page_layoutN)r   rŒ   ©r8   rŠ   r9   r9   r:   r‹     s    
zPdfMerger.setPageLayoutc                 C   s"   | j du rttƒ‚| j  |¡ dS )a  
        Set the page layout.

        :param str 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)rH   r~   r   Z_set_page_layoutr�   r9   r9   r:   rŒ   ˆ  s    
zPdfMerger.set_page_layout)Úmoder5   c                 C   s   t dddƒ |  |¡ dS )zX
        .. deprecated:: 1.28.0

            Use :meth:`set_page_mode` instead.
        ÚsetPageModeÚset_page_modeú3.0.0N)r   r�   ©r8   rŽ   r9   r9   r:   r�   ¤  s    zPdfMerger.setPageModec                 C   s"   | j du rttƒ‚| j  |¡ dS )aP  
        Set the page mode.

        :param str 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)rH   r~   r   r�   r’   r9   r9   r:   r�   ­  s    
zPdfMerger.set_page_mode)Úpdfrq   rG   r5   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 )zHRemove named destinations that are not a part of the specified page set.ú/Pageú/Title)	ra   rc   ri   ÚitemsrG   r€   r$   r   r^   )	r8   r“   rq   rG   Z	new_destsÚlstÚkeyÚobjÚjr9   r9   r:   rh   Ç  s    
zPdfMerger._trim_dests)r“   rB   rG   r5   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 )zJRemove outline item entries that are not a part of the specified page set.Tr   Fr”   N)	ra   rc   ri   Ú	enumeraterf   r^   rG   r€   r$   )r8   r“   rB   rG   Znew_outlineZprev_header_addedr—   rr   rR   Úsubrš   r9   r9   r:   rf   Ù  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”   )
rH   r~   r   rI   r›   rG   r4   r7   r$   Zadd_named_destination_object)r8   Ú
named_destÚpagenors   r9   r9   r:   r‚   ø  s    

zPdfMerger._write_dests)rB   Úparentr5   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)rH   r~   r   rB   ra   rc   rƒ   r›   rG   r4   Ú_write_outline_item_on_pageZadd_outline_item_dict)r8   rB   rŸ   Z
last_addedrR   Zpage_nors   r9   r9   r:   rƒ     s&    

zPdfMerger._write_outline)rT   )rR   rs   r5   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}| |tƒ ¡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/Zoomr   z/GoToz/A)r   ry   r&   r4   r$   r   ZFIT_Hr   ZTOPZFIT_BHZFIT_VÚLEFTZFIT_BVZXYZZFIT_RZBOTTOMÚRIGHTÚgetrd   ra   r%   r^   r"   r    r   ÚSÚDr   )r8   rR   rs   Zoi_typeÚargsZfit2arg_keysZarg_keyr9   r9   r:   r¢   "  s4    



üú
ÿþÿz%PdfMerger._write_outline_item_on_page)rG   r5   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•   ú')rI   ra   r&   r€   r2   r4   r\   r$   )r8   rG   r�   rž   Únprs   r9   r9   r:   rj   E  s    

ÿz#PdfMerger._associate_dests_to_pages)rG   rB   r5   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”   )	rB   ra   rc   rk   r&   r€   r2   r4   r$   )r8   rG   rB   rR   rž   Zoutline_item_pageÚpr9   r9   r:   rk   W  s     

z+PdfMerger._associate_outline_items_to_pages)rR   Úrootr5   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•   )rB   r›   ra   rc   Úfind_outline_itemr   r   r   )r8   rR   r¬   rr   Zoi_enumÚresr9   r9   r:   r­   p  s    
ÿþzPdfMerger.find_outline_itemc                 C   s   |   ||¡S )zZ
        .. deprecated:: 2.9.0
            Use :meth:`find_outline_item` instead.
        )r­   )r8   rR   r¬   r9   r9   r:   Úfind_bookmark‰  s    
zPdfMerger.find_bookmark)	ÚtitlerU   rŸ   ÚcolorÚboldÚitalicrg   Úpagenumr5   c	              
   C   s‚   |dur|durt dƒ‚|durDd}	d}
t |	› d|
› d�t¡ |}|du rTt dƒ‚| j}|du rjttƒ‚| |||d||||¡S )al  
        Add an outline item (commonly referred to as a "Bookmark") to this PDF file.

        :param str title: Title to use for this outline item.
        :param int page_number: Page number this outline item will point to.
        :param parent: A reference to a parent outline item to create nested
            outline items.
        :param tuple color: Color of the outline item's font as a red, green, blue tuple
            from 0.0 to 1.0
        :param bool bold: Outline item font is bold
        :param bool italic: Outline item font is italic
        :param Fit fit: The fit of the destination page.
        NzMThe argument pagenum of add_outline_item is deprecated. Use page_number only.r´   rU   úH is deprecated as an argument and will be removed in PyPDF2==4.0.0. Use rW   rX   )r\   rY   rZ   r[   rH   r~   r   Úadd_outline_item)r8   r°   rU   rŸ   r±   r²   r³   rg   r´   rl   rm   Úwriterr9   r9   r:   r¶   •  s:    ÿÿûøzPdfMerger.add_outline_itemú/Fit)	r°   r´   rŸ   r±   r²   r³   rg   r¨   r5   c           	      G   s*   t dddƒ |  ||||||t||d�¡S )zZ
        .. deprecated:: 1.28.0
            Use :meth:`add_outline_item` instead.
        ÚaddBookmarkr¶   r‘   ©Zfit_typeZfit_args©r   r¶   r!   ©	r8   r°   r´   rŸ   r±   r²   r³   rg   r¨   r9   r9   r:   r¹   Ì  s    
ùzPdfMerger.addBookmarkc           	      G   s*   t dddƒ |  ||||||t||d�¡S )zY
        .. deprecated:: 2.9.0
            Use :meth:`add_outline_item` instead.
        r¹   r¶   r‘   rº   r»   r¼   r9   r9   r:   Úadd_bookmarkæ  s    
ùzPdfMerger.add_bookmark)r°   r´   r5   c                 C   s   t dddƒ |  ||¡S )z_
        .. deprecated:: 1.28.0
            Use :meth:`add_named_destination` instead.
        ÚaddNamedDestinationÚadd_named_destinationr‘   )r   r¿   )r8   r°   r´   r9   r9   r:   r¾      s    ÿzPdfMerger.addNamedDestination)r°   rU   r´   r5   c                 C   s€   |dur|durt dƒ‚|durDd}d}t |› d|› d�t¡ |}|du rTt dƒ‚tt|ƒt|ƒtjdd	�ƒ}| j	 
|¡ dS )
z¢
        Add a destination to the output.

        :param str title: Title to use
        :param int page_number: Page number this destination points at.
        NzRThe argument pagenum of add_named_destination is deprecated. Use page_number only.r´   rU   rµ   rW   rX   i:  )Útop)r\   rY   rZ   r[   r   r(   r&   r!   Zfit_horizontallyrI   r^   )r8   r°   rU   r´   rl   rm   Údestr9   r9   r:   r¿   
  s,    ÿÿû
ýzPdfMerger.add_named_destination)FrC   )NNNNTN)NNT)NN)N)N)N)NNFFr¸   )NNFFr¸   )NN)Br<   r=   r>   r?   r   Úboolr   r   r   r;   rK   r   r   ÚBaseExceptionr   rQ   r@   r   ry   r+   ru   r   r   r   r]   r*   r
   r^   rO   rP   r   r   r‡   r‰   r-   r‹   rŒ   r/   r�   r�   rh   r.   rf   r‚   r	   r'   r)   rƒ   r   r1   r¢   rj   rk   r­   r¯   r   r#   Úfloatr!   r¶   r,   r0   r¹   r½   r¾   r¿   r9   r9   r9   r:   rA   X   s8   ÿþ
û
      ùøvþ#
   ù"ÿø" 				"û"û  ý
üþ" ÿþ ý

ü ý

ü÷ö;     øö     øö  üûrA   c                       s&   e Zd Zeeddœ‡ fdd„Z‡  ZS )ÚPdfFileMergerN)r¨   Úkwargsr5   c                    s>   t dddƒ d|vr(t|ƒdk r(d|d< tƒ j|i |¤Ž d S )NrÅ   rA   r‘   rD   r   T)r   r`   Úsuperr;   )r8   r¨   rÆ   ©Ú	__class__r9   r:   r;   0  s    zPdfFileMerger.__init__)r<   r=   r>   r   r;   Ú__classcell__r9   r9   rÈ   r:   rÅ   /  s   rÅ   )@rY   Úior   r   r   Úpathlibr   Útypesr   Útypingr   r   r	   r
   r   r   r   r   r   r_   r   Z_pager   r…   r   Ú_utilsr   r   r   r   Z_writerr   Ú	constantsr   r   r�   r   r   Zgenericr   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   Z	pageranger*   r+   r,   r-   r.   r/   r0   r   r1   rA   rÅ   r9   r9   r9   r:   Ú<module>   s0   ,<
     \