pdf_reports icon indicating copy to clipboard operation
pdf_reports copied to clipboard

Add another img

Open davidpatalejos opened this issue 5 years ago • 6 comments

Hi everyone. I want to change the logo from the basic example but I can't get it right, It looks empty without any image.

This is my syntax for adding a new image, I have it in a folder and at the same folder but I can't get it right and I don't know why.

img(style="width:400px; display:block; margin:0 auto;" src="{{ img }}") img(style="width:400px; display:block; margin:0 auto;" src="/static/avocado.png") img(style="width:400px; display:block; margin:0 auto;" src="/static/avocado.jpg") img(style="width:400px; display:block; margin:0 auto;" src="./avocado.png") img(style="width:400px; display:block; margin:0 auto;" src="./avocado.jpg")

None of them above is showing the image.

Thank you a lot.

davidpatalejos avatar Jul 08 '20 17:07 davidpatalejos

Looking at the generated html of the basic example, the absolute file path is used. I guess you can use python to join the relative path with the current working directory to get the absolute path and inject the path as a variable into pug. I was not able to use relative paths, only absolute paths.

Support for relative paths or an example showing how that can be done would be great!

bacox avatar Jul 15 '20 10:07 bacox

The pug template refers to the GLOBALS variable. One option is adding the image path:

import pdf_reports
pdf_reports.GLOBALS["avocado"] = "/path/to/avocado.png"

Then modify the pug template:

img(style="width:200px; display:block; margin:0 auto;"
    src="file:///{{ avocado }}")

PDF output: screenshot

veghp avatar Sep 12 '20 12:09 veghp

A related question: attribute interpolation doesn't seem to work with pug_to_html. I have a list of images that I want to render with

each fpath in img_files
   img(src=fpath)

where img_files is a list containing image paths ['file:///{{img1}}', 'file:///{{img2}}' ... ]. These paths have already been added to pdf_reports.GLOBALS. @veghp Do you have a solution to this? Thanks a lot.

Theophylline avatar Oct 28 '20 04:10 Theophylline

Where did you define img_files? This works for me: pdf_reports.GLOBALS["img_files"] = ["file:///path/to/avocado.png", "file:///path/to/screenshot.png"] Pug template:

each fpath in img_files
    img(style="width:200px; display:block; margin:0 auto;"
    src=fpath)

Screenshot2

veghp avatar Oct 28 '20 11:10 veghp

I defined img_files in another Python script and passed it to pug_to_html. It works now, thank you so much! I stored the file paths as separate keys in GLOBALS, which was why it didn't work.

Theophylline avatar Oct 28 '20 16:10 Theophylline

Great, I'm glad it works now!

veghp avatar Oct 28 '20 17:10 veghp