Empty PDF using XChart ChartPie as SVG ressource with PDFBoxGraphics2D and Salamander

121 views Asked by At

I've been trying to compose a pie chart using XChart then vectorising it to SVG format to use it in a PDF built with PDFBox.

To do that, I use:

  • XChart for the pie chart building and vectorising it ;
  • SVGSalamender for the Graphics2D building based on previous SVG vectorising ;
  • PDFBoxGraphics2D for adding previous Graphics2D to my PDF.

Everything runs fine (in compiles and runs without errors) but it does not work. The output PDF stays empty.

This is my code:

@Test
public void test() {
  try {
    PieChart camembertXChart = camembertXChart();

    BufferedImage imageCamembertXChart = BitmapEncoder.getBufferedImage(camembertXChart);

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    File svg = new File("F:\\Users\\mangin\\Desktop\\Camembert_XChart.svg");
    VectorGraphicsEncoder.saveVectorGraphic(camembertXChart, svg.getName(), VectorGraphicsFormat.SVG);
    byte[] data = output.toByteArray();
    FileInputStream input = new FileInputStream(svg);
    BufferedImage imageCamembertXChartVectorise = ImageIO.read(input);

    PDDocument document = new PDDocument();
    PDPage page = new PDPage();

    document.addPage(page);

    PDPageContentStream contenu = new PDPageContentStream(document, page, AppendMode.APPEND, false, true);

    PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document, 200, 200);

    // SVG Salamander
    SVGUniverse universSVG = new SVGUniverse();
    SVGDiagram diagramSVG = universSVG.getDiagram(universSVG.loadSVG(svg.toURI().toURL()));
    diagramSVG.render(pdfBoxGraphics2D);
    pdfBoxGraphics2D.dispose();

    var xform = pdfBoxGraphics2D.getXFormObject();

    var transform = AffineTransform.getTranslateInstance(200, 200);
    xform.setMatrix(transform);

    contenu.drawForm(xform);

    contenu.close();

    document.save("D://tests/archi/PDF_avec_elements_graphique.pdf");
    document.close();
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

private PieChart camembertXChart() {
  PieChart camembert2 = new PieChartBuilder().width(200).height(200).build();
  Color[] sliceColors = new Color[] {
    new Color(183, 203, 231),
    new Color(131, 173, 219),
    new Color(84, 143, 197),
    new Color(70, 120, 165) };
  camembert2.getStyler().setSeriesColors(sliceColors);
  camembert2.getStyler().setLegendVisible(false);
  camembert2.getStyler().setPlotBorderVisible(false);
  camembert2.getStyler().setChartTitleVisible(false);
  camembert2.getStyler().setChartTitleBoxVisible(false);
  camembert2.getStyler().setChartPadding(0);
  camembert2.getStyler().setChartTitlePadding(0);
  camembert2.getStyler().setBorderWidth(3);
  camembert2.getStyler().setAnnotationsFont(new Font("Arial", Font.BOLD, 10));
  camembert2.getStyler().setAnnotationsFontColor(Color.WHITE);
  camembert2.addSeries("Item 1", 9);
  camembert2.addSeries("Item 2", 10);
  camembert2.addSeries("Item 3", 23);
  camembert2.addSeries("Item 4", 58);
  return camembert2;
}

Does anyone have any experience in an equivalent development ? Or does anybody know why the PDF is empty?

Thanks and regards,

Thomas.

0

There are 0 answers