Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover page logo not displayed on Windows #86

Open
jolekss opened this issue Oct 28, 2021 · 3 comments
Open

Cover page logo not displayed on Windows #86

jolekss opened this issue Oct 28, 2021 · 3 comments

Comments

@jolekss
Copy link

jolekss commented Oct 28, 2021

I am running mkdocs-with-pdf 0.9.3 on Windows 10. The cover page logo (cover_logo) does not appear in the PDF. All other images work fine.

In the debug HTML, the image URL shows up as file://C:\dir1\dir2\dir3\pdf_cover.png, which does not even work in the browser (I use Chrome).

The only way I was able to work around this problem was by manually modifying the mkdocs_with_pdf\templates\filters\url.py file where I changed the following line:
return 'file://' + path
to the following:
return 'file:///' + path.replace("\\", "/")
Then the image appears correctly in the PDF.

Anything I am missing or is this indeed a bug in the plugin?

@pyjoku
Copy link

pyjoku commented May 14, 2022

I am having the same issue. I used several approaches to solve issues with weasyprint.
So i also started to try it inside WSL with ubuntu. So it am not sure this is a problem related to just windows.
I downloaded the samples and tried to render those. no success either. The cover page image is missing.

Just did an experiment and used a weblink to an image. That is being rendered perfectly. just not the local files.

@flavienbwk
Copy link

I have the same problem. It works on Linux but not Windows.

@Lexy2
Copy link

Lexy2 commented Oct 17, 2023

I worked around it by adding a pdf_event_hook.py with the following content:

import logging, re, os.path
from urllib.parse import urlparse, urlunparse
from bs4 import BeautifulSoup

def normalize_url(str_url):
    url = urlparse(str_url)
    if url.scheme == 'file' and '\\' in url.netloc:
        return urlunparse((url.scheme, url.netloc.replace('\\', '/'), url.path, url.params, url.query, url.fragment))
    elif not url.scheme:
        return urlunparse(('file', url.netloc, os.path.abspath(url.path).replace('\\', '/'), url.params, url.query, url.fragment))
    return str_url


def fix_urls(match):
    quote, str_url = match.groups()
    return f'url({quote}{normalize_url(str_url)}{quote})'


def pre_pdf_render(soup: BeautifulSoup, logger: logging) -> BeautifulSoup:
    logger.info('(hook on pre_pdf_render)')
    url_re = re.compile(r'\burl\(([\'"]?)(.*?)\1\)')
    logger.info('Replacing relative url links with absolute and Windows-style paths in CSS')
    for tag in soup.find_all(lambda t: 'style' in t.attrs or t.name == 'style'):
        if tag.name == 'style':
            for child in tag.children:
                child.replace_with(url_re.sub(fix_urls, child))
        else:
            tag['style'] = url_re.sub(fix_urls, tag['style'])
    logger.info('Replacing img src tags')
    for tag in soup('img'):
        src = tag.get('src')
        if src:
            tag['src'] = normalize_url(src)
    return soup

Of course, I had to replace my relative links with relative to mkdocs.yml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants