001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine;
029:
030: import java.awt.Image;
031: import java.awt.print.PrinterJob;
032: import java.io.InputStream;
033: import java.io.OutputStream;
034: import java.lang.reflect.Method;
035:
036: import net.sf.jasperreports.engine.print.JRPrinterAWT;
037: import net.sf.jasperreports.engine.util.JRClassLoader;
038: import net.sf.jasperreports.engine.util.JRLoader;
039:
040: /**
041: * Façade class for the JasperReports engine.
042: *
043: * @author Teodor Danciu (teodord@users.sourceforge.net)
044: * @version $Id: JasperPrintManager.java 1712 2007-04-30 18:04:51Z teodord $
045: */
046: public class JasperPrintManager {
047:
048: /**
049: *
050: */
051: private static boolean isJre14orLater = true;
052:
053: static {
054: isJre14orLater = true;
055: try {
056: PrinterJob.class.getMethod("lookupPrintServices",
057: (Class[]) null);
058: } catch (NoSuchMethodException e) {
059: isJre14orLater = false;
060: }
061: }
062:
063: /**
064: * @deprecated Replaced by {@link JasperExportManager#exportReportToPdfFile(String)}.
065: */
066: public static String printReportToPdfFile(String sourceFileName)
067: throws JRException {
068: return JasperExportManager
069: .exportReportToPdfFile(sourceFileName);
070: }
071:
072: /**
073: * @deprecated Replaced by {@link JasperExportManager#exportReportToPdfFile(String, String)}.
074: */
075: public static void printReportToPdfFile(String sourceFileName,
076: String destFileName) throws JRException {
077: JasperExportManager.exportReportToPdfFile(sourceFileName,
078: destFileName);
079: }
080:
081: /**
082: * @deprecated Replaced by {@link JasperExportManager#exportReportToPdfFile(JasperPrint, String)}.
083: */
084: public static void printReportToPdfFile(JasperPrint jasperPrint,
085: String destFileName) throws JRException {
086: JasperExportManager.exportReportToPdfFile(jasperPrint,
087: destFileName);
088: }
089:
090: /**
091: * @deprecated Replaced by {@link JasperExportManager#exportReportToPdfStream(InputStream, OutputStream)}.
092: */
093: public static void printReportToPdfStream(InputStream inputStream,
094: OutputStream outputStream) throws JRException {
095: JasperExportManager.exportReportToPdfStream(inputStream,
096: outputStream);
097: }
098:
099: /**
100: * @deprecated Replaced by {@link JasperExportManager#exportReportToPdfStream(JasperPrint, OutputStream)}.
101: */
102: public static void printReportToPdfStream(JasperPrint jasperPrint,
103: OutputStream outputStream) throws JRException {
104: JasperExportManager.exportReportToPdfStream(jasperPrint,
105: outputStream);
106: }
107:
108: /**
109: * @deprecated Replaced by {@link JasperExportManager#exportReportToPdf(JasperPrint)}.
110: */
111: public static byte[] printReportToPdf(JasperPrint jasperPrint)
112: throws JRException {
113: return JasperExportManager.exportReportToPdf(jasperPrint);
114: }
115:
116: /**
117: * @deprecated Replaced by {@link JasperExportManager#exportReportToXmlFile(String, boolean)}.
118: */
119: public static String printReportToXmlFile(String sourceFileName,
120: boolean isEmbeddingImages) throws JRException {
121: return JasperExportManager.exportReportToXmlFile(
122: sourceFileName, isEmbeddingImages);
123: }
124:
125: /**
126: * @deprecated Replaced by {@link JasperExportManager#exportReportToXmlFile(String, String, boolean)}.
127: */
128: public static void printReportToXmlFile(String sourceFileName,
129: String destFileName, boolean isEmbeddingImages)
130: throws JRException {
131: JasperExportManager.exportReportToXmlFile(sourceFileName,
132: destFileName, isEmbeddingImages);
133: }
134:
135: /**
136: * @deprecated Replaced by {@link JasperExportManager#exportReportToXmlFile(JasperPrint, String, boolean)}.
137: */
138: public static void printReportToXmlFile(JasperPrint jasperPrint,
139: String destFileName, boolean isEmbeddingImages)
140: throws JRException {
141: JasperExportManager.exportReportToXmlFile(jasperPrint,
142: destFileName, isEmbeddingImages);
143: }
144:
145: /**
146: * @deprecated Replaced by {@link JasperExportManager#exportReportToXmlStream(InputStream, OutputStream)}.
147: */
148: public static void printReportToXmlStream(InputStream inputStream,
149: OutputStream outputStream) throws JRException {
150: JasperExportManager.exportReportToXmlStream(inputStream,
151: outputStream);
152: }
153:
154: /**
155: * @deprecated Replaced by {@link JasperExportManager#exportReportToXmlStream(JasperPrint, OutputStream)}.
156: */
157: public static void printReportToXmlStream(JasperPrint jasperPrint,
158: OutputStream outputStream) throws JRException {
159: JasperExportManager.exportReportToXmlStream(jasperPrint,
160: outputStream);
161: }
162:
163: /**
164: * @deprecated Replaced by {@link JasperExportManager#exportReportToXml(JasperPrint)}.
165: */
166: public static String printReportToXml(JasperPrint jasperPrint)
167: throws JRException {
168: return JasperExportManager.exportReportToXml(jasperPrint);
169: }
170:
171: /**
172: *
173: */
174: public static boolean printReport(String sourceFileName,
175: boolean withPrintDialog) throws JRException {
176: JasperPrint jasperPrint = (JasperPrint) JRLoader
177: .loadObject(sourceFileName);
178:
179: return printReport(jasperPrint, withPrintDialog);
180: }
181:
182: /**
183: *
184: */
185: public static boolean printReport(InputStream inputStream,
186: boolean withPrintDialog) throws JRException {
187: JasperPrint jasperPrint = (JasperPrint) JRLoader
188: .loadObject(inputStream);
189:
190: return printReport(jasperPrint, withPrintDialog);
191: }
192:
193: /**
194: *
195: */
196: public static boolean printReport(JasperPrint jasperPrint,
197: boolean withPrintDialog) throws JRException {
198: //artf1936
199: if (!checkAvailablePrinters()) {
200: throw new JRException("No printer available.");
201: }
202: //END - artf1936
203:
204: return printPages(jasperPrint, 0,
205: jasperPrint.getPages().size() - 1, withPrintDialog);
206: }
207:
208: /**
209: *
210: */
211: public static boolean printPage(String sourceFileName,
212: int pageIndex, boolean withPrintDialog) throws JRException {
213: JasperPrint jasperPrint = (JasperPrint) JRLoader
214: .loadObject(sourceFileName);
215:
216: return printPage(jasperPrint, pageIndex, withPrintDialog);
217: }
218:
219: /**
220: *
221: */
222: public static boolean printPage(InputStream inputStream,
223: int pageIndex, boolean withPrintDialog) throws JRException {
224: JasperPrint jasperPrint = (JasperPrint) JRLoader
225: .loadObject(inputStream);
226:
227: return printPage(jasperPrint, pageIndex, withPrintDialog);
228: }
229:
230: /**
231: *
232: */
233: public static boolean printPage(JasperPrint jasperPrint,
234: int pageIndex, boolean withPrintDialog) throws JRException {
235: return printPages(jasperPrint, pageIndex, pageIndex,
236: withPrintDialog);
237: }
238:
239: /**
240: *
241: */
242: public static boolean printPages(String sourceFileName,
243: int firstPageIndex, int lastPageIndex,
244: boolean withPrintDialog) throws JRException {
245: JasperPrint jasperPrint = (JasperPrint) JRLoader
246: .loadObject(sourceFileName);
247:
248: return printPages(jasperPrint, firstPageIndex, lastPageIndex,
249: withPrintDialog);
250: }
251:
252: /**
253: *
254: */
255: public static boolean printPages(InputStream inputStream,
256: int firstPageIndex, int lastPageIndex,
257: boolean withPrintDialog) throws JRException {
258: JasperPrint jasperPrint = (JasperPrint) JRLoader
259: .loadObject(inputStream);
260:
261: return printPages(jasperPrint, firstPageIndex, lastPageIndex,
262: withPrintDialog);
263: }
264:
265: /**
266: *
267: */
268: public static boolean printPages(JasperPrint jasperPrint,
269: int firstPageIndex, int lastPageIndex,
270: boolean withPrintDialog) throws JRException {
271: return JRPrinterAWT.printPages(jasperPrint, firstPageIndex,
272: lastPageIndex, withPrintDialog);
273: }
274:
275: /**
276: *
277: */
278: public static Image printPageToImage(String sourceFileName,
279: int pageIndex, float zoom) throws JRException {
280: JasperPrint jasperPrint = (JasperPrint) JRLoader
281: .loadObject(sourceFileName);
282:
283: return printPageToImage(jasperPrint, pageIndex, zoom);
284: }
285:
286: /**
287: *
288: */
289: public static Image printPageToImage(InputStream inputStream,
290: int pageIndex, float zoom) throws JRException {
291: JasperPrint jasperPrint = (JasperPrint) JRLoader
292: .loadObject(inputStream);
293:
294: return printPageToImage(jasperPrint, pageIndex, zoom);
295: }
296:
297: /**
298: *
299: */
300: public static Image printPageToImage(JasperPrint jasperPrint,
301: int pageIndex, float zoom) throws JRException {
302: return JRPrinterAWT.printPageToImage(jasperPrint, pageIndex,
303: zoom);
304: }
305:
306: // artf1936
307: private static boolean checkAvailablePrinters() {
308: if (isJre14orLater) {
309: try {
310: Class printServiceExporterClass = JRClassLoader
311: .loadClassForRealName("net.sf.jasperreports.engine.export.JRPrintServiceExporter");
312: Method method = printServiceExporterClass.getMethod(
313: "checkAvailablePrinters", (Class[]) null);
314: return ((Boolean) method.invoke(null, (Object[]) null))
315: .booleanValue();
316: } catch (Exception e) {
317: }
318: }
319: return true;
320: }
321:
322: }
|