
    Mh                         d Z ddlZddlZddlZddlZddlZddlZddlm	Z
 ddlmZmZ ddlmZ  e
j        d          duZej        dk    Z G d	 d
e          ZdS )z$Export to PDF via a headless browser    N)util)Booldefault   )HTMLExporter
playwrightntc                   D    e Zd ZdZdZ edd                              d          Z edd                              d          Z e	d	          d
             Z
 e	d          d             Z edd                              d          Zd Zd fd	Z xZS )WebPDFExporterzWriter designed to write to PDF files.

    This inherits from :class:`HTMLExporter`. It creates the HTML using the
    template machinery, and then run playwright to create a pdf.
    zPDF via HTMLFzTWhether to allow downloading Chromium if no suitable version is found on the system.)helpT)configz
        Split generated notebook into multiple pages.

        If False, a PDF with one long page will be generated.

        Set to True to match behavior of LaTeX based PDF generator
        file_extensionc                     dS )N.html selfs    Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/nbconvert/exporters/webpdf.py_file_extension_defaultz&WebPDFExporter._file_extension_default/   s    w    template_namec                     dS )Nwebpdfr   r   s    r   _template_name_defaultz%WebPDFExporter._template_name_default3   s    xr   a  
        Disable chromium security sandbox when converting to PDF.

        WARNING: This could cause arbitrary code execution in specific circumstances,
        where JS in your notebook can execute serverside code! Please use with
        caution.

        ``https://github.com/puppeteer/puppeteer/blob/main@%7B2020-12-14T17:22:24Z%7D/docs/troubleshooting.md#setting-up-chrome-linux-sandbox``
        has more information.

        This is required for webpdf to work inside most container environments.
        c                      fd}t           j                                        }t          j        dd          }|5  |                    |                    d                     ddd           n# 1 swxY w Y   	 d }|                    | ||                                                    }t          j
        |j                   n# t          j
        |j                   w xY w|S )zRun playwright.c                 h  K   j         rdgng }	 ddlm} n$# t          $ r}d}t	          |          |d}~ww xY wj        r%t          j        dddd	g}t          j	        |            |            
                                 d{V }|j        }	 |                    d
d
d
|           d{V }n># t          $ r1}d}|                                 d{V  t	          |          |d}~ww xY w|                                 d{V }	|	                    d           d{V  |	                    d           d{V  |	                    d| j         d           d{V  |	                    d           d{V  ddi}
j        s_|	                    d           d{V }|d         }|d         }|
                    t/          |d          t/          |d          d            |	j        di |
 d{V }|                                 d{V  |                                 d{V  |S )zRun main playwright script.z--no-sandboxr   )async_playwrightzhPlaywright is not installed to support Web PDF conversion. Please install `nbconvert[webpdf]` to enable.Nz-mr   installchromiumF)handle_siginthandle_sigtermhandle_sighupargszNo suitable chromium executable found on the system. Please use '--allow-chromium-download' to allow downloading one,or install it using `playwright install chromium`.print)mediad   zfile://networkidle)
wait_untilprint_backgroundTa  () => {
                    const rect = document.body.getBoundingClientRect();
                    return {
                    width: Math.ceil(rect.width) + 1,
                    height: Math.ceil(rect.height) + 1,
                    }
                }widthheighti@8  )r*   r+   r   )disable_sandboxplaywright.async_apir   ModuleNotFoundErrorRuntimeErrorallow_chromium_downloadsys
executable
subprocess
check_callstartr   launch	Exceptionstopnew_pageemulate_mediawait_for_timeoutgotonamepaginateevaluateupdateminpdfclose)	temp_filer#   r   emsgcmdr   r   browserpage
pdf_params
dimensionsr*   r+   pdf_datar   s                  r   mainz+WebPDFExporter.run_playwright.<locals>.mainJ   sZ     '+';CN##D/AAAAAAA& / / /D  #3''Q./ + +~t\9jQ%c***//117799999999J!*H/ ("'UY] !0 ! !        / / /I 
 !oo'''''''''"3''Q./ !))++++++++D$$7$333333333'',,,,,,,,,))6in66=)QQQQQQQQQ'',,,,,,,,,,d3J=  $(==$ $      
 #7+#H-!!!$UH!5!5"%fh"7"7    &TX33
33333333H--//!!!!!!!//#########Os(    
949B2 2
C-<,C((C-r   F)suffixdeletezutf-8Nc                     t           rt          j                    nt          j                    }t          j        |           |                    |           S )zRun an internal coroutine.)
IS_WINDOWSasyncioProactorEventLoopnew_event_loopset_event_looprun_until_complete)coroloops     r   run_coroutinez4WebPDFExporter.run_playwright.<locals>.run_coroutine   sR     "2G-/// /11  &t,,,..t444r   )
concurrentfuturesThreadPoolExecutortempfileNamedTemporaryFilewriteencodesubmitresultosunlinkr=   )r   htmlrM   poolrD   rY   rL   s   `      r   run_playwrightzWebPDFExporter.run_playwrightG   s9   @	 @	 @	 @	 @	D !4466 /wuMMM	 	2 	2OODKK00111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2	&	5 	5 	5 {{=$$y//BBIIKKH Iin%%%%BIin%%%%s   )A22A69A6>4C C'Nc                      t                      j        |fd|i|\  }}| j                            d           |                     |          }| j                            d           d|d<   ||fS )zConvert from a notebook node.	resourceszBuilding PDFzPDF successfully createdz.pdfoutput_extension)superfrom_notebook_nodeloginforg   )r   nbri   kwre   rL   	__class__s         r   rl   z!WebPDFExporter.from_notebook_node   s    4%''4RSS9SPRSSin%%%&&t,,0111 )/	$%""r   )N)__name__
__module____qualname____doc__export_from_notebookr   tagr0   r>   r   r   r   r,   rg   rl   __classcell__)rq   s   @r   r   r      sG         *"dc   
cc 
 t	 	 	 
cc  W   W_   d   
cc  ` ` `D# # # # # # # # # #r   r   )ru   rR   concurrent.futuresrZ   rc   r3   r1   r]   	importlibr   importlib_util	traitletsr   r   re   r   	find_specPLAYWRIGHT_INSTALLEDr=   rQ   r   r   r   r   <module>r      s    * *
      				     



  , , , , , , # # # # # # # #      /~/==TI W_
_# _# _# _# _#\ _# _# _# _# _#r   