-
Notifications
You must be signed in to change notification settings - Fork 22
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
Modifications needed to allow to print a JFreeChart into a PDF document #1
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the Pull Request. Would you mind the small fixes before merging. Also saveGraphicsState() seems strange, as this saves the state on the XForm content stream, which does not really make that much sense from outside.
=> Are you really sure you need saveGraphicsState()? It should be a NOP...
@@ -477,11 +495,34 @@ public void drawGlyphVector(GlyphVector g, float x, float y) { | |||
public void fill(Shape s) { | |||
try { | |||
contentStream.saveGraphicsState(); | |||
|
|||
// Possibly set the alpha constant | |||
if((composite != null) && (composite instanceof AlphaComposite)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind removing the redundant composite != null check?
null instanceof AlphaComposite will allways be false
@@ -238,6 +239,19 @@ public void dispose() { | |||
public void draw(Shape s) { | |||
try { | |||
contentStream.saveGraphicsState(); | |||
|
|||
// Possibly set the alpha constant | |||
if((composite != null) && (composite instanceof AlphaComposite)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, composite instanceof AlphaComposite will be false if composite is null
// If, instead, several 2D graphic objects have to be written (one or more times) | ||
// into a PDF document, the graphic state has to be saved after a graphic object | ||
// has been written for the last time and a new graphic object has to be created | ||
public void saveGraphicsState() throws IOException |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I don't get why you need this... And also if you really need this, where is the restoreGraphicsState()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that it deals with the internal content stream, but it is definitively not a NOP: if I try to print several different charts in the same PDF file, without that instruction I get a corrupted file; while it works if I save the graphic state after each chart print.
I guess PDFBox realeses some resources with saveGraphicState().
Maybe there's some different/better way to achieve the same result (?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is creating a new PdfBoxGraphics2D per chart an option? That should work and would be the clean way. It should not really need that more space in the final PDF.
At least thats the way I do my charts.
I'll try to do a release later today when I've merged your changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm aware that it may seem insane, but I do create a different PdfBoxGraphics2D object per chart (like in the example code I specified).
Actually I write each chart once, each one in a different page, with a different document content stream for each page!
I assume PDFBox (maybe it is a bug) cleans or releases some resources in the document when the graphic state is saved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems very strange. It would be awesome if you could create a small testcase / testdriver for that. Just put it in src/test/java/de/rototor/pdfbox/graphics2d. And feel free to add dependencies to JFreeChart or whatever you need for that to the pom.xml with test-scope.
And this shouldn't even work, as on the contentStream close() has already been called after dispose()... You might writing directly into the main content stream of the document... I've got to investigate what PDFBox does here.
I'll try to get to this later today when I'm at home.
Also a side note: You know that you can use https://github.com/danfickle/openhtmltopdf to create your reports, so you don't have to mess around with PDFBox directly? I've integrated this library there, so that you can put your charts as HTML- tags into the report. A sample how to integrate your own renderer are here: https://github.com/danfickle/openhtmltopdf/blob/open-dev-v1/openhtmltopdf-examples/src/main/java/com/openhtmltopdf/testcases/TestcaseRunner.java - If it's not clear how this works feel free to ask.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your reply.
I'm afraid now I'm not able to create the testcase, as this happens in a bigger environment with thousands of classes.
I will try to build something from scratch, hoping it will behave the same way. Maybe next week
… shape problem can not yet be reproduced...
@FabioVassallo I built a basic multi page test with JFreeChart here. What JFreeChart diagram type are you using? I had no problem with putting multiple diagrams into document. I've just released 0.4 - without your saveGraphicsState() method, because this is not the right solution for your problem, and I don't want to have hacks in the API. Their is something wrong with the way you handle the content stream of the main page. Maybe you don't close the content stream after each page? Please compare your code with my minimal sample. |
Hi, Emmeran. Thank you for effort. I put a little example in src/test/java, where I replicated this kind of issue. Fabio |
I tried to use your classes to print a JFreeChart into a PDF document (see code example below):
It works fine, but as JFreeChart uses objects a person wouldn't consciously use (for instance rectangles with zero width), I had to do some code modifications (only in class PdfBoxGraphics2D).
I also added the support for the alpha value.
Please, find the file attached: you can locate my changes by looking for string "TODO MODIFICATION" (a comment explains each change).
It would be great if you could make a new version of pdfbox-graphics2d, incorporating my code.