zhaoterryy / mkdocs-pdf-export-plugin

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

Error with new MkDocs 1.0, initial Fix. #5

Closed erickjx closed 5 years ago

erickjx commented 5 years ago

Hi i have troubles with new MkDocs 1.0, the error was:

... result = method(item, **kwargs) File "/usr/lib/python3.6/site-packages/mkdocs_pdf_export_plugin/plugin.py", line 54, in on_post_page path = os.path.dirname(page.abs_output_path) AttributeError: 'Page' object has no attribute 'abs_output_path'

I fixed this lines 'mkdocs_pdf_export_plugin/plugin.py':

53 54 #path = os.path.dirname(page.abs_output_path) 55 path = os.path.dirname(page.file.abs_dest_path) 56 os.makedirs(path, exist_ok=True) 57 58 #filename = os.path.splitext(os.path.basename(page.input_path))[0] 59 filename = os.path.splitext(os.path.basename(page.file.src_path))[0] 60 61 base_url = urls.path2url(os.path.join(path, filename))

Then run ok, but not display PDF to download on html build. Thanks in advance.

erickjx commented 5 years ago

With the new material theme working Ok....

erickjx commented 5 years ago

PDFs are wrong, only have parts of document with new MkDocs.

erickjx commented 5 years ago

Finally work all ok , the problem was mkdocs-material, to fix need 3.0.3 version and the next fix in plugin.py:

53 54 #path = os.path.dirname(page.abs_output_path) 55 path = os.path.dirname(page.file.abs_dest_path) 56 os.makedirs(path, exist_ok=True) 57
58 #filename = os.path.splitext(os.path.basename(page.input_path))[0] 59 filename = os.path.splitext(os.path.basename(page.file.src_path))[0] 60
61 #base_url = urls.path2url(os.path.join(path, filename)) 62 base_url = urls.path2url(path)

Please merge this patch.

cdeboever3 commented 5 years ago

Thanks for posting your fixes @erickjx. I'm also running into the following error even after making your changes and installing mkdocs-material 3.0.3:

INFO: Step 2 - Fetching and parsing CSS - https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700%7CRoboto+Mono
Error converting index.md to PDF: cannot use string() on <cdata 'unsigned char *' NULL>

Did you see this error at all? The header of my mkdocs.yml is

theme:
    name: 'material'
plugins:
    - search
    - pdf-export:
            verbose: true
extra_css:
    - assets/stylesheets/weasyprint.css
extra_javascript:
    - assets/javascripts/pdf-download.js
erickjx commented 5 years ago

Hi i upgrade all modules:

pip3 install -U -I pip mkdocs markdown pip3 install -U -I pymdown-extensions fontawesome_markdown mkdocs-bootstrap mkdocs-bootswatch mkdocs-material markdown-include pygments tables scipy six WeasyPrint

And only have this error, the error is common but i can generate PDF ok:

WARNING: Error: Expected ':' after declaration name, got {} block. at 319:9. INFO: Step 2 - Fetching and parsing CSS - file:///home/workspace/mkdocs/target/site/css/weasyprint.css INFO: Step 2 - Fetching and parsing CSS - file:///home/workspace/mkdocs/target/site/assets/fonts/font-awesome.css ERROR: Failed to load font at "file:///home/workspace/mkdocs/target/site/assets/fonts/specimen/FontAwesome.woff2" WARNING: Ignored text-rendering:auto at 4:314, unknown property. WARNING: Ignored -webkit-font-smoothing:antialiased at 4:334, unknown property. WARNING: Ignored -moz-osx-font-smoothing:grayscale at 4:369, unknown property.

erickjx commented 5 years ago

In the pdf-download.js change this:

4 // Add a download button (the following code works for the mkdocs-material theme): 5 var article = document.getElementsByTagName('article')[0];

To: 4 // Add a download button (the following code works for the mkdocs-material theme): 5 var article = document.querySelectorAll('a.toclink')[0];

cdeboever3 commented 5 years ago

Thanks @erickjx. I built a fresh Python environment with those packages and changed pdf-download.js as you described but I still hit the same error:

Error converting index.md to PDF: cannot use string() on <cdata 'unsigned char *' NULL>

I hit that error when plugin.py tries to execute the line

html.write_pdf(os.path.join(path, filename + '.pdf'))

So maybe the problem is in WeasyPrint? Are you using WeasyPrint 0.42.3?

erickjx commented 5 years ago

Hi, i use WeasyPrint: /usr/lib64/python3.6/site-packages (0.42.3)

My enviroment is OpenSuse 15.0 with Python 3.6, the plugin.py patch is:

--- plugin.py   2018-08-21 00:59:35.682864055 -0500
+++ /usr/lib/python3.6/site-packages/mkdocs_pdf_export_plugin/plugin.py 2018-08-07 20:40:36.603659903 -0500
@@ -51,12 +51,15 @@

         self.num_files += 1

-        path = os.path.dirname(page.abs_output_path)
+        #path = os.path.dirname(page.abs_output_path)
+        path = os.path.dirname(page.file.abs_dest_path)
         os.makedirs(path, exist_ok=True)

-        filename = os.path.splitext(os.path.basename(page.input_path))[0]
+        #filename = os.path.splitext(os.path.basename(page.input_path))[0]
+        filename = os.path.splitext(os.path.basename(page.file.src_path))[0]

-        base_url = urls.path2url(os.path.join(path, filename))
+        #base_url = urls.path2url(os.path.join(path, filename))
+        base_url = urls.path2url(path)

         try:
             html = HTML(string=output_content, base_url=base_url, media_type=self.config['media_type'])

The full pdf-download.js is:

document.addEventListener('DOMContentLoaded', function () {
    var link = document.querySelector('link[rel=alternate][type="application/pdf"]');
    if (link) {
        // Add a download button (the following code works for the mkdocs-material theme):
        //var article = document.getElementsByTagName('article')[0];
        var article = document.querySelectorAll('a.toclink')[0];
        var articleParent = article.parentNode;

        var element = document.createElement('a');
        element.setAttribute('href', link.href);
        element.setAttribute('class', 'md-icon md-content__icon');
        element.setAttribute('title', 'Download as PDF');
        element.setAttribute('download', '');
        // MaterialIcons http://cms.devoffice.com/repack/packs/material-icons/
        //element.innerHTML = '&#xE2C4;'; // download
        element.innerHTML = '&#xE415;'; // pdf

        article.insertBefore(element, article.children[0]);
        //articleParent.insertBefore(element, article.nextSibling);
    }
});