a
    ø£BeÄ  ã                   @   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 )ÚWebPDFExporterz¹Writer 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   s   dS )Nú.html© ©Úselfr   r   ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/nbconvert/exporters/webpdf.pyÚ_file_extension_default/   s    z&WebPDFExporter._file_extension_defaultZtemplate_namec                 C   s   dS )NZwebpdfr   r   r   r   r   Ú_template_name_default3   s    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 sN0    Y  z,dd	„ }| |||ƒ¡ ¡ }W t	 
|j¡ nt	 
|j¡ 0 |S )
zRun playwright.c              
   “   sª  ˆ j rdgng }zddlm} W n2 tyR } zd}t|ƒ|‚W Y d}~n
d}~0 0 ˆ jrttjdddd	g}t 	|¡ |ƒ  
¡ I dH }|j}z|jd
d
d
|d�I dH }W n2 tyØ } zd}t|ƒ|‚W Y d}~n
d}~0 0 | ¡ 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f i |
¤Ž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Úargsz§No 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mediaéd   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   )Údisable_sandboxZplaywright.async_apir   ÚModuleNotFoundErrorÚRuntimeErrorÚallow_chromium_downloadÚsysÚ
executableÚ
subprocessÚ
check_callÚstartr   ZlaunchÚ	ExceptionZ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   sP    ÿ 
ÿÿ ÿ
	þÿ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   Úhtmlr2   Úpoolr+   r<   r1   r   r   r   Úrun_playwrightG   s    @
.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ÚinforJ   )r   ÚnbrK   ÚkwrH   r1   ©Ú	__class__r   r   rM   §   s    
z!WebPDFExporter.from_notebook_node)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zexport_from_notebookr   Útagr   r&   r   r   r   r   rJ   rM   Ú__classcell__r   r   rR   r   r	      s4   þýþ	÷

þò`r	   )rW   r6   Úconcurrent.futuresr=   rF   r!   r   r@   Ú	importlibr   Zimportlib_utilZ	traitletsr   r   rH   r   Ú	find_specZPLAYWRIGHT_INSTALLEDr%   r5   r	   r   r   r   r   Ú<module>   s   
