o
    ë6Wj 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dedededdfdd„ZdS )	Ú_MergedPagez@Collect necessary information on each page that is being merged.ÚpagedataÚsrcÚidÚreturnNc                 C   s   || _ || _d | _|| _d S ©N)r3   r2   Úout_pagedatar4   )Úselfr2   r3   r4   © r9   ú]/home/esfera/Documents/content_generation/venv/lib/python3.10/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�	d]dedeeef dd	fd
d„ƒZ	d^dd„Z
deee  dee dee dd	fdd„Zeddd�											d_dee deeeef dee dee dedee dd	fdd„ƒZdeeeef deeee f fdd„Zeddd�					d`deeeef dee ded	eeeef eeeef ee f dedd	f
dd„ƒZdeeef dd	fd d!„Zdad"d#„Zd$eeef dd	fd%d&„Z d$eeef dd	fd'd(„Z!d)e"dd	fd*d+„Z#d)e"dd	fd,d-„Z$d.e%dd	fd/d0„Z&d.e%dd	fd1d2„Z'd3ed4eeeeef f deeeef eeeef ee f deeeef  fd5d6„Z(d3ede)deeeef eeeef ee f de)fd7d8„Z*dad9d:„Z+edd�				dbdee,e-  d;ee. dd	fd<d=„ƒZ/edd>�dee-e0f d?e1dd	fd@dA„ƒZ2dee1 dd	fdBdC„Z3edd�		dcdee1 dee,e-  dd	fdDdE„ƒZ4edd>�		dcdeeef dFee) deee  fdGdH„ƒZ5edd>�		dcdeeef dFee) deee  fdIdJ„ƒZ6d	d	d	dde7d	fdKedee d;ed	e.e8f dLeee9e9e9f  dMedNedOe:dPee de8fdQdR„Z;							SdddKedPed;ed	e.e8f dLeee9e9e9f  dMedNedOe<dTe=de8fdUdV„Z>							SdddKedPed;ed	e.e8f dLeee9e9e9f  dMedNedOe<dTe=de8fdWdX„Z?dKedPedd	fdYdZ„Z@				dbdKedee dPee dd	fd[d\„ZAd	S )eÚ	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)Ú	bookmarksFÚ ÚstrictÚfileobjr5   Nc                 C   s6   g | _ g | _tƒ | _g | _g | _d| _|| _|| _d S )Nr   )	ÚinputsÚpagesr   ÚoutputrB   Únamed_destsÚid_countrF   rE   )r8   rE   rF   r9   r9   r:   r;   i   s   
zPdfMerger.__init__c                 C   s   | S r6   r9   )r8   r9   r9   r:   Ú	__enter__v   s   zPdfMerger.__enter__Úexc_typeÚexcÚ	tracebackc                 C   s   | j r	|  | j ¡ |  ¡  dS )z*Write to the fileobj and close the merger.N)rF   ÚwriteÚclose)r8   rM   rN   rO   r9   r9   r:   Ú__exit__z   s   zPdfMerger.__exit__Úoutline_itemÚimport_outline)ÚbookmarkÚimport_bookmarksTÚpage_numberrH   Úpositionc                 C   sÔ  |dur|du r|}d}d}t  |› d|› d�t¡ ntdƒ‚|du r'tdƒ‚|du r/tdƒ‚|  |¡\}	}
t|	| jd	�}| j |	|f¡ |
durL|
|_	|du rXd
t
|jƒf}nt|tƒrf| t
|jƒ¡}nt|tƒrln	t|tƒsut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.

        :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``.
        NrX   rW   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)rE   r   z0"pages" must be a tuple of (start, stop[, step])r   )!ÚwarningsÚwarnÚDeprecationWarningÚ
ValueErrorÚ_create_streamr   rE   rG   ÚappendÚ_encryptionÚlenrH   Ú
isinstancer*   ÚindicesÚlistÚtupleÚ	TypeErrorrB   Ú_trim_outliner'   r(   r&   rK   r!   ÚfitÚnamed_destinationsÚ_trim_destsrJ   Úranger1   Ú_associate_dests_to_pagesÚ!_associate_outline_items_to_pages)r8   rW   rF   rS   rH   rT   rX   Úold_termÚnew_termÚstreamÚencryption_objÚreaderÚsrcpagesrB   Úoutline_item_typÚdestsÚtrimmed_destsÚiÚpager4   Úmpr9   r9   r:   Úmerge…   sp   #ÿûÿ


ý


zPdfMerger.mergec                 C   s°   d }t |ttfƒrt|dƒ}||fS t |tƒr9|jr|j}|j ¡ }|j d¡ t	|j 
¡ ƒ}|j |¡ ||fS t|dƒrTt|dƒrT| d¡ | 
¡ }t	|ƒ}||fS tdƒ‚)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.)rc   Ústrr   r   r   ra   rq   Útellr}   r   r~   ÚhasattrÚNotImplementedError)r8   rF   rr   rq   Ú	orig_tellÚfilecontentr9   r9   r:   r_   û   s(   	

ì
õ
úÿzPdfMerger._create_streamc                 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)r{   rb   rH   )r8   rF   rS   rH   rT   r9   r9   r:   r`     s   !zPdfMerger.appendc                 C   s–   | j du r	ttƒ‚| jD ]$}| j  |j¡ tttt	f | j j
 ¡ ƒ}| j  |tj d  ¡ ¡|_q|  ¡  |  ¡  | j  |¡\}}|rI| ¡  dS 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éÿÿÿÿ)rI   ÚRuntimeErrorÚERR_CLOSED_WRITERrH   Úadd_pager2   r   r   r   r   Ú_pagesÚ
get_objectÚget_referenceÚPAÚKIDSr7   Ú_write_destsÚ_write_outlinerP   rQ   )r8   rF   ry   Ú	pages_objÚmy_fileÚret_fileobjr9   r9   r:   rP   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)rH   rG   rQ   rI   )r8   ÚfoÚ_readerr9   r9   r:   rQ   a  s
   

zPdfMerger.closeÚinfosc                 C   ó"   | 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)rI   r†   r‡   Úadd_metadata©r8   r•   r9   r9   r:   r—   j  s   
zPdfMerger.add_metadatac                 C   ó   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  ó   
zPdfMerger.addMetadataÚlayoutc                 C   r™   )zZ
        .. deprecated:: 1.28.0

            Use :meth:`set_page_layout` instead.
        ÚsetPageLayoutÚset_page_layoutN)r   rž   ©r8   rœ   r9   r9   r:   r�     r›   zPdfMerger.setPageLayoutc                 C   r–   )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)rI   r†   r‡   Ú_set_page_layoutrŸ   r9   r9   r:   rž   ˆ  s   
zPdfMerger.set_page_layoutÚmodec                 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   r–   )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)rI   r†   r‡   r£   r¥   r9   r9   r:   r£   ­  s   
zPdfMerger.set_page_modeÚpdfrv   c           	      C   s’   g }t |tƒr	|ntt|Ž ƒ}| ¡ D ]3\}}|D ],}|j|  ¡ |d  ¡ krE|d  ¡ |tdƒ< t|ƒt|d ƒks>J ‚| |¡  nqq|S )zHRemove named destinations that are not a part of the specified page set.ú/Pageú/Title)	rc   re   rl   ÚitemsrH   rŠ   r$   r   r`   )	r8   r¦   rv   rH   Ú	new_destsÚlstÚkeyÚobjÚjr9   r9   r:   rk   Ç  s   
ü€zPdfMerger._trim_destsc                 C   sÒ   g }d}t |tƒr|ntt|Ž ƒ}t|ƒD ]Q\}}t |tƒr8|  |||¡}	|	r7|s2| ||d  ¡ | |	¡ qd}|D ])}
|d du rEq<|j|
  ¡ |d  ¡ kre|d  ¡ |tdƒ< | |¡ d} nq<q|S )zJRemove outline item entries that are not a part of the specified page set.Tr   Fr§   N)	rc   re   rl   Ú	enumeraterh   r`   rH   rŠ   r$   )r8   r¦   rB   rH   Únew_outlineÚprev_header_addedr«   rx   rS   Úsubr®   r9   r9   r:   rh   Ù  s6   

€ÿ
þ
ú€zPdfMerger._trim_outlinec                 C   sv   | j d u r	ttƒ‚| jD ],}d }d|v r.t| jƒD ]\}}|j|d kr-|j|tdƒ<  nq|d ur8| j  	|¡ qd S )Nr§   )
rI   r†   r‡   rJ   r¯   rH   r4   r7   r$   Úadd_named_destination_object)r8   Ú
named_destÚpagenory   r9   r9   r:   rŽ   ø  s   

þ€÷zPdfMerger._write_destsÚparentc                 C   sº   | j d u r	ttƒ‚|d u r| j}|d usJ dƒ‚d }|D ]>}t|tƒr*|  ||¡ qd }d|v rIt| jƒD ]\}}|j	|d krH|  
||¡  nq5|d urZ|d= |d= | j  ||¡}qd S )Núhint for mypyr§   ú/Type)rI   r†   r‡   rB   rc   re   r�   r¯   rH   r4   Ú_write_outline_item_on_pageÚadd_outline_item_dict)r8   rB   r¶   Ú
last_addedrS   Úpage_nory   r9   r9   r:   r�     s,   

