Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Possible bug in Flying Saucers 'Graphics2DRenderer' #63

Closed
skjardenCode opened this issue Jan 15, 2017 · 2 comments
Closed

Possible bug in Flying Saucers 'Graphics2DRenderer' #63

skjardenCode opened this issue Jan 15, 2017 · 2 comments

Comments

@skjardenCode
Copy link

skjardenCode commented Jan 15, 2017

Hi Daniel,

thanks a lot for your ongoing efforts on this helpful project!

We recently came up with the use case to render a certain small HTML to an image before we render it to PDF. As OpenHtmlToPdf is based on Flying Saucer, we tried to use the com.openhtmltopdf.simple.Graphics2DRenderer class to do so, which should be an easy task.

Unfortunately (using Java 6 and Windows 10 with the latest OpenHtmlToPdf version available in Maven Central), the image output of Graphics2DRenderer yield wrong results, mainly what seems like an encoding problem.

Please, see the following simple example code:

String xhtml = 
        "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
                + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
        + "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"de\" lang=\"de\">"
            + "<body>"
                + "Moonbase"
            + "</body>"
        + "</html>";

InputSource inputSource = new InputSource( new BufferedReader( new StringReader( xhtml ) ) );
Document document = XMLResource.load( inputSource ).getDocument();

Graphics2DRenderer graphics2dRenderer = new Graphics2DRenderer();
graphics2dRenderer.setDocument( document, null );

BufferedImage bufferedImage = new BufferedImage( 500, 500, BufferedImage.TYPE_INT_RGB );
Graphics2D g2d = bufferedImage.createGraphics();

graphics2dRenderer.layout( g2d, new Dimension( 500, 500 ) );
graphics2dRenderer.render( g2d );

g2d.dispose();

File outputfile = File.createTempFile( Graphics2DRendererTest.class.getSimpleName(), ".png" );
ImageIO.write( bufferedImage, "png", outputfile );
Desktop.getDesktop().open( outputfile );

The image you get (trying to render the word "Moonbase") is the following:

image

We than integrated the latest Flying Saucer version into our project, next to OpenHtmlToPdf, and just changed the line

import com.openhtmltopdf.simple.Graphics2DRenderer;

to

import org.xhtmlrenderer.simple.Graphics2DRenderer;

so the example code is using the Graphics2DRenderer from the latest Flying Saucer lib ('flying-saucer-core-9.1.1.jar').

Running the code again we get the following image:

image

Which is the expected result.

This seems to be a bug in Flying Saucer. Making it short, I have two questions:

  1. Which Flying Saucer version is OpenHtmlToPdf based on?

  2. Is it possible that you update the Flying Saucer code to fix the above bug (if it IS a bug)?

Thanks a lot!

@danfickle
Copy link
Owner

Hi @skjardenCode
As we've worked on the PDF side quite a lot of bitrot has occurred in the java2d output to the point it is not currently useable. I did remove reference to image output from the readme but I should have explicitly said that image output is broken. I apologise for your wasted effort in trying to get it to work.

I have not deleted the image output code as at some point we may want to get it working again. As for your bug, it may be related to encoding or fonts. I'd recommend keep using flying saucer for image output if possible.

rototor added a commit to rototor/openhtmltopdf that referenced this issue Feb 26, 2017
… as a smoke test and also render PNGs for each PDF written. This allows to compare what features are missing in the Java2D side.
rototor added a commit to rototor/openhtmltopdf that referenced this issue Feb 26, 2017
… as a smoke test and also render PNGs for each PDF written. This allows to compare what features are missing in the Java2D side.
@danfickle
Copy link
Owner

Closed in favor of #73

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants