orzih / mkdocs-with-pdf

Generate a single PDF file from MkDocs repository.
MIT License
325 stars 76 forks source link

Remove image links in PDF #85

Closed hmsdoc closed 2 years ago

hmsdoc commented 2 years ago

I'm working on a documentation that includes a lot of screenshots. To make small details better readable, each screenshot is linked to the original image. If I click on a screenshot, the corresponding image is opened in its original full screen resolution. This works perfect in the HTML version of the documentation. But in the exported PDF these links are preserved which causes broken links. All images are stored in the folder /images. So, if I click on a linked image in the PDF it tries to open the image from the folder /images, which obviously does not exist.

Is there any way to keep linked images in the HTML output but disable all image links in the PDF output?

hmsdoc commented 2 years ago

I was able to remove the image links by adding the file pdf_event_hook.py as described here. In this file I added the following code to the pre PDF render section:

def pre_pdf_render(soup: BeautifulSoup, logger: logging) -> BeautifulSoup:
    logger.info('(hook on pre_pdf_render)')

    # Remove image links
    for img in soup.find_all('img'):
        if img.parent.name == 'a':
            img.parent.unwrap()

    return soup