þ€ózPdfMerger._write_outline)rU   ry   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 ]!}||v rWt|| tƒsW| 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   r   r&   r4   r$   r   ÚFIT_Hr   ÚTOPÚFIT_BHÚFIT_VÚLEFTÚFIT_BVÚXYZÚFIT_RÚBOTTOMÚRIGHTÚgetrf   rc   r%   r`   r"   r    r   ÚSÚDr   )r8   rS   ry   Úoi_typeÚargsÚfit2arg_keysÚarg_keyr9   r9   r:   r¹   "  s4   



üú
ÿþÿz%PdfMerger._write_outline_item_on_pagec                 C   sv   | j D ]5}d }|d }t|tƒrq|D ]}| ¡ |j ¡ kr!|j}q|d u r0td|d › d�ƒ‚t|ƒ|tdƒ< qd S )Nr§   zUnresolved named destination 'r¨   ú')rJ   rc   r&   rŠ   r2   r4   r^   r$   )r8   rH   r´   rµ   Únpry   r9   r9   r:   rm   E  s   

€ÿñz#PdfMerger._associate_dests_to_pagesc                 C   s–   |d u r| j }|d usJ dƒ‚|D ]7}t|tƒr|  ||¡ qd }|d }t|tƒr+q|D ]}| ¡ |j ¡ kr;|j}q-|d urHt|ƒ|tdƒ< qd S )Nr·   r§   )	rB   rc   re   rn   r&   rŠ   r2   r4   r$   )r8   rH   rB   rS   rµ   Úoutline_item_pageÚpr9   r9   r:   rn   W  s&   

€€ðz+PdfMerger._associate_outline_items_to_pagesÚrootc                 C   sz   |d u r| j }t|ƒD ]/\}}t|tƒr$|  ||¡}|r#|g|   S q||ks5ttttf |d ƒ|kr:|g  S qd S )Nr¨   )rB   r¯   rc   re   Úfind_outline_itemr   r   r   )r8   rS   rÒ   rx   Ú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   rS   rÒ   r9   r9   r:   Úfind_bookmark‰  s   
zPdfMerger.find_bookmarkÚtitleÚcolorÚboldÚitalicri   Úpagenumc	              
   C   s‚   |dur|durt dƒ‚|dur"d}	d}
t |	› d|
› d�t¡ |}|du r*t dƒ‚| j}|du r5t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Û   rW   úH is deprecated as an argument and will be removed in PyPDF2==4.0.0. Use rY   rZ   )r^   r[   r\   r]   rI   r†   r‡   Úadd_outline_item)r8   r×   rW   r¶   rØ   rÙ   rÚ   ri   rÛ   ro   rp   Úwriterr9   r9   r:   rÝ   •  s:   ÿÿûøzPdfMerger.add_outline_itemú/FitrË   c           	      G   ó*   t dddƒ |  ||||||t||d�¡S )zZ
        .. deprecated:: 1.28.0
            Use :meth:`add_outline_item` instead.
        ÚaddBookmarkrÝ   r¤   ©Úfit_typeÚfit_args©r   rÝ   r!   ©	r8   r×   rÛ   r¶   rØ   rÙ   rÚ   ri   rË   r9   r9   r:   rá   Ì  ó   
ùzPdfMerger.addBookmarkc           	      G   rà   )zY
        .. deprecated:: 2.9.0
            Use :meth:`add_outline_item` instead.
        rá   rÝ   r¤   râ   rå   ræ   r9   r9   r:   Úadd_bookmarkæ  rç   zPdfMerger.add_bookmarkc                 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.addNamedDestinationc                 C   s€   |dur|durt dƒ‚|dur"d}d}t |› d|› d�t¡ |}|du r*t 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Û   rW   rÜ   rY   rZ   i:  )Útop)r^   r[   r\   r]   r   r(   r&   r!   Úfit_horizontallyrJ   r`   )r8   r×   rW   rÛ   ro   rp   Údestr9   r9   r:   rê   
  s,   ÿÿû
ýzPdfMerger.add_named_destination)FrD   )r5   rA   )NNNNTN)NNT)r5   N)NNr6   )NNFFrß   )Br<   r=   r>   r?   r   Úboolr   r   r   r;   rL   r   r   ÚBaseExceptionr   rR   r@   r   r   r+   r{   r   r   r   r_   r*   r
   r`   rP   rQ   r   r   r—   rš   r-   r�   rž   r/   r¢   r£   rk   r.   rh   rŽ   r	   r'   r)   r�   r   r1   r¹   rm   rn   rÓ   rÖ   r   r#   Úfloatr!   rÝ   r,   r0   rá   rè   ré   rê   r9   r9   r9   r:   rA   X   sÀ   ÿÿ
ÿþ

þýü
û
ùþýüûúùøuÿ
þ
#ùþý"ÿüùø"
 				þý"ü
ûþý"ü
û
ý
þýü
ÿÿþ"ÿÿ
ÿþý
þý
üý
þý
ü÷þýüûúùø	÷

ö;øþýüûúùø	÷

öøþýüûúùø	÷

öüþýüûrA   c                       s*   e Zd Zdededdf‡ fdd„Z‡  ZS )ÚPdfFileMergerrË   Úkwargsr5   Nc                    s>   t dddƒ d|vrt|ƒdk rd|d< tƒ j|i |¤Ž d S )Nrñ   rA   r¤   rE   r   T)r   rb   Ú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ñ   )@r[   Úior   r   r   Úpathlibr   Útypesr   Útypingr   r   r	   r
   r   r   r   r   r   ra   r   Ú_pager   r”   r   Ú_utilsr   r   r   r   Ú_writerr   Ú	constantsr   r   rŒ   r   r   Úgenericr   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   Ú	pageranger*   r+   r,   r-   r.   r/   r0   r‡   r1   rA   rñ   r9   r9   r9   r:   Ú<module>   s2   ,<
     \