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.4KB

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