Uploaded image for project: 'Batik'
  1. Batik
  2. BATIK-987

Generating PDF using iText and PrintTranscoder ignores translucent shapes

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Resolution: Unresolved
    • 1.6
    • None
    • GVT
    • None
    • Operating System: All
      Platform: PC

    Description

      I have a JPanel containing a graph (it's the JFreeChart's ChartPanel). It contains few bars, lines, texts, and also translucent rounded rectangles.
      I need to insert this graph into PDF report generated by Eclipse BIRT. So I have to transform my graf into an SVG image and put it into the BIRT.

      It's done by this Batik code:

      DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
      Document document = domImpl.createDocument("http://www.w3.org/2000/svg", "svg", null);

      // Create an instance of the SVG Graphic
      SVGGraphics2D svgGraphic = new SVGGraphics2D(document);

      // draw the chart in the SVG generator
      Rectangle2D bounds = new Rectangle(100, 500);
      chart.draw(svgGraphic, bounds);

      // Write svg file
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      Writer out = new OutputStreamWriter(outputStream, "UTF-8");
      svgGraphic.stream(out, true /* use css styles */);
      outputStream.flush();
      outputStream.close();
      byte[] svgData = outputStream.toByteArray();

      So I have the SVG in form of byte array. Ok, now I give this array to the Eclipse BIRT Framework. I did some investigation and I found that BIRT uses
      the code below to insert an svg image into PDF report(of course it's a bit adapted for this bug report and to enable me to provide some results):

      byte[] svgData = ... viz the code above
      File fileName = new File("C:
      bad.pdf");

      OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));

      com.lowagie.text.Rectangle pagesize = new com.lowagie.text.Rectangle(1000, 500);
      com.lowagie.text.Document document = new com.lowagie.text.Document(pagesize, 50, 50, 50, 50);
      PdfWriter writer = PdfWriter.getInstance(document, out);
      document.open();

      PdfContentByte contentByte = writer.getDirectContent();
      PdfTemplate template = contentByte.createTemplate(1000, 500);
      Graphics2D g2D = template.createGraphics(1000, 500);

      PrintTranscoder transcoder = new PrintTranscoder();
      transcoder.transcode(new TranscoderInput(new ByteArrayInputStream(data)), null);
      PageFormat pg = new PageFormat();
      Paper p = new Paper();
      p.setSize(1000, 500);
      p.setImageableArea(0, 0, 1000, 500);
      pg.setPaper(p);
      transcoder.print(g2D, pg, 0);
      g2D.dispose();
      contentByte.addTemplate(template, 0, 0);

      document.close();
      out.close();

      The resulting PDF really contains my graph, but it absolutely ignores translucent shapes as you can see in the attached file "bad.pdf".
      So I did "my way" PDF generation directly with the iText:

      File fileName = new File("C:
      good.pdf");
      OutputStream out = new BufferedOutputStream(new FileOutputStream(file));

      com.lowagie.text.Rectangle pagesize = new com.lowagie.text.Rectangle(1000, 500);
      com.lowagie.text.Document document = com.lowagie.text.new Document(pagesize, 50, 50, 50, 50);

      PdfWriter writer = PdfWriter.getInstance(document, out);
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      PdfTemplate tp = cb.createTemplate(1000, 500);
      Graphics2D g2 = tp.createGraphics(1000, 500, new DefaultFontMapper());
      Rectangle2D r2D = new Rectangle2D.Double(0, 0, 1000, 500);
      chart.draw(g2, r2D);
      g2.dispose();
      cb.addTemplate(tp, 0, 0);

      document.close();
      out.close();

      As you can see I the part with the PrintTranscoder is skipped and graph is drawn directly to the PDF using iText's Graphics implementation.
      See the attached file "good.pdf". The result is perfect. I thought that the generated SVG can be broken, but saving it as SVG file and
      opening it in the GIMP for example give me good results. So the problem must be somewhere in the PrintTranscoder.

      Attachments

        1. graph.svg
          47 kB
          jan.krakora.cz
        2. bad.pdf
          64 kB
          jan.krakora.cz
        3. good.pdf
          7 kB
          jan.krakora.cz

        Activity

          People

            batik-dev@xmlgraphics.apache.org Batik Developer's Mailing list
            jan.krakora.cz@gmail.com jan.krakora.cz
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: