o
    tf                     @   s   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
dduZejdkZG d	d
 d
eZdS )z$Export to PDF via a headless browser    N)util)Booldefault   )HTMLExporter
playwrightntc                       s   e Zd ZdZdZedddjddZedddjddZe	d	d
d Z
e	ddd ZedddjddZdd Zd fdd	Z  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
        Zfile_extensionc                 C      dS )N.html selfr   r   c/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/nbconvert/exporters/webpdf.py_file_extension_default/      z&WebPDFExporter._file_extension_defaultZtemplate_namec                 C   r   )NZwebpdfr   r   r   r   r   _template_name_default3   r   z%WebPDFExporter._template_name_defaulta  
        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              	      s    fdd}t j }tjddd}| ||d W d   n1 s'w   Y  zdd	 }|||| }W t	
|j |S t	
|j w )
zRun playwright.c              
      s   j rdgng }zddlm} W n ty$ } zd}t||d}~ww  jr5tjdddd	g}t	| | 
 I dH }|j}z|jd
d
d
|dI dH }W n tyh } zd}| I dH  t||d}~ww | I dH }	|	jddI dH  |	dI dH  |	jd| j ddI dH  |	dI dH  ddi}
 js|	dI dH }|d }|d }|
t|dt|dd |	jdi |
I dH }| I dH  | I dH  |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)Zhandle_sigintZhandle_sigtermZ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)Zmediad   zfile://Znetworkidle)Z
wait_untilZ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_sandboxZplaywright.async_apir   ModuleNotFoundErrorRuntimeErrorallow_chromium_downloadsys
executable
subprocess
check_callstartr   Zlaunch	ExceptionstopZnew_pageZemulate_mediaZwait_for_timeoutZgotonamepaginateevaluateupdateminZpdfclose)	temp_filer   r   emsgcmdr   r   ZbrowserpageZ
pdf_params
dimensionsr   r   pdf_datar   r   r   mainJ   s^   


	
	z+WebPDFExporter.run_playwright.<locals>.mainr   F)suffixdeletezutf-8Nc                 S   s(   t rt nt }t| || S )zRun an internal coroutine.)
IS_WINDOWSasyncioZProactorEventLoopnew_event_loopset_event_looprun_until_complete)coroloopr   r   r   run_coroutine   s   


z4WebPDFExporter.run_playwright.<locals>.run_coroutine)
concurrentfuturesThreadPoolExecutortempfileNamedTemporaryFilewriteencodesubmitresultosunlinkr(   )r   htmlr5   poolr.   r?   r4   r   r   r   run_playwrightG   s   
BzWebPDFExporter.run_playwrightNc                    sN   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.pdfZoutput_extension)superfrom_notebook_nodeloginforM   )r   nbrN   kwrK   r4   	__class__r   r   rP      s   
z!WebPDFExporter.from_notebook_node)N)__name__
__module____qualname____doc__Zexport_from_notebookr   tagr    r)   r   r   r   r   rM   rP   __classcell__r   r   rU   r   r	      s6    	

br	   )rZ   r9   concurrent.futuresr@   rI   r#   r!   rC   	importlibr   Zimportlib_utilZ	traitletsr   r   rK   r   	find_specZPLAYWRIGHT_INSTALLEDr(   r8   r	   r   r   r   r   <module>   s    
