o
    >ez_                     @   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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)r0   r/   out_pagedatar1   )selfr/   r0   r1    r6   N/var/www/html/humari/django-venv/lib/python3.10/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	dKdedeeef ddfdd	ZdLd
dZ	de
ee  de
e de
e ddfddZ			dMdedeeeef de
e de
e deddfddZdeeeef deee
e f fddZ			dMd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dNd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#e$de
e" fd)d*Z%e%j&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 ee f deeeef  fd.d/Z'd,ed0e(deeeef eeeef ee f de(fd1d2Z)dNd3d4Z*		dOd0e
e+e,  d5e
e- ddfd6d7Z.dee,e/f d8e0ddfd9d:Z1dee0 ddfd;d<Z2	dPdee0 d0e
e+e,  ddfd=d>Z3	dPdeeef d?e
e( de
ee  fd@dAZ4dddde5fdBeded5ede-e6f dCe
ee7e7e7f  dDedEedFe8de6fdGdHZ9dBededdfdIdJZ:dS )Q	PdfMergerzD
    Use :class:`PdfWriter` instead.

    .. deprecated:: 5.0.0
    F strictfileobjr2   Nc                 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__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	tracebackc                 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_numberoutline_itemrD   import_outlinec                 C   sv  |  |\}}t|| jd}| j||f |dur||_|du r)dt|jf}nt|t	r7|
t|j}nt|tr=n	t|tsFtdg }	g }
|rV|j}
| ||
|}
|rot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    fitnamed_destinations_trim_destsrG   ranger.   _associate_dests_to_pages!_associate_outline_items_to_pages)r5   rQ   rA   rR   rD   rS   streamencryption_objreadersrcpagesrF   outline_item_typdeststrimmed_destsipager1   mpr6   r6   r7   mergey   sL   





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readzPdfMerger.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   rd   tellrp   r   rq   hasattrNotImplementedError)r5   rA   re   rd   	orig_tellfile_contentr6   r6   r7   rT      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.

        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)rn   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 ]$}| 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.

        Args:
            fileobj: Output file. Can be a filename or any kind of
                file-like object.
        N)rE   RuntimeErrorERR_CLOSED_WRITERrD   add_pager/   r   r   rr   r   _pages
get_objectget_referencePAKIDSr4   _write_dests_write_outlinerN   rO   )r5   rA   rl   	pages_objmy_file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   fo_readerr6   r6   r7   rO   2  s
   

zPdfMerger.closeinfosc                 C   "   | 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   ry   rz   add_metadata)r5   r   r6   r6   r7   r   ;  s   
	zPdfMerger.add_metadatalayoutc                 C   r   )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   ry   rz   _set_page_layout)r5   r   r6   r6   r7   set_page_layoutH  s   
zPdfMerger.set_page_layoutmodec                 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   ry   rz   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   
pdfri   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 )z
        Remove named destinations that are not a part of the specified page set.

        Args:
            pdf:
            dests:
            pages:
        /Page/Title)	rX   rZ   ra   itemsrD   r}   r#   r   rU   )	r5   r   ri   rD   	new_destslstkeyobjjr6   r6   r7   r`     s   
zPdfMerger._trim_destsrF   c                 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 )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   ra   	enumerater]   rU   rD   r}   r#   )r5   r   rF   rD   new_outlineprev_header_addedr   rk   rR   subr   r6   r6   r7   r]     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   )
rE   ry   rz   rG   r   rD   r1   r4   r#   add_named_destination_object)r5   
named_dest
page_indexrl   r6   r6   r7   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)rE   ry   rz   rF   rX   rZ   r   r   rD   r1   _write_outline_item_on_pageadd_outline_item_dict)r5   rF   r   
last_addedrR   page_norl   r6   r6   r7   r     s,   

zPdfMerger._write_outlinerl   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 ]!}||v rVt|| tsV|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   rr   r%   r1   r#   r   FIT_Hr   TOPFIT_BHFIT_VLEFTFIT_BVXYZFIT_RBOTTOMRIGHTgetrX   r$   rU   r!   r   r   SDr   )r5   rR   rl   oi_typeargsfit2arg_keysarg_keyr6   r6   r7   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   ')rG   rX   r%   r}   r/   r1   
ValueErrorr#   )r5   rD   r   r   nprl   r6   r6   r7   rb   -  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   )	rF   rX   rZ   rc   r%   r}   r/   r1   r#   )r5   rD   rF   rR   r   outline_item_pagepr6   r6   r7   rc   ?  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   )rF   r   rX   rZ   find_outline_itemr   r   r   )r5   rR   r   rk   oi_enumresr6   r6   r7   r   W  s   

zPdfMerger.find_outline_itemtitlecolorbolditalicr^   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   ry   rz   add_outline_item)	r5   r   rQ   r   r   r   r   r^   writerr6   r6   r7   r   o  s   zPdfMerger.add_outline_itemc                 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    fit_horizontallyrG   rU   )r5   r   rQ   destr6   r6   r7   add_named_destination  s   
zPdfMerger.add_named_destination)Fr?   )r2   r>   )NNT)r2   N)NNr3   );r9   r:   r;   r<   boolr   r   r   r8   rJ   r   r   BaseExceptionr   rP   r=   r   rr   r*   rn   r   r   r   rT   r)   r
   rU   rN   rO   r   r   r   r+   r   r-   r   propertyr   setterr`   r,   r]   r   r	   r&   r(   r   r   r.   r   rb   rc   r   r   r"   floatr    r   r   r6   r6   r6   r7   r>   U   s&   





W
&"
 
	"
"

)



"




	
&r>   N);ior   r   r   pathlibr   typesr   typingr   r   r	   r
   r   r   r   r   r   rV   r   _pager   r   r   _utilsr   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-   rz   r.   r>   r6   r6   r6   r7   <module>   s"   ,<
