o
    ‡Øh  ã                   @   s`   d dl mZ d dlZd dlZd dlmZ d dlmZ e e¡Z	de
de
deee
f fdd	„ZdS )
é    )ÚTupleN)Úpisa)ÚconfigÚmarkdown_contentÚ
page_titleÚreturnc              
   C   sú   z]t  d|› �¡ tjg d¢d�}| | ¡}d|› d|› d�}t ¡ }tj||d�}|j	r:t  
d|j	› �¡ td	ƒ‚| ¡ }| ¡  d
dlm} ||ƒ}	|	› d�}
t  d|
› �¡ ||
fW S  ty| } zt  
dt|ƒ› �¡ tdt|ƒ› �ƒ‚d}~ww )aV  
    Convert Markdown content to PDF format.
    
    Args:
        markdown_content: The Markdown content to convert
        page_title: The title of the page for the PDF document
        
    Returns:
        Tuple[bytes, str]: PDF content as bytes and suggested filename
        
    Raises:
        Exception: If PDF conversion fails
    z%Converting markdown to PDF for page: )ÚtablesÚfenced_codeÚtoc)Ú
extensionszm
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset="UTF-8">
            <title>a‚  </title>
            <style>
                body {
                    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
                    line-height: 1.6;
                    margin: 40px;
                    color: #333;
                }
                h1, h2, h3, h4, h5, h6 {
                    color: #2c3e50;
                    margin-top: 24px;
                    margin-bottom: 16px;
                }
                h1 {
                    border-bottom: 2px solid #eaecef;
                    padding-bottom: 10px;
                }
                code {
                    background-color: #f6f8fa;
                    padding: 2px 4px;
                    border-radius: 3px;
                    font-family: 'Consolas', 'Monaco', monospace;
                }
                pre {
                    background-color: #f6f8fa;
                    padding: 16px;
                    border-radius: 6px;
                    overflow-x: auto;
                    font-family: 'Consolas', 'Monaco', monospace;
                }
                blockquote {
                    border-left: 4px solid #dfe2e5;
                    padding-left: 16px;
                    margin-left: 0;
                    color: #6a737d;
                }
                table {
                    border-collapse: collapse;
                    width: 100%;
                    margin: 12px 0;
                    table-layout: fixed;
                    word-wrap: break-word;
                    overflow-wrap: break-word;
                    font-size: 10px;
                }
                th, td {
                    border: 1px solid #dfe2e5;
                    padding: 4px 6px;
                    text-align: left;
                    word-wrap: break-word;
                    overflow-wrap: break-word;
                    hyphens: auto;
                    font-size: 9px;
                    line-height: 1.2;
                    vertical-align: top;
                }
                th {
                    background-color: #f6f8fa;
                    font-weight: bold;
                    font-size: 8px;
                }
                /* Dynamic column sizing based on content */
                table th:first-child,
                table td:first-child {
                    width: 40%;
                }
                table th:nth-child(2),
                table td:nth-child(2) {
                    width: 15%;
                }
                table th:nth-child(3),
                table td:nth-child(3) {
                    width: 45%;
                }
                /* For tables with more than 3 columns, distribute evenly */
                table th:nth-child(n+4),
                table td:nth-child(n+4) {
                    width: auto;
                }
                /* Page margins and body adjustments */
                body {
                    margin: 20px 15px;
                }
                ul, ol {
                    padding-left: 30px;
                }
                li {
                    margin: 4px 0;
                }
            </style>
        </head>
        <body>
            z)
        </body>
        </html>
        )Údestz#PDF conversion failed with errors: z,Failed to generate PDF from markdown contentr   )Úsanitize_filenamez.pdfz(Successfully converted markdown to PDF: z"Error converting markdown to PDF: zPDF conversion failed: N)ÚloggerÚinfoÚmarkdownÚMarkdownÚconvertÚioÚBytesIOr   Ú	CreatePDFÚerrÚerrorÚ	ExceptionÚgetvalueÚcloseÚappr   Ústr)r   r   ÚmdÚhtml_contentÚhtml_documentÚoutputÚpisa_statusÚpdf_contentr   Úsafe_filenameÚfilenameÚe© r&   ú&/var/www/html/notion2html/md_to_pdf.pyÚconvert_markdown_to_pdf
   s4   
ûaŸg

€þr(   )Útypingr   r   r   Ú	xhtml2pdfr   r   Ú
get_loggerÚ__name__r   r   Úbytesr(   r&   r&   r&   r'   Ú<module>   s    
"