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.

PDFDocumentHandler.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.pdf;
  19. import java.awt.Dimension;
  20. import java.awt.Rectangle;
  21. import java.awt.geom.AffineTransform;
  22. import java.awt.geom.Point2D;
  23. import java.awt.geom.Rectangle2D;
  24. import java.awt.geom.Rectangle2D.Double;
  25. import java.io.IOException;
  26. import java.util.Map;
  27. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29. import org.apache.xmlgraphics.xmp.Metadata;
  30. import org.apache.fop.apps.MimeConstants;
  31. import org.apache.fop.fo.extensions.xmp.XMPMetadata;
  32. import org.apache.fop.pdf.PDFAnnotList;
  33. import org.apache.fop.pdf.PDFDocument;
  34. import org.apache.fop.pdf.PDFPage;
  35. import org.apache.fop.pdf.PDFReference;
  36. import org.apache.fop.pdf.PDFResourceContext;
  37. import org.apache.fop.pdf.PDFResources;
  38. import org.apache.fop.render.extensions.prepress.PageBoundaries;
  39. import org.apache.fop.render.extensions.prepress.PageScale;
  40. import org.apache.fop.render.intermediate.AbstractBinaryWritingIFDocumentHandler;
  41. import org.apache.fop.render.intermediate.IFContext;
  42. import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
  43. import org.apache.fop.render.intermediate.IFDocumentNavigationHandler;
  44. import org.apache.fop.render.intermediate.IFException;
  45. import org.apache.fop.render.intermediate.IFPainter;
  46. /**
  47. * {@code IFDocumentHandler} implementation that produces PDF.
  48. */
  49. public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler {
  50. /** logging instance */
  51. private static Log log = LogFactory.getLog(PDFDocumentHandler.class);
  52. /** the PDF Document being created */
  53. protected PDFDocument pdfDoc;
  54. /**
  55. * Utility class which enables all sorts of features that are not directly connected to the
  56. * normal rendering process.
  57. */
  58. protected PDFRenderingUtil pdfUtil;
  59. /** the /Resources object of the PDF document being created */
  60. protected PDFResources pdfResources;
  61. /** The current content generator */
  62. protected PDFContentGenerator generator;
  63. /** the current annotation list to add annotations to */
  64. protected PDFResourceContext currentContext;
  65. /** the current page to add annotations to */
  66. protected PDFPage currentPage;
  67. /** the current page's PDF reference */
  68. protected PageReference currentPageRef;
  69. /** Used for bookmarks/outlines. */
  70. protected Map pageReferences = new java.util.HashMap();
  71. private PDFDocumentNavigationHandler documentNavigationHandler
  72. = new PDFDocumentNavigationHandler(this);
  73. /**
  74. * Default constructor.
  75. */
  76. public PDFDocumentHandler() {
  77. }
  78. /** {@inheritDoc} */
  79. public boolean supportsPagesOutOfOrder() {
  80. return true;
  81. }
  82. /** {@inheritDoc} */
  83. public String getMimeType() {
  84. return MimeConstants.MIME_PDF;
  85. }
  86. /** {@inheritDoc} */
  87. public void setContext(IFContext context) {
  88. super.setContext(context);
  89. this.pdfUtil = new PDFRenderingUtil(context.getUserAgent());
  90. }
  91. /** {@inheritDoc} */
  92. public IFDocumentHandlerConfigurator getConfigurator() {
  93. return new PDFRendererConfigurator(getUserAgent());
  94. }
  95. /** {@inheritDoc} */
  96. public IFDocumentNavigationHandler getDocumentNavigationHandler() {
  97. return this.documentNavigationHandler;
  98. }
  99. PDFRenderingUtil getPDFUtil() {
  100. return this.pdfUtil;
  101. }
  102. /** {@inheritDoc} */
  103. public void startDocument() throws IFException {
  104. super.startDocument();
  105. try {
  106. this.pdfDoc = pdfUtil.setupPDFDocument(this.outputStream);
  107. } catch (IOException e) {
  108. throw new IFException("I/O error in startDocument()", e);
  109. }
  110. }
  111. /** {@inheritDoc} */
  112. public void endDocumentHeader() throws IFException {
  113. pdfUtil.generateDefaultXMPMetadata();
  114. }
  115. /** {@inheritDoc} */
  116. public void endDocument() throws IFException {
  117. try {
  118. pdfDoc.getResources().addFonts(pdfDoc, fontInfo);
  119. pdfDoc.outputTrailer(this.outputStream);
  120. this.pdfDoc = null;
  121. pdfResources = null;
  122. this.generator = null;
  123. currentContext = null;
  124. currentPage = null;
  125. } catch (IOException ioe) {
  126. throw new IFException("I/O error in endDocument()", ioe);
  127. }
  128. super.endDocument();
  129. }
  130. /** {@inheritDoc} */
  131. public void startPageSequence(String id) throws IFException {
  132. //TODO page sequence title, country and language
  133. }
  134. /** {@inheritDoc} */
  135. public void endPageSequence() throws IFException {
  136. //nop
  137. }
  138. /** {@inheritDoc} */
  139. public void startPage(int index, String name, String pageMasterName, Dimension size)
  140. throws IFException {
  141. this.pdfResources = this.pdfDoc.getResources();
  142. PageBoundaries boundaries = new PageBoundaries(size, getContext().getForeignAttributes());
  143. Rectangle trimBox = boundaries.getTrimBox();
  144. Rectangle bleedBox = boundaries.getBleedBox();
  145. Rectangle mediaBox = boundaries.getMediaBox();
  146. Rectangle cropBox = boundaries.getCropBox();
  147. // set scale attributes
  148. double scaleX = 1;
  149. double scaleY = 1;
  150. String scale = (String) getContext().getForeignAttribute(
  151. PageScale.EXT_PAGE_SCALE);
  152. Point2D scales = PageScale.getScale(scale);
  153. if (scales != null) {
  154. scaleX = scales.getX();
  155. scaleY = scales.getY();
  156. }
  157. this.currentPage = this.pdfDoc.getFactory().makePage(
  158. this.pdfResources,
  159. index,
  160. toPointAndScale(mediaBox, scaleX, scaleY),
  161. toPointAndScale(cropBox, scaleX, scaleY),
  162. toPointAndScale(bleedBox, scaleX, scaleY),
  163. toPointAndScale(trimBox, scaleX, scaleY));
  164. pdfUtil.generatePageLabel(index, name);
  165. currentPageRef = new PageReference(currentPage, size);
  166. this.pageReferences.put(new Integer(index), currentPageRef);
  167. this.generator = new PDFContentGenerator(this.pdfDoc, this.outputStream, this.currentPage);
  168. // Transform the PDF's default coordinate system (0,0 at lower left) to the PDFPainter's
  169. AffineTransform basicPageTransform = new AffineTransform(1, 0, 0, -1, 0,
  170. (scaleY * size.height) / 1000f);
  171. basicPageTransform.scale(scaleX, scaleY);
  172. generator.concatenate(basicPageTransform);
  173. }
  174. private Double toPointAndScale(Rectangle box, double scaleX, double scaleY) {
  175. return new Rectangle2D.Double(box.getX() * scaleX / 1000,
  176. box.getY() * scaleY / 1000,
  177. box.getWidth() * scaleX / 1000,
  178. box.getHeight() * scaleY / 1000);
  179. }
  180. /** {@inheritDoc} */
  181. public IFPainter startPageContent() throws IFException {
  182. return new PDFPainter(this);
  183. }
  184. /** {@inheritDoc} */
  185. public void endPageContent() throws IFException {
  186. //nop
  187. }
  188. /** {@inheritDoc} */
  189. public void endPage() throws IFException {
  190. try {
  191. this.documentNavigationHandler.commit();
  192. this.pdfDoc.registerObject(generator.getStream());
  193. currentPage.setContents(generator.getStream());
  194. PDFAnnotList annots = currentPage.getAnnotations();
  195. if (annots != null) {
  196. this.pdfDoc.addObject(annots);
  197. }
  198. this.pdfDoc.addObject(currentPage);
  199. this.generator.flushPDFDoc();
  200. this.generator = null;
  201. } catch (IOException ioe) {
  202. throw new IFException("I/O error in endPage()", ioe);
  203. }
  204. }
  205. /** {@inheritDoc} */
  206. public void handleExtensionObject(Object extension) throws IFException {
  207. if (extension instanceof XMPMetadata) {
  208. pdfUtil.renderXMPMetadata((XMPMetadata)extension);
  209. } else if (extension instanceof Metadata) {
  210. XMPMetadata wrapper = new XMPMetadata(((Metadata)extension));
  211. pdfUtil.renderXMPMetadata(wrapper);
  212. } else {
  213. log.debug("Don't know how to handle extension object. Ignoring: "
  214. + extension + " (" + extension.getClass().getName() + ")");
  215. }
  216. }
  217. PageReference getPageReference(int pageIndex) {
  218. return (PageReference)this.pageReferences.get(
  219. new Integer(pageIndex));
  220. }
  221. static final class PageReference {
  222. private PDFReference pageRef;
  223. private Dimension pageDimension;
  224. private PageReference(PDFPage page, Dimension dim) {
  225. this.pageRef = page.makeReference();
  226. this.pageDimension = new Dimension(dim);
  227. }
  228. public PDFReference getPageRef() {
  229. return this.pageRef;
  230. }
  231. public Dimension getPageDimension() {
  232. return this.pageDimension;
  233. }
  234. }
  235. }