o
    ^i                     @   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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ListUnicode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ee 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
        file_extensionc                 C      dS )N.html selfr   r   U/home/ubuntu/hpml_nyu/venv/lib/python3.10/site-packages/nbconvert/exporters/webpdf.py_file_extension_default/      z&WebPDFExporter._file_extension_defaulttemplate_namec                 C   r   )N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.
        z
        Additional arguments to pass to the browser rendering to PDF.

        These arguments will be passed directly to the browser launch method
        and can be used to customize browser behavior beyond the default settings.
        c              	      s    fdd}t j }tjddd}| ||d W d   n1 s'w   Y  z|tj	||
 }W t|j |S t|j w )zRun playwright.c              
      s  zddl m} W n ty } zd}t||d}~ww  jr-tjddddg}t| | 	 I dH }|j
} j} jrC|d	 z|jd
d
d
|dI dH }W n tyk } 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.r   )async_playwrightzhPlaywright is not installed to support Web PDF conversion. Please install `nbconvert[webpdf]` to enable.Nz-mr	   installchromiumz--no-sandboxF)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   )playwright.async_apir   ModuleNotFoundErrorRuntimeErrorallow_chromium_downloadsys
executable
subprocess
check_callstartr   browser_argsdisable_sandboxappendlaunch	Exceptionstopnew_pageemulate_mediawait_for_timeoutgotonamepaginateevaluateupdateminpdfclose)	temp_filer   emsgcmdr	   r   r    browserpage
pdf_params
dimensionsr'   r(   pdf_datar   r   r   mainT   sb   



	
	z+WebPDFExporter.run_playwright.<locals>.mainr   F)suffixdeletezutf-8N)
concurrentfuturesThreadPoolExecutortempfileNamedTemporaryFilewriteencodesubmitasynciorunresultosunlinkr<   )r   htmlrL   poolrC   rK   r   r   r   run_playwrightQ   s   
H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.pdfoutput_extension)superfrom_notebook_nodeloginfor^   )r   nbr_   kwr\   rK   	__class__r   r   rb      s   
z!WebPDFExporter.from_notebook_node)N)__name__
__module____qualname____doc__export_from_notebookr   tagr,   r=   r   r   r   r3   r   r   r2   r^   rb   __classcell__r   r   rg   r   r      sB    	


]r   )rl   rW   concurrent.futuresrO   rZ   r/   r-   rR   	importlibr   importlib_util	traitletsr   r   r   r   r\   r   	find_specPLAYWRIGHT_INSTALLEDr<   
IS_WINDOWSr   r   r   r   r   <module>   s    
