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

Images in markdown do not get pulled in #25

Open
tsmcgrath opened this issue Jul 7, 2024 · 11 comments
Open

Images in markdown do not get pulled in #25

tsmcgrath opened this issue Jul 7, 2024 · 11 comments

Comments

@tsmcgrath
Copy link

I am using markdown-pdf to pull in several existing markdown files with embedded images and write them to a single pdf. The separate markdowns display the images (with either a relative or absolute path) correctly. But, when I read them into the library with pdf.add_section the markdown comes in fine and converts to a pdf file but the image is not included.
Code:
`from markdown_pdf import MarkdownPdf
from markdown_pdf import Section

create pdf

pdf = MarkdownPdf(toc_level=2)

add section

pdf.add_section(Section("# Catchment ID 44193\n"))

add 2nd section from markdown file

md = open('./markdown/Intro.md', 'r', newline='', encoding='utf-8-sig').read()
pdf.add_section(Section(md))

set pdf properties

pdf.meta["title"] = "LOCA Report"
pdf.meta["author"] = "Tyson Broad"

save pdf

pdf.save("./src/python/md2pdf_test3.pdf")
`
Image of the local markdown displaying image correctly:
image

Problematic markdown text attached.
Intro.md
Output PDF attached.
md2pdf_test3.pdf

@vb64
Copy link
Owner

vb64 commented Jul 7, 2024

markdown-pdf by default uses the current folder (./) as the image file path prefix.

The image file in intro.md has path /Users/tim/DocumentsLocal/Github/catch23/images/titletest1.jpeg.

To use absolute file pathes, set the root parameter of the Section to the empty string.

In your code this change will be like this:

pdf.add_section(Section(md, root=''))

@tsmcgrath
Copy link
Author

That doesn't seem to work. I also moved the markdown and image file local to the python script and that doesn't seem to work either. The markdown comes in ok, no image.

@tsmcgrath
Copy link
Author

tsmcgrath commented Jul 7, 2024

I can pull an image into it's own section on it's own. Without being inside markdown.

@tsmcgrath
Copy link
Author

Sorry to be a problem child. This is a very useful library and I really appreciate your work.

@vb64
Copy link
Owner

vb64 commented Jul 7, 2024

Sorry, I really didn’t notice that in Intro.md you use html for the image, not markdown.

markdown-pdf is designed to handle markdown, not mixed markdown/html code.

Try changing the code for the image in Intro.md to the following:

[Steep Rocky- Oak/Juniper/Woodland in foreground. Loamy Bottomland-San Saba River in background](titletest1.jpeg)

And place the file titletest1.jpeg in the current directory where your script is running from.

@tsmcgrath
Copy link
Author

tsmcgrath commented Jul 7, 2024

Tried that. Still no luck.
md2pdf_test3.pdf
Intro.md

@vb64
Copy link
Owner

vb64 commented Jul 8, 2024

You can try the example from the readme yourself. The generated pdf will contain an embedded image with the Python logo.

You need to clone the markdown-pdf repository to your host, open a console window, go to the root directory of the project (where the README.md file is located) and run

python makepdf.py README.md markdown_pdf.pdf

A markdown_pdf.pdf file will be created in this directory.

If successful, this will be a good starting point for writing the code you need.

@tsmcgrath
Copy link
Author

I have successfully gotten the example to work without a problem. That's how I started. Inserting an image into the pdf using the library works fine. But if the image is referenced in the markdown file, even using a markdown reference and not html, it does not make it into the pdf. Even if the image correctly displays when previewing the markdown in a browser.

The markdown:

## The Upper San Saba River
![Steep Rocky- Oak/Juniper/Woodland in foreground. Loamy Bottomland-San Saba River in background](images/titletest1.jpeg)

The spring-fed San Saba River is a valuable resource to West-Central Texas. The relatively constant flows of the river

Image rendering correctly when referenced in markdown:
image

Resulting pdf:
md2pdf_test3.pdf

@tsmcgrath
Copy link
Author

If using a referenced image is just not included in the current functionality, fine. I accept that. I just wanted to make sure it was not a bug.

@seangrogan
Copy link

Hi, I know this is an older issue but I was having the same problem and I found a solution:

I needed to add the full path to the image file in the root= argument, e.g. c:/Users/tim/DocumentsLocal/Github/catch23/images/

So my code in my specific project is

def create_pdf_from_md(md_filepath, pdf_filepath):
    with open(md_filepath, 'r') as f:
        md_content = f.read()
   
    root = os.path.dirname(os.path.abspath(md_filepath))
    pdf = MarkdownPdf()
    pdf.add_section(Section(md_content, root=root))
    pdf.save(pdf_filepath)

@vb64
Copy link
Owner

vb64 commented Nov 27, 2024

Yes, the root argument is intended for cases where the embedded image files are not located in the current directory.

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

3 participants