
    f                         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
j                   d      duZej$                  dk(  Z G d	 d
e      Zy)z$Export to PDF via a headless browser    N)util)Booldefault   )HTMLExporter
playwrightntc                       e Zd ZdZdZ edd      j                  d      Z edd      j                  d      Z e	d	      d
        Z
 e	d      d        Z edd      j                  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 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                      y)N.html selfs    U/var/www/cvtools/html/venv/lib/python3.12/site-packages/nbconvert/exporters/webpdf.py_file_extension_defaultz&WebPDFExporter._file_extension_default/   s        template_namec                      y)Nwebpdfr   r   s    r   _template_name_defaultz%WebPDFExporter._template_name_default3   s    r   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                      fd}t         j                  j                         }t        j                  dd      }|5  |j                  |j                  d             ddd       	 d }|j                  | ||            j                         }t        j                  |j                         |S # 1 sw Y   TxY w# t        j                  |j                         w xY w)zRun playwright.c                   K   j                   rdgng }	 ddlm} j
                  r*t        j                  dddd	g}t        j                  |        |       j                          d{   }|j                  }	 |j                  d
d
d
|       d{   }|j                          d{   }	|	j!                  d       d{    |	j#                  d       d{    |	j%                  d| j&                   d       d{    |	j#                  d       d{    ddi}
j(                  sK|	j+                  d       d{   }|d   }|d   }|
j-                  t/        |d      t/        |d      d        |	j0                  di |
 d{   }|j3                          d{    |j                          d{    |S # t        $ r}d}t	        |      |d}~ww xY w7 7 Z# t        $ r,}d}|j                          d{  7   t	        |      |d}~ww xY w7 }7 f7 P7 +7 7 7 7 7 xw)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   s                  r   mainz+WebPDFExporter.run_playwright.<locals>.mainJ   sI    '+';';N#D/A ++~~t\9jQ%%c*/17799J!**H/ ("'UY] !0 !  !))++D$$7$333'',,,))ginn%56=)QQQ'',,,,d3J== $(==$ 
 #7+#H-!!!$UH!5"%fh"7 &TXX3
33H--/!!//###Ow ' /D  #3'Q./ :  /I 
 !oo'''"3'Q./ ,3,Q,$ 4!#s  I G AI *G++I <G1 G.G1 I ,H)-I H,I H/ 'I H2I  H5!(I 	H8
AI H:I -H<.I H>I 	G(G##G((I .G1 1	H&:H!HH!!H&&I ,I /I 2I 5I 8I :I <I >I r   F)suffixdeletezutf-8Nc                     t         rt        j                         nt        j                         }t        j                  |       |j                  |       S )zRun an internal coroutine.)
IS_WINDOWSasyncioProactorEventLoopnew_event_loopset_event_looprun_until_complete)coroloops     r   run_coroutinez4WebPDFExporter.run_playwright.<locals>.run_coroutine   sG     " --/ //1  &&t,..t44r   )
concurrentfuturesThreadPoolExecutortempfileNamedTemporaryFilewriteencodesubmitresultosunlinkr=   )r   htmlrM   poolrD   rY   rL   s   `      r   run_playwrightzWebPDFExporter.run_playwrightG   s    @	D !!446 //wuM	 	2OODKK01	2	&	5 {{=$y/BIIKH IIinn%)	2 	2& IIinn%s   !B2()B> 2B;>!Cc                     t        |   |fd|i|\  }}| j                  j                  d       | j	                  |      }| j                  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inforg   )r   nbri   kwre   rL   	__class__s         r   rl   z!WebPDFExporter.from_notebook_node   sj    '4RS9SPRSin%&&t,01 )/	$%""r   )N)__name__
__module____qualname____doc__export_from_notebookr   tagr0   r>   r   r   r   r,   rg   rl   __classcell__)rq   s   @r   r   r      s     *"c 
cc 
 	 
cc    _   
cc  `D# #r   r   )ru   rR   concurrent.futuresrZ   rc   r3   r1   r]   	importlibr   importlib_util	traitletsr   r   re   r   	find_specPLAYWRIGHT_INSTALLEDr=   rQ   r   r   r   r   <module>r      sX    *
   	  
  , # /~//=TI WW_
_#\ _#r   