
    b                     Z    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y)z$Export to PDF via a headless browser    N)Booldefault   )HTMLExporterc                       e Zd ZdZdZ edd      j                  d      Z edd      j                  d      Zd	Z	 e
d
      d        Z e
d      d        Z edd      j                  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                      y)N.html selfs    </usr/lib/python3/dist-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_default/   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                     	 ddl m} ddlm} | j                  s |       st        d      |S # t        $ r}t        d      |d }~ww xY w)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   sa    	(5 ++N4DS   # 	@ 	s   . 	AAA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 pyppeteer.c                   K   	j                   rdgng } 	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       |j                  |       d {   }|j                          d {    |S 7 7 	7 7 7 7 7 w7 .7 w)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   sc    '+';';N#D5D335"%eRV G !**D##H---,,s###))ginn%56.)QQQ,,s###+T2J== $(==$ 
 #7+#H-!!!$UH!5"%fh"7 "XXj11H--/!!OE +-#Q#$ 2!s   /E$EE$
EE$#E$E$<E='E$$E%E$=E>(E$&E'A
E$1E 2E$	E"
E$E$E$E$E$E$E$ E$"E$r   F)suffixdeletezutf-8Nc                 v    t        j                         }t        j                  |       |j                  |       S N)asyncionew_event_loopset_event_looprun_until_complete)coroloops     r   run_coroutinez3WebPDFExporter.run_pyppeteer.<locals>.run_coroutine   s/    --/&&t,..t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   s    $	L !!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                     | j                          t        |   |fd|i|\  }}| j                  j	                  d       | j                  |      }| j                  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   sv    !'4RS9SPRSin%%%d+0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      s     *"c 
cc 
 	 
cc  "O  _   
cc   >@# #r   r   )rh   rF   concurrent.futuresrM   rV   rP   	traitletsr   r   rX   r   r   r   r   r   <module>ro      s*    *
   	  # O#\ O#r   