vb64 / markdown-pdf

Markdown to pdf renderer
MIT License
47 stars 2 forks source link

Images in markdown do not get pulled in #25

Open tsmcgrath opened 4 months ago

tsmcgrath commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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

tsmcgrath commented 4 months ago

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

vb64 commented 4 months ago

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 commented 4 months ago

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

vb64 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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.