
    :Mc                     b    d Z ddlZddlZddlZddlZddlmZmZ ddl	m
Z
  G d de
          ZdS )z$Export to PDF via a headless browser    N)Booldefault   )HTMLExporterc                   N    e Zd ZdZdZ edd                              d          Z edd                              d          Zd	Z	 e
d
          d             Z e
d          d             Z edd                              d          Z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 pyppeteer 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	text/htmlfile_extensionc                     dS )N.html selfs    :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_default/   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                     	 ddl m} ddlm} n"# t          $ r}t          d          |d }~ww xY w| j        s |            st          d          |S )Nr   )launch)check_chromiumzgPyppeteer is not installed to support Web PDF conversion. Please install `nbconvert[webpdf]` to enable.zuNo suitable chromium executable found on the system. Please use '--allow-chromium-download' to allow downloading one.)	pyppeteerr   pyppeteer.utilr   ModuleNotFoundErrorRuntimeErrorallow_chromium_download)r   r   r   es       r   _check_launch_reqsz!WebPDFExporter._check_launch_reqsC   s    	((((((5555555" 	 	 	@  	
 + 	NN4D4D 	S   s    
.).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 pyppeteer.c                   K   	j         rdgng } 	                                ddd|           d {V }|                                 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           |                    |           d {V }|                                 d {V  |S )Nz--no-sandboxF)handleSIGINThandleSIGTERMhandleSIGHUPargsscreend   zfile://networkidle0)	waitUntilprintBackgroundTa  () => {
                    const rect = document.body.getBoundingClientRect();
                    return {
                    width: Math.ceil(rect.width) + 1,
                    height: Math.ceil(rect.height) + 1,
                    }
                }widthheighti@8  )r-   r.   )disable_sandboxr!   newPageemulateMediawaitForgotonamepaginateevaluateupdateminpdfclose)
	temp_filer'   browserpage
pdf_params
dimensionsr-   r.   pdf_datar   s
            r   mainz*WebPDFExporter.run_pyppeteer.<locals>.mainV   s     '+';CN##D5D3355"%eRV        G !********D##H---------,,s#########))6in66.)QQQQQQQQQ,,s#########+T2J=  $(==$ $      
 #7+#H-!!!$UH!5!5"%fh"7"7    "XXj11111111H--//!!!!!!!Or   r   F)suffixdeletezutf-8Nc                 z    t          j                    }t          j        |           |                    |           S N)asyncionew_event_loopset_event_looprun_until_complete)coroloops     r   run_coroutinez3WebPDFExporter.run_pyppeteer.<locals>.run_coroutine   s5    -//&t,,,..t444r   )
concurrentfuturesThreadPoolExecutortempfileNamedTemporaryFilewriteencodesubmitresultosunlinkr4   )r   htmlrA   poolr;   rL   r@   s   `      r   run_pyppeteerzWebPDFExporter.run_pyppeteerS   s4   $	 $	 $	 $	 $	L !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 )N	resourceszBuilding PDFzPDF successfully createdz.pdfoutput_extension)r!   superfrom_notebook_nodeloginforZ   )r   nbr\   kwrX   r@   	__class__s         r   r_   z!WebPDFExporter.from_notebook_node   s    !!!4%''4RSS9SPRSSin%%%%%d++0111 )/	$%""r   rE   )__name__
__module____qualname____doc__export_from_notebookr   tagr   r5   output_mimetyper   r   r   r/   r!   rZ   r_   __classcell__)rd   s   @r   r   r      sX         *"dc   
cc 
 t	 	 	 
cc  "OW   W_   d   
cc     > > >@# # # # # # # # # #r   r   )rh   rF   concurrent.futuresrM   rV   rP   	traitletsr   r   rX   r   r   r   r   r   <module>ro      s    * *
      				  # # # # # # # #      O# O# O# O# O#\ O# O# O# O# O#r   