1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /* $Id: ExampleSVG2PDF.java 679326 2008-07-24 09:35:34Z vhennebert $ */
19 
20 package embedding;
21 
22 //Java
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 
28 //Batik
29 import org.apache.batik.transcoder.Transcoder;
30 import org.apache.batik.transcoder.TranscoderException;
31 import org.apache.batik.transcoder.TranscoderInput;
32 import org.apache.batik.transcoder.TranscoderOutput;
33 
34 //FOP
35 import org.apache.fop.svg.PDFTranscoder;
36 
37 /**
38  * This class demonstrates the conversion of an SVG file to PDF using FOP.
39  */
40 public class ExampleSVG2PDF {
41 
42     /**
43      * Converts an FO file to a PDF file using FOP
44      * @param svg the SVG file
45      * @param pdf the target PDF file
46      * @throws IOException In case of an I/O problem
47      * @throws TranscoderException In case of a transcoding problem
48      */
convertSVG2PDF(File svg, File pdf)49     public void convertSVG2PDF(File svg, File pdf) throws IOException, TranscoderException {
50 
51         //Create transcoder
52         Transcoder transcoder = new PDFTranscoder();
53         //Transcoder transcoder = new org.apache.fop.render.ps.PSTranscoder();
54 
55         //Setup input
56         InputStream in = new java.io.FileInputStream(svg);
57         try {
58             TranscoderInput input = new TranscoderInput(in);
59 
60             //Setup output
61             OutputStream out = new java.io.FileOutputStream(pdf);
62             out = new java.io.BufferedOutputStream(out);
63             try {
64                 TranscoderOutput output = new TranscoderOutput(out);
65 
66                 //Do the transformation
67                 transcoder.transcode(input, output);
68             } finally {
69                 out.close();
70             }
71         } finally {
72             in.close();
73         }
74     }
75 
76 
77     /**
78      * Main method.
79      * @param args command-line arguments
80      */
main(String[] args)81     public static void main(String[] args) {
82         try {
83             System.out.println("FOP ExampleSVG2PDF\n");
84             System.out.println("Preparing...");
85 
86             //Setup directories
87             File baseDir = new File(".");
88             File outDir = new File(baseDir, "out");
89             outDir.mkdirs();
90 
91             //Setup input and output files
92             File svgfile = new File(baseDir, "xml/svg/helloworld.svg");
93             File pdffile = new File(outDir, "ResultSVG2PDF.pdf");
94 
95             System.out.println("Input: SVG (" + svgfile + ")");
96             System.out.println("Output: PDF (" + pdffile + ")");
97             System.out.println();
98             System.out.println("Transforming...");
99 
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 }
110