Skip to content

MarkdownToPdf

Export

Export is a class that contains methods to export content to different formats. Initially, it supports exporting content to PDF.

Source code in src/project/export/MarkdownToPdf.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Export:
    """Export is a class that contains methods to export content to different formats.
    Initially, it supports exporting content to PDF.
    """

    @staticmethod
    def export_to_pdf(content: str, container: st.container = None, name: str = 'export.pdf'):
        """Exports the given content to PDF.

        Args:
            content (str): Markdown content to export.
            container (st.container, optional): Parent container to display the download button. Defaults to None.
            name (str, optional): Name of the PDF file. Defaults to 'export.pdf'.
        """
        with open('./src/project/export/mktopdf.md', 'w', encoding='utf-8') as f:
            f.write(content)
        command = ["mdpdf", "-o", "./src/project/export/export.pdf", "./src/project/export/mktopdf.md"]
        try:
            subprocess.run(command, check=True)
            if container is not None:
                container.success("PDF created successfully")
                with open('./src/project/export/export.pdf', 'rb') as f:
                    container.download_button(
                        label="Download PDF",
                        data=f,
                        file_name=name,
                        mime='application/pdf'
                    )
                os.remove('./src/project/export/mktopdf.md')
                os.remove('./src/project/export/export.pdf')
        except subprocess.CalledProcessError as e:
            if container is not None:
                container.error("An error occurred while creating PDF")
                container.error(e)
export_to_pdf(content, container=None, name='export.pdf') staticmethod

Exports the given content to PDF.

Parameters:

  • content (str) –

    Markdown content to export.

  • container (streamlit.container, default: None ) –

    Parent container to display the download button. Defaults to None.

  • name (str, default: 'export.pdf' ) –

    Name of the PDF file. Defaults to 'export.pdf'.

Source code in src/project/export/MarkdownToPdf.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@staticmethod
def export_to_pdf(content: str, container: st.container = None, name: str = 'export.pdf'):
    """Exports the given content to PDF.

    Args:
        content (str): Markdown content to export.
        container (st.container, optional): Parent container to display the download button. Defaults to None.
        name (str, optional): Name of the PDF file. Defaults to 'export.pdf'.
    """
    with open('./src/project/export/mktopdf.md', 'w', encoding='utf-8') as f:
        f.write(content)
    command = ["mdpdf", "-o", "./src/project/export/export.pdf", "./src/project/export/mktopdf.md"]
    try:
        subprocess.run(command, check=True)
        if container is not None:
            container.success("PDF created successfully")
            with open('./src/project/export/export.pdf', 'rb') as f:
                container.download_button(
                    label="Download PDF",
                    data=f,
                    file_name=name,
                    mime='application/pdf'
                )
            os.remove('./src/project/export/mktopdf.md')
            os.remove('./src/project/export/export.pdf')
    except subprocess.CalledProcessError as e:
        if container is not None:
            container.error("An error occurred while creating PDF")
            container.error(e)