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.

PrintRenderer.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 org.apache.fop.render.print;
  19. import java.awt.print.PrinterException;
  20. import java.awt.print.PrinterJob;
  21. import java.io.IOException;
  22. import java.util.Map;
  23. import org.apache.fop.apps.FOUserAgent;
  24. /**
  25. * Renderer that prints through java.awt.PrintJob.
  26. * The actual printing is handled by Java2DRenderer
  27. * since both PrintRenderer and AWTRenderer need to
  28. * support printing.
  29. */
  30. public class PrintRenderer extends PageableRenderer {
  31. /**
  32. * Printing parameter: the preconfigured PrinterJob to use,
  33. * datatype: java.awt.print.PrinterJob
  34. */
  35. public static final String PRINTER_JOB = "printerjob";
  36. /**
  37. * Printing parameter: the number of copies of the document to be printed,
  38. * datatype: a positive Integer
  39. */
  40. public static final String COPIES = "copies";
  41. private int copies = 1;
  42. private PrinterJob printerJob;
  43. /**
  44. * Creates a new PrintRenderer with the options set through the renderer options if a custom
  45. * PrinterJob is not given in FOUserAgent's renderer options.
  46. *
  47. * @param userAgent the user agent that contains configuration details. This cannot be null.
  48. */
  49. public PrintRenderer(FOUserAgent userAgent) {
  50. super(userAgent);
  51. setRendererOptions();
  52. }
  53. private void initializePrinterJob() {
  54. if (this.printerJob == null) {
  55. printerJob = PrinterJob.getPrinterJob();
  56. printerJob.setJobName("FOP Document");
  57. printerJob.setCopies(copies);
  58. if (System.getProperty("dialog") != null) {
  59. if (!printerJob.printDialog()) {
  60. throw new RuntimeException(
  61. "Printing cancelled by operator");
  62. }
  63. }
  64. printerJob.setPageable(this);
  65. }
  66. }
  67. private void setRendererOptions() {
  68. Map rendererOptions = getUserAgent().getRendererOptions();
  69. Object printerJobO = rendererOptions.get(PrintRenderer.PRINTER_JOB);
  70. if (printerJobO != null) {
  71. if (!(printerJobO instanceof PrinterJob)) {
  72. throw new IllegalArgumentException(
  73. "Renderer option " + PrintRenderer.PRINTER_JOB
  74. + " must be an instance of java.awt.print.PrinterJob, but an instance of "
  75. + printerJobO.getClass().getName() + " was given.");
  76. }
  77. printerJob = (PrinterJob)printerJobO;
  78. printerJob.setPageable(this);
  79. }
  80. Object o = rendererOptions.get(PrintRenderer.COPIES);
  81. if (o != null) {
  82. this.copies = getPositiveInteger(o);
  83. }
  84. initializePrinterJob();
  85. }
  86. /** @return the PrinterJob instance that this renderer prints to */
  87. public PrinterJob getPrinterJob() {
  88. return this.printerJob;
  89. }
  90. /** @return the ending page number */
  91. public int getEndNumber() {
  92. return endNumber;
  93. }
  94. /**
  95. * Sets the number of the last page to be printed.
  96. * @param end The ending page number
  97. */
  98. public void setEndPage(int end) {
  99. this.endNumber = end;
  100. }
  101. /** @return the starting page number */
  102. public int getStartPage() {
  103. return startNumber;
  104. }
  105. /**
  106. * Sets the number of the first page to be printed.
  107. * @param start The starting page number
  108. */
  109. public void setStartPage(int start) {
  110. this.startNumber = start;
  111. }
  112. /** {@inheritDoc} */
  113. public void stopRenderer() throws IOException {
  114. super.stopRenderer();
  115. try {
  116. printerJob.print();
  117. } catch (PrinterException e) {
  118. log.error(e);
  119. throw new IOException("Unable to print: " + e.getClass().getName()
  120. + ": " + e.getMessage());
  121. }
  122. clearViewportList();
  123. }
  124. }