flyingsaucerproject / flyingsaucer

XML/XHTML and CSS 2.1 renderer in pure Java
Other
2.02k stars 565 forks source link

CSS background property with url to local image with relative path not getting rendered #440

Closed oxiadenine closed 2 weeks ago

oxiadenine commented 2 weeks ago

In release 9.10.2, org.xhtmlrenderer.util.FSImageWriter was removed, but Java2DRenderer.htmlAsImage() has been added.

The problem:

if I use ImageIO to write the BufferedImage to a File or OutputStream, the CSS property background with an url to a local image with relative path does not render to the final image, and I think the problem is not with ImageIO, but with Java2DRenderer.

9.9.2 (works)

ByteArrayOutputStream().use { outputStream ->
    val graphicRenderer = Java2DRenderer(this, width)

    FSImageWriter().write(graphicRenderer.image, outputStream)

    outputStream.toByteArray()
}

or

ByteArrayOutputStream().use { outputStream ->
    val graphicRenderer = Java2DRenderer(this, width)

    ImageIO.write(graphicRenderer.image, "png", outputStream)

    outputStream.toByteArray()
}

9.10.2 (does not work)

ByteArrayOutputStream().use { outputStream ->
    val image = Java2DRenderer.htmlAsImage(this, width)

    ImageIO.write(image, "png", outputStream)

    outputStream.toByteArray()
}

or

ByteArrayOutputStream().use { outputStream ->
    val graphicRenderer =  Java2DRenderer(this, width)

    ImageIO.write(graphicRenderer.image, "png", outputStream)

    outputStream.toByteArray()
}

P.S. CSS background property with url to remote image or file:// with absolute path is working.

asolntsev commented 2 weeks ago

@oxiadenine Thank you for reporting the problem. In PR https://github.com/flyingsaucerproject/flyingsaucer/pull/441, I've restored classes FSImageWriter and ImageRenderer.

I think you can simplify your code by using ImageRenderer instead of FSImageWriter:

BufferedImage image = ImageRenderer.renderToImage(getClass().getResource("/text-with-background-image.xhtml"), resultingImagePath, 600);

Please share your suggestions how we can improve API of ImageRenderer even more.

oxiadenine commented 2 weeks ago

@asolntsev I have tested what you done, but it is not working, so I think it has to do with the internal functionality of Java2DRenderer, FSImageWriter and ImageRenderer are irrelevant, because as write before, ImageIO is working well in realease 9.9.2.

asolntsev commented 2 weeks ago

@asolntsev I have tested what you done, but it is not working, so I think it has to do with the internal functionality of Java2DRenderer, FSImageWriter and ImageRenderer are irrelevant, because as write before, ImageIO is working well in realease 9.9.2.

How exactly did you test it? How can you be sure that you used the latest snapshot version I built today?

Because I tested, and it worked on my machine. :)

oxiadenine commented 2 weeks ago

@asolntsev I have tested what you done, but it is not working, so I think it has to do with the internal functionality of Java2DRenderer, FSImageWriter and ImageRenderer are irrelevant, because as write before, ImageIO is working well in realease 9.9.2.

How exactly did you test it? How can you be sure that you used the latest snapshot version I built today?

Because I tested, and it worked on my machine. :)

I just copied the changes you done in the two commits linked to this issue and reproduce it in my project, since this changes doesn't modify internal logic of Java2DRenderer, so I created a XML file with my HTML text and loaded it as you done in your test.

Also, ImageRenderer doesn't do anything that I'm currently doing, and FSImageWriter can not be the problem as I mentioned before.

asolntsev commented 2 weeks ago

@oxiadenine Sorry, I don't understand. Please do the following:

  1. use the snapshot version org.xhtmlrenderer:flying-saucer-core:9.11.0-SNAPSHOT
  2. use this code: BufferedImage image = ImageRenderer.renderToImage(getClass().getResource("/text-with-background-image.xhtml"), resultingImagePath, 600);
oxiadenine commented 2 weeks ago

@oxiadenine Sorry, I don't understand. Please do the following:

1. use the snapshot version `org.xhtmlrenderer:flying-saucer-core:9.11.0-SNAPSHOT`

2. use this code: `BufferedImage image = ImageRenderer.renderToImage(getClass().getResource("/text-with-background-image.xhtml"), resultingImagePath, 600);`

Sorry, but it didn't worked, so I created a repository to show you what I doing.

https://github.com/oxiadenine/flying-saucer-test

I'm using Kotlin.

asolntsev commented 2 weeks ago

@oxiadenine Thank you for the working example.

See my fix for this sample: https://github.com/oxiadenine/flying-saucer-test/pull/1

asolntsev commented 2 weeks ago

@oxiadenine FlyingSaucer 9.11.0 has been released.