import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import org.apache.batik.transcoder.Transcoder; import org.apache.batik.transcoder.TranscoderException; import org.apache.batik.transcoder.TranscoderInput; import org.apache.batik.transcoder.TranscoderOutput; import org.apache.fop.svg.PDFTranscoder; public class Test { public static void main(String[] argv) throws TranscoderException, FileNotFoundException { Transcoder transcoder = new PDFTranscoder(); TranscoderInput transcoderInput = new TranscoderInput(new FileInputStream(new File("/tmp/test.svg"))); TranscoderOutput transcoderOutput = new TranscoderOutput(new FileOutputStream(new File("/tmp/test.pdf"))); transcoder.transcode(transcoderInput, transcoderOutput); } }
/** * Transcode a document into a file using the given transcoder. * * @param file Output file * @param transcoder Transcoder to use * @throws IOException On write errors * @throws TranscoderException On input/parsing errors */ protected void transcode(File file, Transcoder transcoder) throws IOException, TranscoderException { // Disable validation, performance is more important here (thumbnails!) transcoder.addTranscodingHint(XMLAbstractTranscoder.KEY_XML_PARSER_VALIDATING, Boolean.FALSE); SVGDocument doc = cloneDocument(); TranscoderInput input = new TranscoderInput(doc); OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); TranscoderOutput output = new TranscoderOutput(out); transcoder.transcode(input, output); out.flush(); out.close(); }
/** * 将SVG输入流转换并写入到JPEG输出流 * * @param reader SVG字符流 * @param output JPEG输出流 * @throws TranscoderException 转换异常 */ public static void svg2jpeg(Reader reader, OutputStream output) throws TranscoderException { if (reader == null) { throw new IllegalArgumentException("Reader must not be null"); } if (output == null) { throw new IllegalArgumentException("OutputStream must not be null"); } Transcoder transcoder = new JPEGTranscoder(); transcoder.transcode(new TranscoderInput(reader), new TranscoderOutput(output)); }
/** * 将SVG输入流转换并写入到JPEG输出流 * * @param input SVG输入流 * @param output JPEG输出流 * @throws TranscoderException 转换异常 */ public static void svg2jpeg(InputStream input, OutputStream output) throws TranscoderException { if (input == null) { throw new IllegalArgumentException("InputStream must not be null"); } if (output == null) { throw new IllegalArgumentException("OutputStream must not be null"); } Transcoder transcoder = new JPEGTranscoder(); transcoder.transcode(new TranscoderInput(input), new TranscoderOutput(output)); }
/** * 将SVG输入流转换并写入到PNG输出流 * * @param input SVG输入流 * @param output PNG输出流 * @throws TranscoderException 转换异常 */ public static void svg2png(InputStream input, OutputStream output) throws TranscoderException { if (input == null) { throw new IllegalArgumentException("InputStream must not be null"); } if (output == null) { throw new IllegalArgumentException("OutputStream must not be null"); } Transcoder transcoder = new PNGTranscoder(); transcoder.transcode(new TranscoderInput(input), new TranscoderOutput(output)); }
/** * Transcode a document into a file using the given transcoder. * * @param file Output file * @param transcoder Transcoder to use * @throws IOException On write errors * @throws TranscoderException On input/parsing errors */ protected void transcode(File file, Transcoder transcoder) throws IOException, TranscoderException { // Disable validation, performance is more important here (thumbnails!) transcoder.addTranscodingHint(XMLAbstractTranscoder.KEY_XML_PARSER_VALIDATING, Boolean.FALSE); SVGDocument doc = cloneDocument(); TranscoderInput input = new TranscoderInput(doc); OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); TranscoderOutput output = new TranscoderOutput(out); transcoder.transcode(input, output); out.flush(); out.close(); }
/** * 将SVG输入流转换并写入到PNG输出流 * * @param reader SVG字符流 * @param output PNG输出流 * @throws TranscoderException 转换异常 */ public static void svg2png(Reader reader, OutputStream output) throws TranscoderException { if (reader == null) { throw new IllegalArgumentException("Reader must not be null"); } if (output == null) { throw new IllegalArgumentException("OutputStream must not be null"); } Transcoder transcoder = new PNGTranscoder(); transcoder.transcode(new TranscoderInput(reader), new TranscoderOutput(output)); }
/** * 将SVG输入流转换并写入到PDF输出流 * * @param reader SVG字符流 * @param output PDF输出流 * @throws TranscoderException 转换异常 */ public static void svg2pdf(Reader reader, OutputStream output) throws TranscoderException { if (reader == null) { throw new IllegalArgumentException("Reader must not be null"); } if (output == null) { throw new IllegalArgumentException("OutputStream must not be null"); } Transcoder transcoder = new PDFTranscoder(); transcoder.transcode(new TranscoderInput(reader), new TranscoderOutput(output)); }
/** * 将SVG输入流转换并写入到PDF输出流 * * @param input SVG输入流 * @param output PDF输出流 * @throws TranscoderException 转换异常 */ public static void svg2pdf(InputStream input, OutputStream output) throws TranscoderException { if (input == null) { throw new IllegalArgumentException("InputStream must not be null"); } if (output == null) { throw new IllegalArgumentException("OutputStream must not be null"); } Transcoder transcoder = new PDFTranscoder(); transcoder.transcode(new TranscoderInput(input), new TranscoderOutput(output)); }
/** * Runs the pretty printer. */ public void run() { if (arguments.length == 0) { printUsage(); return; } try { for (;;) { OptionHandler oh = (OptionHandler)handlers.get(arguments[index]); if (oh == null) { break; } oh.handleOption(); } TranscoderInput in; in = new TranscoderInput(new java.io.FileReader(arguments[index++])); TranscoderOutput out; if (index < arguments.length) { out = new TranscoderOutput(new java.io.FileWriter(arguments[index])); } else { out = new TranscoderOutput(new java.io.OutputStreamWriter(System.out)); } transcoder.transcode(in, out); } catch (Exception e) { e.printStackTrace(); printUsage(); } }
/** * Write an SVG image to a file with the given transcoder to determine the * format * * @param image * the image * @param output * the file to write to * @param trans * the transcoder * @throws IOException * if an error occurs during writing * @throws TranscoderException * if an error occurs during transcoding */ public static void write(final SVGImage image, final File output, Transcoder trans) throws IOException, TranscoderException { final TranscoderInput input = new TranscoderInput(image.createRenderer().getDocument()); final TranscoderOutput toutput = new TranscoderOutput(new FileOutputStream(output)); trans.transcode(input, toutput); } }
/** * Runs the pretty printer. */ public void run() { if (arguments.length == 0) { printUsage(); return; } try { for (;;) { OptionHandler oh = (OptionHandler)handlers.get(arguments[index]); if (oh == null) { break; } oh.handleOption(); } TranscoderInput in; in = new TranscoderInput(new java.io.FileReader(arguments[index++])); TranscoderOutput out; if (index < arguments.length) { out = new TranscoderOutput(new java.io.FileWriter(arguments[index])); } else { out = new TranscoderOutput(new java.io.OutputStreamWriter(System.out)); } transcoder.transcode(in, out); } catch (Exception e) { e.printStackTrace(); printUsage(); } }
public class SvgToPdfConverter { private ArrayList<byte[]> pdfs = new ArrayList<byte[]>(); public void convertSVG2PDF(File svg) throws IOException, TranscoderException { // Create transcoder Transcoder transcoder = new PDFTranscoder(); // Input FileInputStream bais = new FileInputStream(svg); // Output ByteArrayOutputStream baos = new ByteArrayOutputStream(); TranscoderInput input = new TranscoderInput(bais); TranscoderOutput output = new TranscoderOutput(baos); // Do the transformation transcoder.transcode(input, output); pdfs.add(baos.toByteArray()); } public void merge(File mergedPDF) throws IOException, COSVisitorException { PDFMergerUtility ut = new PDFMergerUtility(); for (byte[] bytes : pdfs) { ut.addSource(new ByteArrayInputStream(bytes)); } ut.setDestinationStream(new FileOutputStream(mergedPDF)); ut.mergeDocuments(); } }
/** * Write an SVG image to a file with the given transcoder to determine the * format * * @param image * the image * @param output * the file to write to * @param trans * the transcoder * @throws IOException * if an error occurs during writing * @throws TranscoderException * if an error occurs during transcoding */ public static void write(final SVGImage image, final File output, Transcoder trans) throws IOException, TranscoderException { final TranscoderInput input = new TranscoderInput(image.createRenderer().getDocument()); final TranscoderOutput toutput = new TranscoderOutput(new FileOutputStream(output)); trans.transcode(input, toutput); } }
/** * Receive notification of a successfully completed DOM tree generation. */ public void notify(Document doc) throws SAXException { try { TranscoderInput transInput = new TranscoderInput(doc); // Buffering is done by the pipeline (See shouldSetContentLength) TranscoderOutput transOutput = new TranscoderOutput(this.output); transcoder.transcode(transInput, transOutput); } catch (TranscoderException ex) { if (ex.getException() != null) { if (getLogger().isDebugEnabled()) { getLogger().debug("Got transcoder exception writing image, rethrowing nested exception", ex); } throw new SAXException("Exception writing image", ex.getException()); } if (getLogger().isDebugEnabled()) { getLogger().debug("Got transcoder exception writing image, rethrowing", ex); } throw new SAXException("Exception writing image", ex); } catch (Exception ex) { if (getLogger().isDebugEnabled()) { getLogger().debug("Got exception writing image, rethrowing", ex); } throw new SAXException("Exception writing image", ex); } }
/** * Transcodes the image using the transcoder specified * * @param t * @param svgGenerator * @param useCSS * @param rect * @param fos * @throws IOException * @throws TranscoderException */ public static void transcode(Transcoder t, SVGGraphics2D svgGenerator, boolean useCSS, Rectangle rect, FileOutputStream fos) throws IOException, TranscoderException { PipedWriter pout = new PipedWriter(); BufferedReader rin = new BufferedReader(new PipedReader(pout)); new Piper(svgGenerator, useCSS, pout).start(); t.addTranscodingHint(JPEGTranscoder.KEY_HEIGHT, Float.valueOf(rect.height)); t.addTranscodingHint(JPEGTranscoder.KEY_WIDTH, Float.valueOf(rect.width)); t.transcode(new TranscoderInput(rin), new TranscoderOutput(fos)); }
transcoder.transcode(transcoderInput, transcoderOutput);
transcoder.transcode(input, output); success = true; } catch(Exception te) {
transcoder.transcode(input, output); success = true; } catch(Exception te) {
transcoder.transcode(input, output); success = true; } catch(Exception te) {