JavaFX node to PDF using SVG? [closed]

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật