001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: ExampleSVG2PDF.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package embedding;
021:
022: //Java
023: import java.io.File;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.OutputStream;
027:
028: //Batik
029: import org.apache.batik.transcoder.Transcoder;
030: import org.apache.batik.transcoder.TranscoderException;
031: import org.apache.batik.transcoder.TranscoderInput;
032: import org.apache.batik.transcoder.TranscoderOutput;
033:
034: //FOP
035: import org.apache.fop.svg.PDFTranscoder;
036:
037: /**
038: * This class demonstrates the conversion of an SVG file to PDF using FOP.
039: */
040: public class ExampleSVG2PDF {
041:
042: /**
043: * Converts an FO file to a PDF file using FOP
044: * @param svg the SVG file
045: * @param pdf the target PDF file
046: * @throws IOException In case of an I/O problem
047: * @throws TranscoderException In case of a transcoding problem
048: */
049: public void convertSVG2PDF(File svg, File pdf) throws IOException,
050: TranscoderException {
051:
052: //Create transcoder
053: Transcoder transcoder = new PDFTranscoder();
054: //Transcoder transcoder = new org.apache.fop.render.ps.PSTranscoder();
055:
056: //Setup input
057: InputStream in = new java.io.FileInputStream(svg);
058: try {
059: TranscoderInput input = new TranscoderInput(in);
060:
061: //Setup output
062: OutputStream out = new java.io.FileOutputStream(pdf);
063: out = new java.io.BufferedOutputStream(out);
064: try {
065: TranscoderOutput output = new TranscoderOutput(out);
066:
067: //Do the transformation
068: transcoder.transcode(input, output);
069: } finally {
070: out.close();
071: }
072: } finally {
073: in.close();
074: }
075: }
076:
077: /**
078: * Main method.
079: * @param args command-line arguments
080: */
081: public static void main(String[] args) {
082: try {
083: System.out.println("FOP ExampleSVG2PDF\n");
084: System.out.println("Preparing...");
085:
086: //Setup directories
087: File baseDir = new File(".");
088: File outDir = new File(baseDir, "out");
089: outDir.mkdirs();
090:
091: //Setup input and output files
092: File svgfile = new File(baseDir, "xml/svg/helloworld.svg");
093: File pdffile = new File(outDir, "ResultSVG2PDF.pdf");
094:
095: System.out.println("Input: SVG (" + svgfile + ")");
096: System.out.println("Output: PDF (" + pdffile + ")");
097: System.out.println();
098: System.out.println("Transforming...");
099:
100: ExampleSVG2PDF app = new ExampleSVG2PDF();
101: app.convertSVG2PDF(svgfile, pdffile);
102:
103: System.out.println("Success!");
104: } catch (Exception e) {
105: e.printStackTrace(System.err);
106: System.exit(-1);
107: }
108: }
109: }
|