0

I have noticed that when I use Apache Batik to transcode SVG documents to PDF's that have an opacity set to less than one Batik will create a raster version of the SVG and place it in the pdf instead. When dealing with print this is not desirable. Is there any reason Batik does this? Is there anyway to avoid this flattening of SVG documents regardless of their opacity?

Our code to create the transcoder:

PDFTranscoder pdfTranscoder = new PDFTranscoder();
pdfTranscoder.addTranscodingHint(PDFTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER, PIXEL_CONVERSION);
pdfTranscoder.addTranscodingHint(PDFTranscoder.KEY_AUTO_FONTS, false);

We then take the SVG which is returned form element.getEncodedData() as an SVG string.

TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(element.getEncodedData().getBytes()));
TranscoderOutput output = new TranscoderOutput(byteStream);
pdfTranscoder.transcode(input, output);

For opacity we edit the SVG adding a group. Consider the following svg: Note many markup tags have been removed to keep the example concise:

<svg>
    <rect x="100" y="100" width="100" height="100" />
</svg>

We would edit this SVG to appear as

<svg>
    <g opacity="0.5">
        <rect x="100" y="100" width="100" height="100" />
    </g>
</svg>
Stewart
  • 3,023
  • 2
  • 24
  • 40
  • Why the pdfbox label? And does SVG support transparency? – Tilman Hausherr Dec 12 '18 at 06:42
  • Hi @TilmanHausherr thanks for your reply. Opacity in SVG's is supported. https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/opacity . I added the PDFBox tag as I'm using PDFBOX to handle the result of the Batik encoding. While I suspect the issue was due to Batik I wanted to cover all bases. I will update the question. – Stewart Dec 12 '18 at 22:13
  • PDFBox uses bitmaps to draw "transparency groups" and patterns when it renders an existing PDF. Still not sure if this is the answer or just a behavior similar to batik. – Tilman Hausherr Dec 13 '18 at 04:39
  • @TilmanHausherr can you define what you mean by "transparency group"? Are you referring to the PDF specification or are you referring to a batik transcoded tag in a SVG with opacity set to less than one? – Stewart Dec 17 '18 at 01:09
  • The PDF specification. – Tilman Hausherr Dec 17 '18 at 02:05
  • Well if Batik renders an SVG with opacity as a PDF transparency group and PDFBox renders this as a bitmap this means that it's not possible to render a transparent SVG's with these two tools combined. So my question is should Batik render the SVG as something other than a transparency group or should PDFBox not convert a transparency group to a bitmap? – Stewart Dec 17 '18 at 03:24
  • IF. I don't know because the whole thing is unclear. You didn't share an SVG and a PDF or provide code to show what's going on. PDFBox uses a bitmap when rendering a PDF, i.e. when drawing to a graphics device (which is usually a bitmap, but not always). I know what PDFBox is doing but I don't know what BATIK is doing or what you are doing. – Tilman Hausherr Dec 17 '18 at 07:44
  • Hi @TilmanHausherr I have updated with some of the code we are running. Please let me know if that is enough detail. – Stewart Dec 17 '18 at 23:33
  • I just spent half an hour trying to get this to run. Please include the maven dependencies, and the missing declarations. What is "PIXEL_CONVERSION"? I somehow got it to run with a lot of researching the missing parts, and now I get "NoSuchFieldError: MIME_TYPES_SVG_LIST". – Tilman Hausherr Dec 18 '18 at 08:31
  • Also tell where `element.getEncodedData()` comes from, and provide an SVG file that works. I.e. test whether your minimal example works or if it requires extra research. I was not even able to get it to run with an SVG document generated by batik itself. – Tilman Hausherr Dec 18 '18 at 08:55

0 Answers0