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

Modifications needed to allow to print a JFreeChart into a PDF document #1

Merged
merged 2 commits into from Jun 22, 2017

Conversation

FabioVassallo
Copy link
Contributor

I tried to use your classes to print a JFreeChart into a PDF document (see code example below):

PDDocument document = ...;
PDPageContentStream contentStream = ...;

JFreeChart chart = ...;

float posX  = ...; float posY   = ...;
int   width = ...; int   height = ...;

PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document, width, height);
Rectangle2D rectangle = new Rectangle2D.Double(0, 0, width, height);
chart.draw(pdfBoxGraphics2D, rectangle);

pdfBoxGraphics2D.dispose();

PDFormXObject xform = pdfBoxGraphics2D.getXFormObject();

Matrix matrix = new Matrix();
matrix.translate(posX, posY);
contentStream.transform(matrix);
contentStream.drawForm(xform);

pdfBoxGraphics2D.saveGraphicsState(); // See my modifications in class

...

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.

Copy link
Owner

@rototor rototor left a 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))
Copy link
Owner

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))
Copy link
Owner

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
Copy link
Owner

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()?

Copy link
Contributor Author

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 (?)

Copy link
Owner

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.

Copy link
Contributor Author

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

Copy link
Owner

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.

Copy link
Contributor Author

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

rototor added a commit that referenced this pull request Jun 22, 2017
@rototor rototor merged commit 4d5d998 into rototor:master Jun 22, 2017
@rototor
Copy link
Owner

rototor commented Jun 22, 2017

@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.

@FabioVassallo
Copy link
Contributor Author

Hi, Emmeran.

Thank you for effort.
I had the time to investigate the problem, and it turned out to be a memory allocation problem.
During the build of the document, I do massive calculations by loading data from an external system.
In some way (I guess) the saveGraphicsState() method triggers the release of some unused memory.
Probably there may be a cleaner way to solve this...

I put a little example in src/test/java, where I replicated this kind of issue.

Fabio

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

Successfully merging this pull request may close these issues.

None yet

2 participants