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

Image overlay inside cells #165

Open
knorrest opened this issue Feb 27, 2019 · 13 comments
Open

Image overlay inside cells #165

knorrest opened this issue Feb 27, 2019 · 13 comments

Comments

@knorrest
Copy link
Contributor

I'm creating pdf:s with the library, using borderless nested pdf-tables to make it simple to place my content. My problem is that in addition to text and background, I would need to add image overlays inside some cells. In openpdf/itext such things seem to be done using cell events. Am I right in that there is no support for this in the library?

@yogthos
Copy link
Collaborator

yogthos commented Feb 27, 2019

Yeah, there's no support for hooking into cell events at the moment. I'd be open to a PR for declaring an events hook in the table cell options map if you'd be interested to take a look.

@knorrest
Copy link
Contributor Author

Ok, I'll look at it - will get back to you when I've thought a bit...

@knorrest
Copy link
Contributor Author

knorrest commented Mar 1, 2019

Ok, made a PR!

@knorrest
Copy link
Contributor Author

knorrest commented Mar 1, 2019

One option for putting images in cells would be to add some code I now have in my app to the library:

`
(defn- h-align [alignment left width img-left]
(case alignment
:left left
:right (+ left (- width img-left))
(+ left (/ (- width img-left) 2))))

(defn- v-align [alignment bottom height img-height]
(case alignment
:bottom bottom
:top (+ bottom (- height img-height))
(+ bottom (/ (- height img-height) 2))))

(defn- fit-image-fn [halignment valignment ^URL img-url]
(fn [^Rectangle pos ^PdfContentByte canvas]
(let [left (.getLeft pos)
bottom (.getBottom pos)
width (.getWidth pos)
height (.getHeight pos)
img (Image/getInstance img-url)]
(.scaleToFit img width height)
(let [img-width (.getScaledWidth img)
img-height (.getScaledHeight img)
img-left (h-align halignment left width img-width)
img-bottom (v-align valignment bottom height img-height)]
(.setAbsolutePosition img img-left img-bottom)
(.addImage canvas img)))))`

Used like:

{:background-layer-fn (fit-image-fn :center :bottom trend-image)}

@yogthos
Copy link
Collaborator

yogthos commented Mar 1, 2019

Thanks, just saw the PR, and trying it out locally everything looks good to me. I think the helper functions would be useful to add. Might make sense to expose them declaratively via cell options. Maybe something like {:background-layer {:fit-image? true ...}.

@knorrest
Copy link
Contributor Author

knorrest commented Mar 1, 2019

Ok, I’ll sketch something up and get back.

@yogthos
Copy link
Collaborator

yogthos commented Mar 1, 2019

Fantastic, and I'm good to do a release as soon as we get this ironed out.

@knorrest
Copy link
Contributor Author

knorrest commented Mar 5, 2019

Thinking about it, maybe the best option is to just add a section in the readme with some examples. Theres a million things that one could do in cell event callbacks, with image background or overlay and shading being the ones that I needed now. A few simple examples might be enough to just show the idea. I agree that the declarative approach is really nice, but since we're using the more "advanced" part of the underlying API here it might make sense to just expose that in a nice way.

@yogthos
Copy link
Collaborator

yogthos commented Mar 5, 2019

I'm ok with that, if some common patterns emerge we can add declarative syntax to cover those via the standard API at a later point. I can push out a release tonight then.

@knorrest
Copy link
Contributor Author

knorrest commented Mar 6, 2019

Ok, I'll add something to the doc and make a PR. If you want that in the release you'll have to wait until tomorrow, I'm a bit stuck with other things today.

@knorrest
Copy link
Contributor Author

knorrest commented Mar 6, 2019

Found some time, and did a PR with example added to the readme. :-)

@knorrest
Copy link
Contributor Author

knorrest commented Mar 6, 2019

...and a release would be terrific!

@yogthos
Copy link
Collaborator

yogthos commented Mar 6, 2019

Fantastic, thanks for adding the docs and I just pushed out 2.3.2 with the updates.

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

2 participants