There's something which I do not understand going on between getting the SVG code from the SQL table and passing it into Batik to transcode it (displaying the PNG).
If I run the CF code below I get an error: The image cannot be displayed because it contains errors. This is the only error any of this produces.
I also tested the value from the table with isXML() which returns true.
But if I:
- un-comment Section 1,
- COPY the SVG code produced by the CFDUMP,
- paste that SVG code back into the CFSET in Section 2,
- and re-comment-out comment section 1
THEN the PNG displays in the browser as expected. The process of displaying the SVG code in CFDUMP and then manually pasting it into the code to use it somehow (mysteriously) alters it so it works...
I've tried all sorts of variations. It seems like ColdFusion is doing something I don't understand when it displays the SVG code in CFDUMP (OR possibly is it some sort of charset issue from SQL?).
Anyone have any ideas what this might be? I'm stymied.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<CFQUERY NAME="getSketch">
SELECT sketch_svg AS svg
FROM sketch
WHERE ordID = 1
</CFQUERY>
<cfset svg = Replace(getSketch.svg,'"','','all')>
<!--- Commented-out Section 1
<cfset svg = Replace(svg,"'","''",'all')>
<cfset svg = Replace(svg,"##","####",'all')>
<CFDUMP var="#svg#" abort>
--->
<!--- Commented-out Section 2
<cfset svg = '[Manually paste SVG code from CFDUMP here...]'>
--->
<!--- The following takes the svg code and displays it as a PNG in the browser - This works fine if the SVG code is okay...) --->
<cfscript>
context = getPageContext();
context.setFlushOutput(false);
response = context.getResponse().getResponse();
response.setContentType("image/png");
transcoder = createObject("java", "org.apache.batik.transcoder.image.PNGTranscoder").init();
inputStream = createObject("java", "java.io.StringBufferInputStream").init(svg);
input = createObject("java", "org.apache.batik.transcoder.TranscoderInput").init(inputStream);
outputStream = response.getOutputStream();
output = createObject("java", "org.apache.batik.transcoder.TranscoderOutput").init(outputStream);
transcoder.transcode(input, output);
output.close();
outputStream.close();
</cfscript>