o
    h]                     @   s<  d dl mZmZmZ d dl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 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(m)Z)m*Z* ddl+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3 dZ4G dd dZ5G dd dZ6G dd de6Z7dS )    )BytesIOFileIOIOBase)AnyDictIterableListOptionalTupleUnioncast   )
Encryption)
PageObject)	PdfReader)StrByteTypedeprecate_with_replacementstr_)	PdfWriter)GoToActionArguments)PagesAttributes)TypArgumentsTypFitArguments)ArrayObjectBookmarkDestinationDictionaryObjectFloatObjectIndirectObject
NameObject
NullObjectNumberObjectTextStringObject
TreeObject)	PageRangePageRangeSpec)FitType
LayoutTypeOutlinesType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)r-   r,   out_pagedatar.   )selfr,   r-   r.    r3   V/var/www/html/magazine_api/magazine_env/lib/python3.10/site-packages/PyPDF2/_merger.py__init__=   s   
z_MergedPage.__init__)__name__
__module____qualname____doc__r   r   intr5   r3   r3   r3   r4   r+   :   s    r+   c                   @   s|  e Zd ZdZdNdeddfddZ			dOd	ed
eee	f de
e de
e deddfddZd
eee	f deeee
e f fddZ			dOd
eee	f de
e dedeeeef eeeef f deddf
ddZd
eddfddZdPd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#d$Zd"eddfd%d&Zd'e	d(eeeeef f deeeef eeeef f de eeef  fd)d*Z!d'e	d+e"deeeef eeeef f de"fd,d-Z#dPd.d/Z$		dQd0e
e%e&  d1e
e' ddfd2d3Z(dee&e)f d4e*ddfd5d6Z+de e* ddfd7d8Z,	dRde e* d0e
e%e&  ddfd9d:Z-	dRdeeef d;e
e" de
e e  fd<d=Z.					>dSd?ed@ed1ede'e/f dAe
ee0e0e0f  dBedCedDe1dEe2de/fdFdGZ3					>dSd?ed@ed1ede'e/f dAe
ee0e0e0f  dBedCedDe1dEe2de/fdHdIZ4d?ed@eddfdJdKZ5d?ed@eddfdLdMZ6dS )T	PdfMergera  
    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``.
    Fstrictr/   Nc                 C   s0   g | _ g | _t | _g | _g | _d| _|| _d S )Nr   )inputspagesr   output	bookmarksnamed_destsid_countr<   )r2   r<   r3   r3   r4   r5   S   s   
zPdfMerger.__init__Tpositionfileobjbookmarkr>   import_bookmarksc                 C   sp  |  |\}}}t|| jd}	| j||	|f |dur||	_|du r+dt|	jf}nt|t	r9|
t|	j}n	t|tsBtdg }
g }|rR|	j}| |	||}|rltt|t| jttj}|  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 position: 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 bookmark: Optionally, you may specify a bookmark to be
            applied at the beginning of the included file by supplying the text
            of the bookmark.

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

        :param bool import_bookmarks: You may prevent the source document's
            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<   r=   append_encryptionlenr>   
isinstancer$   indicestuple	TypeErrorZoutlines_trim_outliner   r"   r!   rB   r   r   ZFITr@   Znamed_destinations_trim_destsrA   ranger+   _associate_dests_to_pages_associate_bookmarks_to_pages)r2   rC   rD   rE   r>   rF   streammy_fileencryption_objreaderZsrcpagesoutlineZbookmark_typdestsZtrimmed_destsipager.   mpr3   r3   r4   merge\   sH   




