You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExampleSVG2PDF.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /* $Id$ */
  18. package embedding;
  19. //Java
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. //Batik
  25. import org.apache.batik.transcoder.Transcoder;
  26. import org.apache.batik.transcoder.TranscoderException;
  27. import org.apache.batik.transcoder.TranscoderInput;
  28. import org.apache.batik.transcoder.TranscoderOutput;
  29. //FOP
  30. import org.apache.fop.svg.PDFTranscoder;
  31. /**
  32. * This class demonstrates the conversion of an SVG file to PDF using FOP.
  33. */
  34. public class ExampleSVG2PDF {
  35. /**
  36. * Converts an FO file to a PDF file using FOP
  37. * @param svg the SVG file
  38. * @param pdf the target PDF file
  39. * @throws IOException In case of an I/O problem
  40. * @throws TranscoderException In case of a transcoding problem
  41. */
  42. public void convertSVG2PDF(File svg, File pdf) throws IOException, TranscoderException {
  43. //Create transcoder
  44. Transcoder transcoder = new PDFTranscoder();
  45. //Transcoder transcoder = new org.apache.fop.render.ps.PSTranscoder();
  46. //Setup input
  47. InputStream in = new java.io.FileInputStream(svg);
  48. try {
  49. TranscoderInput input = new TranscoderInput(in);
  50. //Setup output
  51. OutputStream out = new java.io.FileOutputStream(pdf);
  52. out = new java.io.BufferedOutputStream(out);
  53. try {
  54. TranscoderOutput output = new TranscoderOutput(out);
  55. //Do the transformation
  56. transcoder.transcode(input, output);
  57. } finally {
  58. out.close();
  59. }
  60. } finally {
  61. in.close();
  62. }
  63. }
  64. /**
  65. * Main method.
  66. * @param args command-line arguments
  67. */
  68. public static void main(String[] args) {
  69. try {
  70. System.out.println("FOP ExampleSVG2PDF\n");
  71. System.out.println("Preparing...");
  72. //Setup directories
  73. File baseDir = new File(".");
  74. File outDir = new File(baseDir, "out");
  75. outDir.mkdirs();
  76. //Setup input and output files
  77. File svgfile = new File(baseDir, "xml/svg/helloworld.svg");
  78. File pdffile = new File(outDir, "ResultSVG2PDF.pdf");
  79. System.out.println("Input: SVG (" + svgfile + ")");
  80. System.out.println("Output: PDF (" + pdffile + ")");
  81. System.out.println();
  82. System.out.println("Transforming...");
  83. ExampleSVG2PDF app = new ExampleSVG2PDF();
  84. app.convertSVG2PDF(svgfile, pdffile);
  85. System.out.println("Success!");
  86. } catch (Exception e) {
  87. e.printStackTrace(System.err);
  88. System.exit(-1);
  89. }
  90. }
  91. }