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 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
        file_extensionc                 C      dS )N.html selfr   r   U/var/www/edux/Edux_v2/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.
        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)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   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asyncio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unlinkr8   )r   htmlrH   poolr?   rS   rG   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.pdfoutput_extension)superfrom_notebook_nodeloginfora   )r   nbrb   kwr_   rG   	__class__r   r   re      s   
z!WebPDFExporter.from_notebook_node)N)__name__
__module____qualname____doc__export_from_notebookr   tagr+   r9   r   r   r   r'   ra   re   __classcell__r   r   rj   r   r	      s6    	

br	   )ro   rL   concurrent.futuresrT   r]   r.   r,   rW   	importlibr   importlib_util	traitletsr   r   r_   r   	find_specPLAYWRIGHT_INSTALLEDr8   rK   r	   r   r   r   r   <module>   s    
