1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id$ */
package org.apache.fop.render.pdf;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.util.Map;
import org.w3c.dom.NodeList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xmlgraphics.xmp.Metadata;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.fo.extensions.xmp.XMPMetadata;
import org.apache.fop.pdf.PDFAnnotList;
import org.apache.fop.pdf.PDFDocument;
import org.apache.fop.pdf.PDFPage;
import org.apache.fop.pdf.PDFResourceContext;
import org.apache.fop.pdf.PDFResources;
import org.apache.fop.render.extensions.prepress.PageBoundaries;
import org.apache.fop.render.extensions.prepress.PageScale;
import org.apache.fop.render.intermediate.AbstractBinaryWritingIFDocumentHandler;
import org.apache.fop.render.intermediate.IFContext;
import org.apache.fop.render.intermediate.IFDocumentHandler;
import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
import org.apache.fop.render.intermediate.IFDocumentNavigationHandler;
import org.apache.fop.render.intermediate.IFException;
import org.apache.fop.render.intermediate.IFPainter;
import org.apache.fop.render.pdf.extensions.PDFEmbeddedFileExtensionAttachment;
import org.apache.fop.util.XMLUtil;
/**
* {@link IFDocumentHandler} implementation that produces PDF.
*/
public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler {
/** logging instance */
private static Log log = LogFactory.getLog(PDFDocumentHandler.class);
private int pageSequenceIndex;
private boolean accessEnabled;
private PDFLogicalStructureHandler logicalStructureHandler;
/** the PDF Document being created */
protected PDFDocument pdfDoc;
/**
* Utility class which enables all sorts of features that are not directly connected to the
* normal rendering process.
*/
protected PDFRenderingUtil pdfUtil;
/** the /Resources object of the PDF document being created */
protected PDFResources pdfResources;
/** The current content generator */
protected PDFContentGenerator generator;
/** the current annotation list to add annotations to */
protected PDFResourceContext currentContext;
/** the current page to add annotations to */
protected PDFPage currentPage;
/** the current page's PDF reference */
protected PageReference currentPageRef;
/** Used for bookmarks/outlines. */
protected Map<Integer, PageReference> pageReferences =
new java.util.HashMap<Integer, PageReference>();
private final PDFDocumentNavigationHandler documentNavigationHandler
= new PDFDocumentNavigationHandler(this);
/**
* Default constructor.
*/
public PDFDocumentHandler() {
}
/** {@inheritDoc} */
public boolean supportsPagesOutOfOrder() {
return !accessEnabled;
}
/** {@inheritDoc} */
public String getMimeType() {
return MimeConstants.MIME_PDF;
}
/** {@inheritDoc} */
public void setContext(IFContext context) {
super.setContext(context);
this.pdfUtil = new PDFRenderingUtil(context.getUserAgent());
}
/** {@inheritDoc} */
public IFDocumentHandlerConfigurator getConfigurator() {
return new PDFRendererConfigurator(getUserAgent());
}
/** {@inheritDoc} */
public IFDocumentNavigationHandler getDocumentNavigationHandler() {
return this.documentNavigationHandler;
}
PDFRenderingUtil getPDFUtil() {
return this.pdfUtil;
}
PDFLogicalStructureHandler getLogicalStructureHandler() {
return logicalStructureHandler;
}
/** {@inheritDoc} */
public void startDocument() throws IFException {
super.startDocument();
try {
this.pdfDoc = pdfUtil.setupPDFDocument(this.outputStream);
this.accessEnabled = getUserAgent().isAccessibilityEnabled();
if (accessEnabled) {
pdfDoc.getRoot().makeTagged();
logicalStructureHandler = new PDFLogicalStructureHandler(pdfDoc,
getUserAgent().getEventBroadcaster());
}
} catch (IOException e) {
throw new IFException("I/O error in startDocument()", e);
}
}
/** {@inheritDoc} */
public void endDocumentHeader() throws IFException {
pdfUtil.generateDefaultXMPMetadata();
}
/** {@inheritDoc} */
public void endDocument() throws IFException {
try {
pdfDoc.getResources().addFonts(pdfDoc, fontInfo);
pdfDoc.outputTrailer(this.outputStream);
this.pdfDoc = null;
pdfResources = null;
this.generator = null;
currentContext = null;
currentPage = null;
} catch (IOException ioe) {
throw new IFException("I/O error in endDocument()", ioe);
}
super.endDocument();
}
/** {@inheritDoc} */
public void startPageSequence(String id) throws IFException {
//TODO page sequence title
if (this.pdfDoc.getRoot().getLanguage() == null
&& getContext().getLanguage() != null) {
//No document-level language set, so we use the first page-sequence's language
this.pdfDoc.getRoot().setLanguage(XMLUtil.toRFC3066(getContext().getLanguage()));
}
if (accessEnabled) {
NodeList nodes = getUserAgent().getStructureTree().getPageSequence(pageSequenceIndex++);
logicalStructureHandler.processStructureTree(nodes, getContext().getLanguage());
}
}
/** {@inheritDoc} */
public void endPageSequence() throws IFException {
//nop
}
/** {@inheritDoc} */
public void startPage(int index, String name, String pageMasterName, Dimension size)
throws IFException {
this.pdfResources = this.pdfDoc.getResources();
PageBoundaries boundaries = new PageBoundaries(size, getContext().getForeignAttributes());
Rectangle trimBox = boundaries.getTrimBox();
Rectangle bleedBox = boundaries.getBleedBox();
Rectangle mediaBox = boundaries.getMediaBox();
Rectangle cropBox = boundaries.getCropBox();
// set scale attributes
double scaleX = 1;
double scaleY = 1;
String scale = (String) getContext().getForeignAttribute(
PageScale.EXT_PAGE_SCALE);
Point2D scales = PageScale.getScale(scale);
if (scales != null) {
scaleX = scales.getX();
scaleY = scales.getY();
}
//PDF uses the lower left as origin, need to transform from FOP's internal coord system
AffineTransform boxTransform = new AffineTransform(
scaleX / 1000, 0, 0, -scaleY / 1000, 0, scaleY * size.getHeight() / 1000);
this.currentPage = this.pdfDoc.getFactory().makePage(
this.pdfResources,
index,
toPDFCoordSystem(mediaBox, boxTransform),
toPDFCoordSystem(cropBox, boxTransform),
toPDFCoordSystem(bleedBox, boxTransform),
toPDFCoordSystem(trimBox, boxTransform));
if (accessEnabled) {
logicalStructureHandler.startPage(currentPage);
}
pdfUtil.generatePageLabel(index, name);
currentPageRef = new PageReference(currentPage, size);
this.pageReferences.put(Integer.valueOf(index), currentPageRef);
this.generator = new PDFContentGenerator(this.pdfDoc, this.outputStream,
this.currentPage);
// Transform the PDF's default coordinate system (0,0 at lower left) to the PDFPainter's
AffineTransform basicPageTransform = new AffineTransform(1, 0, 0, -1, 0,
(scaleY * size.height) / 1000f);
basicPageTransform.scale(scaleX, scaleY);
generator.saveGraphicsState();
generator.concatenate(basicPageTransform);
}
private Rectangle2D toPDFCoordSystem(Rectangle box, AffineTransform transform) {
return transform.createTransformedShape(box).getBounds2D();
}
/** {@inheritDoc} */
public IFPainter startPageContent() throws IFException {
return new PDFPainter(this, logicalStructureHandler);
}
/** {@inheritDoc} */
public void endPageContent() throws IFException {
generator.restoreGraphicsState();
//for top-level transform to change the default coordinate system
}
/** {@inheritDoc} */
public void endPage() throws IFException {
if (accessEnabled) {
logicalStructureHandler.endPage();
}
try {
this.documentNavigationHandler.commit();
this.pdfDoc.registerObject(generator.getStream());
currentPage.setContents(generator.getStream());
PDFAnnotList annots = currentPage.getAnnotations();
if (annots != null) {
this.pdfDoc.addObject(annots);
}
this.pdfDoc.addObject(currentPage);
this.generator.flushPDFDoc();
this.generator = null;
} catch (IOException ioe) {
throw new IFException("I/O error in endPage()", ioe);
}
}
/** {@inheritDoc} */
public void handleExtensionObject(Object extension) throws IFException {
if (extension instanceof XMPMetadata) {
pdfUtil.renderXMPMetadata((XMPMetadata)extension);
} else if (extension instanceof Metadata) {
XMPMetadata wrapper = new XMPMetadata(((Metadata)extension));
pdfUtil.renderXMPMetadata(wrapper);
} else if (extension instanceof PDFEmbeddedFileExtensionAttachment) {
PDFEmbeddedFileExtensionAttachment embeddedFile
= (PDFEmbeddedFileExtensionAttachment)extension;
try {
pdfUtil.addEmbeddedFile(embeddedFile);
} catch (IOException ioe) {
throw new IFException("Error adding embedded file: " + embeddedFile.getSrc(), ioe);
}
} else {
log.debug("Don't know how to handle extension object. Ignoring: "
+ extension + " (" + extension.getClass().getName() + ")");
}
}
PageReference getPageReference(int pageIndex) {
return this.pageReferences.get(Integer.valueOf(pageIndex));
}
static final class PageReference {
private final String pageRef;
private final Dimension pageDimension;
private PageReference(PDFPage page, Dimension dim) {
// Avoid keeping references to PDFPage as memory usage is
// considerably increased when handling thousands of pages.
this.pageRef = page.makeReference().toString();
this.pageDimension = new Dimension(dim);
}
public String getPageRef() {
return this.pageRef;
}
public Dimension getPageDimension() {
return this.pageDimension;
}
}
}
|