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.

PDFRendererConfigurator.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.util.List;
  20. import java.util.Map;
  21. import org.apache.avalon.framework.configuration.Configuration;
  22. import org.apache.avalon.framework.configuration.ConfigurationException;
  23. import org.apache.fop.apps.FOPException;
  24. import org.apache.fop.apps.FOUserAgent;
  25. import org.apache.fop.pdf.PDFAMode;
  26. import org.apache.fop.pdf.PDFEncryptionParams;
  27. import org.apache.fop.pdf.PDFFilterList;
  28. import org.apache.fop.pdf.PDFXMode;
  29. import org.apache.fop.render.PrintRendererConfigurator;
  30. import org.apache.fop.render.Renderer;
  31. import org.apache.fop.render.intermediate.IFDocumentHandler;
  32. import org.apache.fop.util.LogUtil;
  33. /**
  34. * PDF renderer configurator.
  35. */
  36. public class PDFRendererConfigurator extends PrintRendererConfigurator {
  37. /**
  38. * Default constructor
  39. *
  40. * @param userAgent user agent
  41. */
  42. public PDFRendererConfigurator(FOUserAgent userAgent) {
  43. super(userAgent);
  44. }
  45. /**
  46. * Throws an UnsupportedOperationException.
  47. *
  48. * @param renderer not used
  49. */
  50. public void configure(Renderer renderer) {
  51. throw new UnsupportedOperationException();
  52. }
  53. private void configure(Configuration cfg, PDFRenderingUtil pdfUtil) throws FOPException {
  54. //PDF filters
  55. try {
  56. Map filterMap = buildFilterMapFromConfiguration(cfg);
  57. if (filterMap != null) {
  58. pdfUtil.setFilterMap(filterMap);
  59. }
  60. } catch (ConfigurationException e) {
  61. LogUtil.handleException(log, e, false);
  62. }
  63. String s = cfg.getChild(PDFConfigurationConstants.PDF_A_MODE, true).getValue(null);
  64. if (s != null) {
  65. pdfUtil.setAMode(PDFAMode.valueOf(s));
  66. }
  67. s = cfg.getChild(PDFConfigurationConstants.PDF_X_MODE, true).getValue(null);
  68. if (s != null) {
  69. pdfUtil.setXMode(PDFXMode.valueOf(s));
  70. }
  71. Configuration encryptionParamsConfig
  72. = cfg.getChild(PDFConfigurationConstants.ENCRYPTION_PARAMS, false);
  73. if (encryptionParamsConfig != null) {
  74. PDFEncryptionParams encryptionParams = pdfUtil.getEncryptionParams();
  75. Configuration ownerPasswordConfig = encryptionParamsConfig.getChild(
  76. PDFConfigurationConstants.OWNER_PASSWORD, false);
  77. if (ownerPasswordConfig != null) {
  78. String ownerPassword = ownerPasswordConfig.getValue(null);
  79. if (ownerPassword != null) {
  80. encryptionParams.setOwnerPassword(ownerPassword);
  81. }
  82. }
  83. Configuration userPasswordConfig = encryptionParamsConfig.getChild(
  84. PDFConfigurationConstants.USER_PASSWORD, false);
  85. if (userPasswordConfig != null) {
  86. String userPassword = userPasswordConfig.getValue(null);
  87. if (userPassword != null) {
  88. encryptionParams.setUserPassword(userPassword);
  89. }
  90. }
  91. Configuration noPrintConfig = encryptionParamsConfig.getChild(
  92. PDFConfigurationConstants.NO_PRINT, false);
  93. if (noPrintConfig != null) {
  94. encryptionParams.setAllowPrint(false);
  95. }
  96. Configuration noCopyContentConfig = encryptionParamsConfig.getChild(
  97. PDFConfigurationConstants.NO_COPY_CONTENT, false);
  98. if (noCopyContentConfig != null) {
  99. encryptionParams.setAllowCopyContent(false);
  100. }
  101. Configuration noEditContentConfig = encryptionParamsConfig.getChild(
  102. PDFConfigurationConstants.NO_EDIT_CONTENT, false);
  103. if (noEditContentConfig != null) {
  104. encryptionParams.setAllowEditContent(false);
  105. }
  106. Configuration noAnnotationsConfig = encryptionParamsConfig.getChild(
  107. PDFConfigurationConstants.NO_ANNOTATIONS, false);
  108. if (noAnnotationsConfig != null) {
  109. encryptionParams.setAllowEditAnnotations(false);
  110. }
  111. Configuration noFillInForms = encryptionParamsConfig.getChild(
  112. PDFConfigurationConstants.NO_FILLINFORMS, false);
  113. if (noFillInForms != null) {
  114. encryptionParams.setAllowFillInForms(false);
  115. }
  116. Configuration noAccessContentConfig = encryptionParamsConfig.getChild(
  117. PDFConfigurationConstants.NO_ACCESSCONTENT, false);
  118. if (noAccessContentConfig != null) {
  119. encryptionParams.setAllowAccessContent(false);
  120. }
  121. Configuration noAssembleDocConfig = encryptionParamsConfig.getChild(
  122. PDFConfigurationConstants.NO_ASSEMBLEDOC, false);
  123. if (noAssembleDocConfig != null) {
  124. encryptionParams.setAllowAssembleDocument(false);
  125. }
  126. Configuration noPrintHqConfig = encryptionParamsConfig.getChild(
  127. PDFConfigurationConstants.NO_PRINTHQ, false);
  128. if (noPrintHqConfig != null) {
  129. encryptionParams.setAllowPrintHq(false);
  130. }
  131. Configuration encryptionLengthConfig = encryptionParamsConfig.getChild(
  132. PDFConfigurationConstants.ENCRYPTION_LENGTH, false);
  133. if (encryptionLengthConfig != null) {
  134. int encryptionLength = checkEncryptionLength(
  135. Integer.parseInt(encryptionLengthConfig.getValue(null)));
  136. encryptionParams.setEncryptionLengthInBits(encryptionLength);
  137. }
  138. }
  139. s = cfg.getChild(PDFConfigurationConstants.KEY_OUTPUT_PROFILE, true).getValue(null);
  140. if (s != null) {
  141. pdfUtil.setOutputProfileURI(s);
  142. }
  143. Configuration disableColorSpaceConfig = cfg.getChild(
  144. PDFConfigurationConstants.KEY_DISABLE_SRGB_COLORSPACE, false);
  145. if (disableColorSpaceConfig != null) {
  146. pdfUtil.setDisableSRGBColorSpace(
  147. disableColorSpaceConfig.getValueAsBoolean(false));
  148. }
  149. }
  150. private int checkEncryptionLength(int encryptionLength) {
  151. int correctEncryptionLength = encryptionLength;
  152. if (encryptionLength < 40) {
  153. correctEncryptionLength = 40;
  154. } else if (encryptionLength > 128) {
  155. correctEncryptionLength = 128;
  156. } else if (encryptionLength % 8 != 0) {
  157. correctEncryptionLength = ((int) Math.round(encryptionLength / 8.0f)) * 8;
  158. }
  159. if (correctEncryptionLength != encryptionLength) {
  160. PDFEventProducer.Provider.get(userAgent.getEventBroadcaster())
  161. .incorrectEncryptionLength(this, encryptionLength, correctEncryptionLength);
  162. }
  163. return correctEncryptionLength;
  164. }
  165. /**
  166. * Builds a filter map from an Avalon Configuration object.
  167. *
  168. * @param cfg the Configuration object
  169. * @return Map the newly built filter map
  170. * @throws ConfigurationException if a filter list is defined twice
  171. */
  172. public static Map buildFilterMapFromConfiguration(Configuration cfg)
  173. throws ConfigurationException {
  174. Map filterMap = new java.util.HashMap();
  175. Configuration[] filterLists = cfg.getChildren("filterList");
  176. for (int i = 0; i < filterLists.length; i++) {
  177. Configuration filters = filterLists[i];
  178. String type = filters.getAttribute("type", null);
  179. Configuration[] filt = filters.getChildren("value");
  180. List filterList = new java.util.ArrayList();
  181. for (int j = 0; j < filt.length; j++) {
  182. String name = filt[j].getValue();
  183. filterList.add(name);
  184. }
  185. if (type == null) {
  186. type = PDFFilterList.DEFAULT_FILTER;
  187. }
  188. if (!filterList.isEmpty() && log.isDebugEnabled()) {
  189. StringBuffer debug = new StringBuffer("Adding PDF filter");
  190. if (filterList.size() != 1) {
  191. debug.append("s");
  192. }
  193. debug.append(" for type ").append(type).append(": ");
  194. for (int j = 0; j < filterList.size(); j++) {
  195. if (j != 0) {
  196. debug.append(", ");
  197. }
  198. debug.append(filterList.get(j));
  199. }
  200. log.debug(debug.toString());
  201. }
  202. if (filterMap.get(type) != null) {
  203. throw new ConfigurationException("A filterList of type '"
  204. + type + "' has already been defined");
  205. }
  206. filterMap.put(type, filterList);
  207. }
  208. return filterMap;
  209. }
  210. // ---=== IFDocumentHandler configuration ===---
  211. /** {@inheritDoc} */
  212. public void configure(IFDocumentHandler documentHandler) throws FOPException {
  213. Configuration cfg = super.getRendererConfig(documentHandler.getMimeType());
  214. if (cfg != null) {
  215. PDFDocumentHandler pdfDocumentHandler = (PDFDocumentHandler)documentHandler;
  216. PDFRenderingUtil pdfUtil = pdfDocumentHandler.getPDFUtil();
  217. configure(cfg, pdfUtil);
  218. }
  219. }
  220. }