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.

ExampleFO2JPSPrint.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 javax.print.Doc;
  23. import javax.print.DocFlavor;
  24. import javax.print.DocPrintJob;
  25. import javax.print.PrintException;
  26. import javax.print.PrintService;
  27. import javax.print.PrintServiceLookup;
  28. import javax.print.ServiceUI;
  29. import javax.print.SimpleDoc;
  30. import javax.print.attribute.HashPrintRequestAttributeSet;
  31. import javax.print.attribute.PrintRequestAttributeSet;
  32. import javax.xml.transform.Result;
  33. import javax.xml.transform.Source;
  34. import javax.xml.transform.Transformer;
  35. import javax.xml.transform.TransformerException;
  36. import javax.xml.transform.TransformerFactory;
  37. import javax.xml.transform.sax.SAXResult;
  38. import javax.xml.transform.stream.StreamSource;
  39. import org.apache.fop.apps.FOPException;
  40. import org.apache.fop.apps.FOUserAgent;
  41. import org.apache.fop.apps.Fop;
  42. import org.apache.fop.apps.FopFactory;
  43. import org.apache.fop.render.print.PageableRenderer;
  44. /**
  45. * This class demonstrates printing an FO file using JPS (Java Printing System).
  46. */
  47. public class ExampleFO2JPSPrint {
  48. // configure fopFactory as desired
  49. private final FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  50. private DocPrintJob createDocPrintJob() {
  51. PrintService[] services = PrintServiceLookup.lookupPrintServices(
  52. DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
  53. PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
  54. PrintService printService = ServiceUI.printDialog(null, 50, 50,
  55. services, services[0], null, attributes);
  56. if (printService != null) {
  57. return printService.createPrintJob();
  58. } else {
  59. return null;
  60. }
  61. }
  62. /**
  63. * Prints an FO file using JPS.
  64. * @param fo the FO file
  65. * @throws IOException In case of an I/O problem
  66. * @throws FOPException In case of a FOP problem
  67. * @throws TransformerException In case of a problem during XSLT processing
  68. * @throws PrintException If an error occurs while printing
  69. */
  70. public void printFO(File fo)
  71. throws IOException, FOPException, TransformerException, PrintException {
  72. //Set up DocPrintJob instance
  73. DocPrintJob printJob = createDocPrintJob();
  74. //Set up a custom user agent so we can supply our own renderer instance
  75. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  76. PageableRenderer renderer = new PageableRenderer(userAgent);
  77. userAgent.setRendererOverride(renderer);
  78. // Construct FOP with desired output format
  79. Fop fop = fopFactory.newFop(userAgent);
  80. // Setup JAXP using identity transformer
  81. TransformerFactory factory = TransformerFactory.newInstance();
  82. Transformer transformer = factory.newTransformer(); // identity transformer
  83. // Setup input stream
  84. Source src = new StreamSource(fo);
  85. // Resulting SAX events (the generated FO) must be piped through to FOP
  86. Result res = new SAXResult(fop.getDefaultHandler());
  87. // Start XSLT transformation and FOP processing
  88. transformer.transform(src, res);
  89. Doc doc = new SimpleDoc(renderer, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
  90. printJob.print(doc, null);
  91. }
  92. /**
  93. * Main method.
  94. * @param args command-line arguments
  95. */
  96. public static void main(String[] args) {
  97. try {
  98. System.out.println("FOP ExampleFO2JPSPrint\n");
  99. System.out.println("Preparing...");
  100. //Setup directories
  101. File baseDir = new File(".");
  102. File outDir = new File(baseDir, "out");
  103. outDir.mkdirs();
  104. //Setup input and output files
  105. File fofile = new File(baseDir, "xml/fo/helloworld.fo");
  106. System.out.println("Input: XSL-FO (" + fofile + ")");
  107. System.out.println("Output: JPS (Java Printing System)");
  108. System.out.println();
  109. System.out.println("Transforming...");
  110. ExampleFO2JPSPrint app = new ExampleFO2JPSPrint();
  111. app.printFO(fofile);
  112. System.out.println("Success!");
  113. } catch (Exception e) {
  114. e.printStackTrace(System.err);
  115. System.exit(-1);
  116. }
  117. }
  118. }