orzih / mkdocs-with-pdf

Generate a single PDF file from MkDocs repository.
MIT License
332 stars 77 forks source link

debug_html build fails on Windows if output is redirected #56

Closed rikedyp closed 3 years ago

rikedyp commented 3 years ago

Seems related to handling of unicode in Python, except the same repo and configuration successfully builds and outputs to file on Linux

Windows 10 Version 10.0.19042 Build 19042 Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)]

mkdocs build
# a bunch of HTML and stuff
INFO    -  Converting 34 articles to PDF took 8.6s
INFO    -  Documentation built in 9.49 seconds
mkdocs build > pdf.html
# a bunch of INFO and WARNING messages
Traceback (most recent call last):
  File "c:\users\rpark\appdata\local\programs\python\python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\rpark\appdata\local\programs\python\python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\rpark\AppData\Local\Programs\Python\Python37\Scripts\mkdocs.exe\__main__.py", line 7, in <module>
  File "c:\users\rpark\appdata\local\programs\python\python37\lib\site-packages\click\core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "c:\users\rpark\appdata\local\programs\python\python37\lib\site-packages\click\core.py", line 782, in main
    rv = self.invoke(ctx)
  File "c:\users\rpark\appdata\local\programs\python\python37\lib\site-packages\click\core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "c:\users\rpark\appdata\local\programs\python\python37\lib\site-packages\click\core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:\users\rpark\appdata\local\programs\python\python37\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "c:\users\rpark\appdata\local\programs\python\python37\lib\site-packages\mkdocs\__main__.py", line 152, in build_command
    build.build(config.load_config(**kwargs), dirty=not clean)
  File "c:\users\rpark\appdata\local\programs\python\python37\lib\site-packages\mkdocs\commands\build.py", line 295, in build
    config['plugins'].run_event('post_build', config=config)
  File "c:\users\rpark\appdata\local\programs\python\python37\lib\site-packages\mkdocs\plugins.py", line 96, in run_event
    result = method(**kwargs)
  File "c:\users\rpark\appdata\local\programs\python\python37\lib\site-packages\mkdocs_with_pdf\plugin.py", line 135, in on_post_build
    self.generator.on_post_build(config, self.config['output_path'])
  File "c:\users\rpark\appdata\local\programs\python\python37\lib\site-packages\mkdocs_with_pdf\generator.py", line 132, in on_post_build
    print(html_string)
  File "c:\users\rpark\appdata\local\programs\python\python37\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2190' in position 34939: character maps to <undefined>
orzih commented 3 years ago

Hi @rikedyp.

This is a code conversion problem between Windows-1252 and Unicode. Can you try the PYTHONUTF8 environment variable?

$ set PYTHONUTF8=1
$ mkdocs build > debug_pre_pdf.html

refs: PEP-0540

rikedyp commented 3 years ago

That works, thanks!