benjohnde/play-pdf

Can't get images via http request?

sslotsky opened this issue · 3 comments

If I render the template as html I can render an image with the src attribute pointing to another server on my machine, i.e.

<img src="http://localhost/<path-to-file>/<filename> />

But if I render the PDF with PDF.ok(), I get the error pasted below and a 0 byte document. Using the class path in the src attribute works fine, but in my use case I have to get the images from another server outside of Play!

The documentation seems to indicate that it should be able to load images with an http request, but it doesn't appear to be working.

[error] application - Creating document from template java.util.NoSuchElementException: None.get at scala.None$.get(Option.scala:274) ~[scala-library.jar:0.11.3] at scala.None$.get(Option.scala:272) ~[scala-library.jar:0.11.3] at util.pdf.PDF$MyUserAgent.getImageResource(PDF.java:40) ~[pdf_2.9.1-0.2.jar:0.2] at org.xhtmlrenderer.pdf.ITextReplacedElementFactory.createReplacedElement(ITextReplacedElementFactory.java:57) ~[core-renderer-R8.jar:na] at org.xhtmlrenderer.render.BlockBox.calcDimensions(BlockBox.java:674) ~[core-renderer-R8.jar:na] at org.xhtmlrenderer.render.BlockBox.calcDimensions(BlockBox.java:628) ~[core-renderer-R8.jar:na] [info] Compiling 1 Java source to /home/sam/select_account/target/scala-2.9.1/classes...

Looking into that, please stand by...

Hi Joerg,
I work with Sam and threw together this fix. There are probably more elegant solutions out there, but this is working for us at the moment. At the very beginning of PDF.getImageResource(), I added:

if(uri != null && uri.indexOf("http") >= 0) {
  return super.getImageResource(uri);
}

Has been fixed by Gert Vantienen in commit a315e47 - thanks!