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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 FopFactory fopFactory = FopFactory.newInstance();
  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();
  77. renderer.setUserAgent(userAgent);
  78. userAgent.setRendererOverride(renderer);
  79. // Construct FOP with desired output format
  80. Fop fop = fopFactory.newFop(userAgent);
  81. // Setup JAXP using identity transformer
  82. TransformerFactory factory = TransformerFactory.newInstance();
  83. Transformer transformer = factory.newTransformer(); // identity transformer
  84. // Setup input stream
  85. Source src = new StreamSource(fo);
  86. // Resulting SAX events (the generated FO) must be piped through to FOP
  87. Result res = new SAXResult(fop.getDefaultHandler());
  88. // Start XSLT transformation and FOP processing
  89. transformer.transform(src, res);
  90. Doc doc = new SimpleDoc(renderer, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
  91. printJob.print(doc, null);
  92. }
  93. /**
  94. * Main method.
  95. * @param args command-line arguments
  96. */
  97. public static void main(String[] args) {
  98. try {
  99. System.out.println("FOP ExampleFO2JPSPrint\n");
  100. System.out.println("Preparing...");
  101. //Setup directories
  102. File baseDir = new File(".");
  103. File outDir = new File(baseDir, "out");
  104. outDir.mkdirs();
  105. //Setup input and output files
  106. File fofile = new File(baseDir, "xml/fo/helloworld.fo");
  107. System.out.println("Input: XSL-FO (" + fofile + ")");
  108. System.out.println("Output: JPS (Java Printing System)");
  109. System.out.println();
  110. System.out.println("Transforming...");
  111. ExampleFO2JPSPrint app = new ExampleFO2JPSPrint();
  112. app.printFO(fofile);
  113. System.out.println("Success!");
  114. } catch (Exception e) {
  115. e.printStackTrace(System.err);
  116. System.exit(-1);
  117. }
  118. }
  119. }