zhaoterryy / mkdocs-pdf-export-plugin

An MkDocs plugin to export content pages as PDF files
MIT License
312 stars 43 forks source link

Add support for the mkdocs theme #3

Open shauser opened 6 years ago

shauser commented 6 years ago

Test and fix issues with the default theme, add the PDF download link.

shauser commented 6 years ago

Newest version for the stylesheet:

@media print {
    .md-container {
        display: block;
        padding-top: 0;
    }

    .md-main {
        display: block;
        height: inherit;
    }

    .md-main__inner {
        height: inherit;
        padding-top: 0;
    }

    .md-typeset .codehilitetable .linenos {
        display: none;
    }

    .md-typeset .footnote-ref {
        display: inline-block;
    }

    .md-typeset .admonition {
        display: block;
        border-top: .1rem solid rgba(0,0,0,.07);
        border-right: .1rem solid rgba(0,0,0,.07);
        border-bottom: .1rem solid rgba(0,0,0,.07);
        page-break-inside: avoid;
    }

    .md-typeset a::after {
        color: inherit;
        content: none;
    }

    .md-typeset table:not([class]) th {
        min-width: 0;
    }

    .md-typeset table {
        border: .1rem solid rgba(0,0,0,.07);
    }
}
erickjx commented 5 years ago

Hi, i need change Download text & icon, can you put this enhancement?, thanks in advance.

--- /usr/lib/python3.6/site-packages/mkdocs_pdf_export_plugin/plugin.py.org     2018-09-06 20:14:05.338321078 -0500
+++ /usr/lib/python3.6/site-packages/mkdocs_pdf_export_plugin/plugin.py 2018-09-06 21:28:45.214137461 -0500
@@ -18,6 +18,9 @@
         ('media_type', config_options.Type(utils.string_types, default=DEFAULT_MEDIA_TYPE)),
         ('verbose', config_options.Type(bool, default=False)),
         ('enabled_if_env', config_options.Type(utils.string_types)),
+        ('download_text', config_options.Type(utils.string_types, default='PDF Export')),
+        ('download_icon', config_options.Type(utils.string_types, default='\uE2C4')),
+        ('download_extra', config_options.Type(utils.string_types, default='')),
     )

     def __init__(self):
@@ -28,7 +31,7 @@
         self.total_time = 0

     def on_config(self, config):
-        self.renderer = Renderer(config['theme'].name)
+        self.renderer = Renderer(config['theme'].name, self.config)

         from weasyprint.logger import LOGGER
         import logging

--- /usr/lib/python3.6/site-packages/mkdocs_pdf_export_plugin/renderer.py.org   2018-09-06 20:41:44.789960171 -0500
+++ /usr/lib/python3.6/site-packages/mkdocs_pdf_export_plugin/renderer.py       2018-09-06 21:01:48.291104193 -0500
@@ -8,8 +8,9 @@

 class Renderer(object):
-    def __init__(self, theme: str):
+    def __init__(self, theme: str, config):
         self.theme = self._load_theme_handler(theme)
+        self.config = config;

     def render_pdf(self, content: str, base_url: str, filename: str):
         soup = BeautifulSoup(content, 'html.parser')
@@ -25,7 +26,7 @@
         html.write_pdf(filename)

     def add_link(self, content: str, filename: str):
-        return self.theme.modify_html(content, filename)
+        return self.theme.modify_html(self.config, content, filename)

     @staticmethod
     def _load_theme_handler(theme: str):

--- /usr/lib/python3.6/site-packages/mkdocs_pdf_export_plugin/themes/material.py.org    2018-09-06 20:17:58.129637218 -0500
+++ /usr/lib/python3.6/site-packages/mkdocs_pdf_export_plugin/themes/material.py        2018-09-06 22:08:03.335729194 -0500
@@ -53,11 +53,11 @@
     """

-def modify_html(html: str, href: str) -> str:
+def modify_html(config, html: str, href: str) -> str:
     soup = BeautifulSoup(html, 'html.parser')
-    a = soup.new_tag('a', href=href, title='PDF Export', download=None)
+    a = soup.new_tag('a', href=href, title=config['download_text'], download=None)
     a['class'] = 'md-icon md-content__icon'
-    a.string = '\uE2C4'
+    a.string = config['download_icon']
     soup.article.insert(0, a)

     return str(soup)

--- /usr/lib/python3.6/site-packages/mkdocs_pdf_export_plugin/themes/generic.py.org     2018-09-06 23:22:42.175530943 -0500
+++ /usr/lib/python3.6/site-packages/mkdocs_pdf_export_plugin/themes/generic.py 2018-09-06 23:24:33.869122031 -0500
@@ -5,11 +5,11 @@
     return None

-def modify_html(html: str, href: str) -> str:
+def modify_html(config, html: str, href: str) -> str:
     soup = BeautifulSoup(html, 'html.parser')

     if soup.head:
-        link = soup.new_tag('link', href=href, rel='alternate', title='PDF Export', type='application/pdf')
+        link = soup.new_tag('link', href=href, rel='alternate', title=config['download_text'], type='application/pdf')
         soup.head.append(link)

     return str(soup)
fletort commented 4 years ago

i am interested to get working this theme on the pdf output, as the output is very clean. the only problem is that is lose some line at the end of each page.... which css rule did you add to not have this problem ?