zPdfMerger.mergec                 C   s   d}d }t |trt|d}d}nBt |tr7|jr|j}|j }|jd t|j	 }|j| d}nt
|drQt
|drQ|d |	 }t|}d}n|}|||fS )NFrbTr   seekread)rK   strr   r   rI   rT   tellr_   r   r`   hasattr)r2   rD   rU   rV   rT   Z	orig_tellZfilecontentr3   r3   r4   rG      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 bookmark: Optionally, you may specify a bookmark to be
            applied at the beginning of the included file by supplying the text
            of the bookmark.

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

        :param bool import_bookmarks: You may prevent the source document's
            bookmarks from being imported by specifying this as ``False``.
        N)r]   rJ   r>   )r2   rD   rE   r>   rF   r3   r3   r4   rH      s   zPdfMerger.appendc                 C   s   | j du r	ttd}t|trt|d}d}| jD ]$}| j |j t	t
ttf | j j }| j |tj d  |_q|   |   | j | |rU|  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.
        NFwbT)r?   RuntimeErrorERR_CLOSED_WRITERrK   ra   r   r>   Zadd_pager,   r   r   r   Z_pages
get_objectZget_referencePAZKIDSr1   _write_dests_write_bookmarkswriteclose)r2   rD   rU   r[   Z	pages_objr3   r3   r4   rl      s$   



zPdfMerger.writec                 C   s4   g | _ | jD ]\}}}|r|  qg | _d| _dS )zHShut all file descriptors (input and output) and clear all memory usage.N)r>   r=   rm   r?   )r2   fo_readerZminer3   r3   r4   rm     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)r?   rf   rg   add_metadatar2   rp   r3   r3   r4   rr   $  s   
zPdfMerger.add_metadatac                 C      t dd | | dS )zW
        .. deprecated:: 1.28.0

            Use :meth:`add_metadata` instead.
        addMetadatarr   N)r   rr   rs   r3   r3   r4   ru   0     
zPdfMerger.addMetadatalayoutc                 C   rt   )zZ
        .. deprecated:: 1.28.0

            Use :meth:`set_page_layout` instead.
        setPageLayoutset_page_layoutN)r   ry   r2   rw   r3   r3   r4   rx   9  rv   zPdfMerger.setPageLayoutc                 C   rq   )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)r?   rf   rg   Z_set_page_layoutrz   r3   r3   r4   ry   B  s   
zPdfMerger.set_page_layoutmodec                 C   rt   )zX
        .. deprecated:: 1.28.0

            Use :meth:`set_page_mode` instead.
        setPageModeset_page_modeN)r   r}   r2   r{   r3   r3   r4   r|   ^  rv   zPdfMerger.setPageModec                 C   rq   )aR  
        Set the page mode.

        :param str mode: The page mode to use.

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

           * - /UseNone
             - Do not show outlines or thumbnails panels
           * - /UseOutlines
             - Show outlines (aka bookmarks) panel
           * - /UseThumbs
             - Show page thumbnails panel
           * - /FullScreen
             - Fullscreen view
           * - /UseOC
             - Show Optional Content Group (OCG) panel
           * - /UseAttachments
             - Show attachments panel
        N)r?   rf   rg   r}   r~   r3   r3   r4   r}   g  s   
zPdfMerger.set_page_modepdfrY   c                 C   s|   g }|  D ]5\}}t| D ],}|j|  |d  kr:|d  |td< t|t|d ks3J ||  nqq|S )zHRemove named destinations that are not a part of the specified page set./Page/Title)itemsrQ   r>   rh   r   r   rH   )r2   r   rY   r>   Z	new_destskeyobjjr3   r3   r4   rP     s   
zPdfMerger._trim_destsrX   c           
      C   s   g }d}t |D ]S\}}t|tr+| |||}|r*|s%|||d   || qd}t| D ])}	|d du r:q1|j|	  |d  krZ|d  |td< || d} nq1q|S )zNRemove outline/bookmark entries that are not a part of the specified page set.Tr   Fr   N)		enumeraterK   listrO   rH   rQ   r>   rh   r   )
r2   r   rX   r>   Znew_outlineZprev_header_addedrZ   osubr   r3   r3   r4   rO     s,   


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   )
r?   rf   rg   rA   r   r>   r.   r1   r   Zadd_named_destination_object)r2   Z
named_destpagenor[   r3   r3   r4   rj     s   

zPdfMerger._write_destsr@   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)r?   rf   rg   r@   rK   r   rk   r   r>   r.   _write_bookmark_on_pageZadd_bookmark_dict)r2   r@   r   Z
last_addedrE   Zpage_nor[   r3   r3   r4   rk     s,   

zPdfMerger._write_bookmarksr[   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   ra   r!   r.   r   r   FIT_Hr   ZTOPZFIT_BHZFIT_VZLEFTZFIT_BVZXYZZFIT_RZBOTTOMZRIGHTgetrM   rK   r    rH   r   r   r   SDr   )r2   rE   r[   Zbm_typeargsZfit2arg_keysZarg_keyr3   r3   r4   r     s0   



z!PdfMerger._write_bookmark_on_pagec                 C   sv   | j D ]5}d }|d }t|trq|D ]}| |j kr!|j}q|d ur/t||td< qtd|d  dd S )Nr   zUnresolved named destination 'r   ')rA   rK   r!   rh   r,   r.   r   
ValueError)r2   r>   ndr   nppr3   r3   r4   rR     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   )	r@   rK   r   rS   r!   rh   r,   r.   r   )r2   r>   r@   br   bpr   r3   r3   r4   rS     s&   

z'PdfMerger._associate_bookmarks_to_pagesrootc                 C   sl   |d u r| j }t|D ](\}}t|tr$| ||}|r#|g|   S q||ks.|d |kr3|g  S qd S )Nr   )r@   r   rK   r   find_bookmark)r2   rE   r   rZ   r   resr3   r3   r4   r      s   

zPdfMerger.find_bookmark/Fittitlepagenumcolorbolditalicfitr   c           	      G   s(   t dd | j|||||||g|R  S )zV
        .. deprecated:: 1.28.0
            Use :meth:`add_bookmark` instead.
        addBookmarkadd_bookmark)r   r   )	r2   r   r   r   r   r   r   r   r   r3   r3   r4   r   5  s   
zPdfMerger.addBookmarkc           
      G   s4   | j }	|	du rtt|	j|||||||g|R  S )aG  
        Add a bookmark to this PDF file.

        :param str title: Title to use for this bookmark.
        :param int pagenum: Page number this bookmark will point to.
        :param parent: A reference to a parent bookmark to create nested
            bookmarks.
        :param tuple color: Color of the bookmark as a red, green, blue tuple
            from 0.0 to 1.0
        :param bool bold: Bookmark is bold
        :param bool italic: Bookmark is italic
        :param str fit: The fit of the destination page. See
            :meth:`addLink()<addLink>` for details.
        N)r?   rf   rg   r   )
r2   r   r   r   r   r   r   r   r   writerr3   r3   r4   r   I  s   zPdfMerger.add_bookmarkc                 C   s   t dd | ||S )z_
        .. deprecated:: 1.28.0
            Use :meth:`add_named_destination` instead.
        addNamedDestinationadd_named_destination)r   r   )r2   r   r   r3   r3   r4   r   i  s   
zPdfMerger.addNamedDestinationc                 C   s0   t t|t|ttjtd}| j| dS )z
        Add a destination to the output.

        :param str title: Title to use
        :param int pagenum: Page number this destination points at.
        i:  N)r   r"   r!   r   r   r   rA   rH   )r2   r   r   destr3   r3   r4   r   q  s   zPdfMerger.add_named_destination)F)NNT)r/   N)NNr0   )NNFFr   )7r6   r7   r8   r9   boolr5   r:   r   r   r   r	   ra   r%   r]   r
   r   r   rG   r$   rH   rl   rm   r   r   rr   ru   r'   rx   ry   r)   r|   r}   r   rP   r(   rO   rj   r   r   r#   rk   r   r+   r   rR   rS   r   r   floatr&   r*   r   r   r   r   r3   r3   r3   r4   r;   D   s(   

V

)
 

$
			






 




	

	

 r;   c                       s*   e Zd Zdededdf fddZ  ZS )PdfFileMergerr   kwargsr/   Nc                    s<   t dd d|vrt|dk rd|d< t j|i | d S )Nr   ZPdfMerger<   r   T)r   rJ   superr5   )r2   r   r   	__class__r3   r4   r5     s   
zPdfFileMerger.__init__)r6   r7   r8   r   r5   __classcell__r3   r3   r   r4   r     s    "r   N)8ior   r   r   typingr   r   r   r   r	   r
   r   r   rI   r   Z_pager   ro   r   Z_utilsr   r   r   Z_writerr   	constantsr   r   ri   r   r   Zgenericr   r   r   r   r   r   r   r    r!   r"   r#   Z	pageranger$   r%   typesr&   r'   r(   r)   r*   rg   r+   r;   r   r3   r3   r3   r4   <module>   s*   (4
    A