Edited to hopefully now meet StackOverflow guidelines.
Possible duplicate of How can I export a JavaFX node to an SVG image?
I am trying to get a rather complicated tree chart JavaFX node onto a PDF using Apache PDF Box. However, due to resolution issues I do not want to rasterize the chart by making it an image first or taking a snapshot. I have also seen and (successfully) tried solutions to manually draw simple shapes directly onto the pdf which worked perfectly well and the resolution was amazing.
As my actual tree chart is very complicated I cannot begin to think how to draw it again manually. Which is why I started thinking about converting the node to an SVG file to draw it onto a PDF as shown here: Drawing vector images on PDF with PDFBox
The shown example there seems very straightforward, the only issue is converting the node to SVG. I tried using this https://github.com/hervegirod/fxsvgimage as they are explicitly stating that ‘It also allows to convert a JavaFX Node tree to a SVG file’. In theory I think it could work like this:
// get treeChart
Node node = treeChart.getChart().getContent();
double contentWidth = node.getBoundsInParent().getWidth() + ADDITIONAL_WIDTH;
double contentHeight = node.getBoundsInParent().getHeight() + ADDITIONAL_HEIGHT;
// taken from example usage of the converter from https://github.com/hervegirod/fxsvgimage
ConverterParameters params = new ConverterParameters();
params.width = contentWidth;
params.height = contentHeight;
SVGConverter converter = new SVGConverter();
// Perform the conversion
File svgFile = new File("mysample.svg");
try {
converter.convert(node, svgFile, params);
System.out.println("SVG file created: " + svgFile.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
//create PDF document object
try (PDDocument document = new PDDocument()) {
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
// idea how to get svg onto pdf from here /questions/31718075/drawing-vector-images-on-pdf-with-pdfbox
URI uri = svgFile.toURI();
SVGUniverse svgUniverse = new SVGUniverse();
SVGDiagram diagram = svgUniverse.getDiagram(svgUniverse.loadSVG(uri.toURL()));
// Create a PDFGraphics2D object
PdfBoxGraphics2D graphics2D = new PdfBoxGraphics2D(document, (int) contentWidth, (int) contentHeight);
// Render the JavaFX node onto the PDFGraphics2D object
try {
diagram.render(graphics2D);
} finally {
graphics2D.dispose();
}
// Get XObjects
PDFormXObject xform = graphics2D.getXFormObject();
PDPageContentStream contentStream = new PDPageContentStream(document, page);
Matrix matrix = new Matrix();
matrix.translate(0, 20);
contentStream.transform(matrix);
// Draw the form
contentStream.drawForm(xform);
contentStream.close();
// Save the document
document.save(new File("mysample.pdf"));
document.close();
In theory this seems totally doable and I tried it roughly like this with no errors in intelliJ but when running the program I get the following errors:
java.lang.NoClassDefFoundError: org/girod/javafx/svgimage/tosvg/SVGConverter Caused by: java.lang.ClassNotFoundException: org.girod.javafx.svgimage.tosvg.SVGConverter
and the same messages for ConverterParameters. Now I am not sure if this is a maven problem, even though I do not get any errors in my code. Or if it is because of this bug the creators of https://github.com/hervegirod/fxsvgimage have issued: “The converter from JavaFX to svg jar is not included in the distrib”.
Does anyone have an idea why I am getting these errors? Or maybe someone has a totally different idea on how to render my node on a PDF. Perhaps an SVG is not necessary after all…
7