001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either 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, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * PdfOutputProcessor.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.output.pageable.pdf;
030:
031: import java.io.OutputStream;
032:
033: import org.jfree.fonts.encoding.EncodingRegistry;
034: import org.jfree.fonts.itext.ITextFontStorage;
035: import org.jfree.report.layout.model.LogicalPageBox;
036: import org.jfree.report.layout.model.PageGrid;
037: import org.jfree.report.layout.output.ContentProcessingException;
038: import org.jfree.report.layout.output.LogicalPageKey;
039: import org.jfree.report.layout.output.OutputProcessorMetaData;
040: import org.jfree.report.layout.output.PhysicalPageKey;
041: import org.jfree.report.modules.output.pageable.base.AbstractPageableOutputProcessor;
042: import org.jfree.report.modules.output.pageable.base.AllPageFlowSelector;
043: import org.jfree.report.modules.output.pageable.base.PageFlowSelector;
044: import org.jfree.report.modules.output.pageable.pdf.internal.PdfDocumentWriter;
045: import org.jfree.report.modules.output.pageable.pdf.internal.PdfOutputProcessorMetaData;
046: import org.jfree.report.modules.output.support.itext.BaseFontModule;
047: import org.jfree.util.Configuration;
048:
049: /**
050: * A streaming target, which produces a PDF document.
051: *
052: * @author Thomas Morgner
053: */
054: public class PdfOutputProcessor extends AbstractPageableOutputProcessor {
055: private PdfOutputProcessorMetaData metaData;
056: private PageFlowSelector flowSelector;
057: private OutputStream outputStream;
058: private PdfDocumentWriter writer;
059:
060: public PdfOutputProcessor(final Configuration configuration,
061: final OutputStream outputStream) {
062: if (configuration == null) {
063: throw new NullPointerException(
064: "Configuration must not be null");
065: }
066: if (outputStream == null) {
067: throw new NullPointerException(
068: "OutputStream must not be null");
069: }
070:
071: this .outputStream = outputStream;
072: this .flowSelector = new AllPageFlowSelector();
073:
074: // for the sake of simplicity, we use the AWT font registry for now.
075: // This is less accurate than using the iText fonts, but completing
076: // the TrueType registry or implementing an iText registry is too expensive
077: // for now.
078: final String encoding = configuration
079: .getConfigProperty(
080: "org.jfree.report.modules.output.pageable.pdf.Encoding",
081: EncodingRegistry.getPlatformDefaultEncoding());
082: final ITextFontStorage fontStorage = new ITextFontStorage(
083: BaseFontModule.getFontRegistry(), encoding);
084:
085: metaData = new PdfOutputProcessorMetaData(configuration,
086: fontStorage);
087:
088: }
089:
090: public OutputProcessorMetaData getMetaData() {
091: return metaData;
092: }
093:
094: public PageFlowSelector getFlowSelector() {
095: return flowSelector;
096: }
097:
098: public void setFlowSelector(final PageFlowSelector flowSelector) {
099: if (flowSelector == null) {
100: throw new NullPointerException();
101: }
102:
103: this .flowSelector = flowSelector;
104: }
105:
106: protected void processingContentFinished() {
107: if (writer != null) {
108: writer.close();
109: }
110: }
111:
112: protected void processPhysicalPage(final PageGrid pageGrid,
113: final LogicalPageBox logicalPage, final int row,
114: final int col, final PhysicalPageKey pageKey)
115: throws ContentProcessingException {
116: try {
117: if (writer == null) {
118: writer = new PdfDocumentWriter(metaData, outputStream);
119: writer.open();
120: }
121: writer.processPhysicalPage(pageGrid, logicalPage, row, col,
122: pageKey);
123: } catch (Exception e) {
124: throw new ContentProcessingException(
125: "Failed to generate PDF document", e);
126: }
127: }
128:
129: protected void processLogicalPage(final LogicalPageKey key,
130: final LogicalPageBox logicalPage)
131: throws ContentProcessingException {
132: try {
133: if (writer == null) {
134: writer = new PdfDocumentWriter(metaData, outputStream);
135: writer.open();
136: }
137: writer.processLogicalPage(key, logicalPage);
138: } catch (Exception e) {
139: throw new ContentProcessingException(
140: "Failed to generate PDF document", e);
141: }
142: }
143:
144: }
|