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.

PDFRendererConfig.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.ArrayList;
  20. import java.util.EnumMap;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.fop.apps.FOPException;
  27. import org.apache.fop.apps.FOUserAgent;
  28. import org.apache.fop.apps.MimeConstants;
  29. import org.apache.fop.configuration.Configuration;
  30. import org.apache.fop.configuration.ConfigurationException;
  31. import org.apache.fop.fonts.DefaultFontConfig;
  32. import org.apache.fop.fonts.DefaultFontConfig.DefaultFontConfigParser;
  33. import org.apache.fop.fonts.FontEventAdapter;
  34. import org.apache.fop.pdf.PDFEncryptionParams;
  35. import org.apache.fop.pdf.PDFFilterList;
  36. import org.apache.fop.render.RendererConfig;
  37. import org.apache.fop.render.RendererConfigOption;
  38. import org.apache.fop.util.LogUtil;
  39. import static org.apache.fop.render.pdf.PDFEncryptionOption.ENCRYPTION_LENGTH;
  40. import static org.apache.fop.render.pdf.PDFEncryptionOption.ENCRYPTION_PARAMS;
  41. import static org.apache.fop.render.pdf.PDFEncryptionOption.ENCRYPT_METADATA;
  42. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_ACCESSCONTENT;
  43. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_ANNOTATIONS;
  44. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_ASSEMBLEDOC;
  45. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_COPY_CONTENT;
  46. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_EDIT_CONTENT;
  47. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_FILLINFORMS;
  48. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_PRINT;
  49. import static org.apache.fop.render.pdf.PDFEncryptionOption.NO_PRINTHQ;
  50. import static org.apache.fop.render.pdf.PDFEncryptionOption.OWNER_PASSWORD;
  51. import static org.apache.fop.render.pdf.PDFEncryptionOption.USER_PASSWORD;
  52. import static org.apache.fop.render.pdf.PDFRendererOption.DISABLE_SRGB_COLORSPACE;
  53. import static org.apache.fop.render.pdf.PDFRendererOption.FILTER_LIST;
  54. import static org.apache.fop.render.pdf.PDFRendererOption.LINEARIZATION;
  55. import static org.apache.fop.render.pdf.PDFRendererOption.MERGE_FONTS;
  56. import static org.apache.fop.render.pdf.PDFRendererOption.OUTPUT_PROFILE;
  57. import static org.apache.fop.render.pdf.PDFRendererOption.PDF_A_MODE;
  58. import static org.apache.fop.render.pdf.PDFRendererOption.PDF_UA_MODE;
  59. import static org.apache.fop.render.pdf.PDFRendererOption.PDF_VT_MODE;
  60. import static org.apache.fop.render.pdf.PDFRendererOption.PDF_X_MODE;
  61. import static org.apache.fop.render.pdf.PDFRendererOption.VERSION;
  62. /**
  63. * The PDF renderer configuration data object.
  64. */
  65. public final class PDFRendererConfig implements RendererConfig {
  66. private static final Log LOG = LogFactory.getLog(PDFRendererConfig.class);
  67. private final PDFRendererOptionsConfig configOption;
  68. private final DefaultFontConfig fontConfig;
  69. private PDFRendererConfig(DefaultFontConfig fontConfig, PDFRendererOptionsConfig config) {
  70. this.fontConfig = fontConfig;
  71. this.configOption = config;
  72. }
  73. public PDFRendererOptionsConfig getConfigOptions() {
  74. return configOption;
  75. }
  76. public DefaultFontConfig getFontInfoConfig() {
  77. return fontConfig;
  78. }
  79. /**
  80. * The PDF renderer configuration data parser.
  81. */
  82. public static final class PDFRendererConfigParser implements RendererConfigParser {
  83. public PDFRendererConfig build(FOUserAgent userAgent, Configuration cfg) throws FOPException {
  84. boolean strict = userAgent != null ? userAgent.validateUserConfigStrictly() : false;
  85. return new ParserHelper(cfg, userAgent, strict).pdfConfig;
  86. }
  87. public String getMimeType() {
  88. return MimeConstants.MIME_PDF;
  89. }
  90. }
  91. private static final class ParserHelper {
  92. private final Map<PDFRendererOption, Object> configOptions
  93. = new EnumMap<PDFRendererOption, Object>(PDFRendererOption.class);
  94. private PDFEncryptionParams encryptionConfig;
  95. private PDFRendererConfig pdfConfig;
  96. private ParserHelper(Configuration cfg, FOUserAgent userAgent, boolean strict) throws FOPException {
  97. if (cfg != null) {
  98. configure(cfg, userAgent, strict);
  99. }
  100. if (userAgent == null) {
  101. pdfConfig = new PDFRendererConfig(new DefaultFontConfigParser().parse(cfg, strict),
  102. new PDFRendererOptionsConfig(configOptions, encryptionConfig));
  103. } else {
  104. pdfConfig = new PDFRendererConfig(new DefaultFontConfigParser().parse(cfg, strict,
  105. new FontEventAdapter(userAgent.getEventBroadcaster())),
  106. new PDFRendererOptionsConfig(configOptions, encryptionConfig));
  107. }
  108. }
  109. private void parseAndPut(PDFRendererOption option, Configuration cfg) {
  110. put(option, option.parse(parseConfig(cfg, option)));
  111. }
  112. private void put(PDFRendererOption option, Object value) {
  113. if (value != null && !value.equals(option.getDefaultValue())) {
  114. configOptions.put(option, value);
  115. }
  116. }
  117. private void configure(Configuration cfg, FOUserAgent userAgent, boolean strict) throws FOPException {
  118. try {
  119. buildFilterMapFromConfiguration(cfg);
  120. parseAndPut(PDF_A_MODE, cfg);
  121. parseAndPut(PDF_UA_MODE, cfg);
  122. parseAndPut(PDF_X_MODE, cfg);
  123. parseAndPut(PDF_VT_MODE, cfg);
  124. configureEncryptionParams(cfg, userAgent, strict);
  125. parseAndPut(OUTPUT_PROFILE, cfg);
  126. parseAndPut(DISABLE_SRGB_COLORSPACE, cfg);
  127. parseAndPut(MERGE_FONTS, cfg);
  128. parseAndPut(LINEARIZATION, cfg);
  129. parseAndPut(VERSION, cfg);
  130. } catch (ConfigurationException e) {
  131. LogUtil.handleException(LOG, e, strict);
  132. }
  133. }
  134. private void configureEncryptionParams(Configuration cfg, FOUserAgent userAgent, boolean strict) {
  135. Configuration encryptCfg = cfg.getChild(ENCRYPTION_PARAMS, false);
  136. if (encryptCfg != null) {
  137. encryptionConfig = new PDFEncryptionParams();
  138. encryptionConfig.setOwnerPassword(parseConfig(encryptCfg, OWNER_PASSWORD));
  139. encryptionConfig.setUserPassword(parseConfig(encryptCfg, USER_PASSWORD));
  140. encryptionConfig.setAllowPrint(!doesValueExist(encryptCfg, NO_PRINT));
  141. encryptionConfig.setAllowCopyContent(!doesValueExist(encryptCfg, NO_COPY_CONTENT));
  142. encryptionConfig.setAllowEditContent(!doesValueExist(encryptCfg, NO_EDIT_CONTENT));
  143. encryptionConfig.setAllowEditAnnotations(!doesValueExist(encryptCfg, NO_ANNOTATIONS));
  144. encryptionConfig.setAllowFillInForms(!doesValueExist(encryptCfg, NO_FILLINFORMS));
  145. encryptionConfig.setAllowAccessContent(!doesValueExist(encryptCfg, NO_ACCESSCONTENT));
  146. encryptionConfig.setAllowAssembleDocument(!doesValueExist(encryptCfg, NO_ASSEMBLEDOC));
  147. encryptionConfig.setAllowPrintHq(!doesValueExist(encryptCfg, NO_PRINTHQ));
  148. encryptionConfig.setEncryptMetadata(getConfigValue(encryptCfg, ENCRYPT_METADATA, true));
  149. String encryptionLength = parseConfig(encryptCfg, ENCRYPTION_LENGTH);
  150. if (encryptionLength != null) {
  151. int validatedLength = checkEncryptionLength(Integer.parseInt(encryptionLength), userAgent);
  152. encryptionConfig.setEncryptionLengthInBits(validatedLength);
  153. }
  154. }
  155. }
  156. private void buildFilterMapFromConfiguration(Configuration cfg) throws ConfigurationException,
  157. FOPException {
  158. Configuration[] filterLists = cfg.getChildren(FILTER_LIST.getName());
  159. Map<String, List<String>> filterMap = new HashMap<String, List<String>>();
  160. for (Configuration filters : filterLists) {
  161. String type = filters.getAttribute("type", PDFFilterList.DEFAULT_FILTER);
  162. List<String> filterList = new ArrayList<String>();
  163. for (Configuration nameCfg : filters.getChildren("value")) {
  164. filterList.add(nameCfg.getValue());
  165. }
  166. if (!filterList.isEmpty() && LOG.isDebugEnabled()) {
  167. StringBuffer debug = new StringBuffer("Adding PDF filter");
  168. if (filterList.size() != 1) {
  169. debug.append("s");
  170. }
  171. debug.append(" for type ").append(type).append(": ");
  172. for (int j = 0; j < filterList.size(); j++) {
  173. if (j != 0) {
  174. debug.append(", ");
  175. }
  176. debug.append(filterList.get(j));
  177. }
  178. LOG.debug(debug.toString());
  179. }
  180. if (filterMap.get(type) != null) {
  181. throw new ConfigurationException("A filterList of type '"
  182. + type + "' has already been defined");
  183. }
  184. filterMap.put(type, filterList);
  185. }
  186. put(FILTER_LIST, filterMap);
  187. }
  188. private String parseConfig(Configuration cfg, RendererConfigOption option) {
  189. Configuration child = cfg.getChild(option.getName());
  190. String value = child.getValue(null);
  191. if (value == null || "".equals(value)) {
  192. Object v = option.getDefaultValue();
  193. return v == null ? null : v.toString();
  194. }
  195. return value;
  196. }
  197. private boolean doesValueExist(Configuration cfg, RendererConfigOption option) {
  198. return cfg.getChild(option.getName(), false) != null;
  199. }
  200. private boolean getConfigValue(Configuration cfg, RendererConfigOption option, boolean defaultTo) {
  201. if (cfg.getChild(option.getName(), false) != null) {
  202. Configuration child = cfg.getChild(option.getName());
  203. try {
  204. return child.getValueAsBoolean();
  205. } catch (ConfigurationException e) {
  206. return defaultTo;
  207. }
  208. } else {
  209. return defaultTo;
  210. }
  211. }
  212. private int checkEncryptionLength(int encryptionLength, FOUserAgent userAgent) {
  213. int correctEncryptionLength = encryptionLength;
  214. if (encryptionLength < 40) {
  215. correctEncryptionLength = 40;
  216. } else if (encryptionLength > 256) {
  217. correctEncryptionLength = 256;
  218. } else if (encryptionLength > 128 && encryptionLength < 256) {
  219. correctEncryptionLength = 128;
  220. } else if (encryptionLength % 8 != 0) {
  221. correctEncryptionLength = Math.round(encryptionLength / 8.0f) * 8;
  222. }
  223. if (correctEncryptionLength != encryptionLength && userAgent != null) {
  224. PDFEventProducer.Provider.get(userAgent.getEventBroadcaster())
  225. .incorrectEncryptionLength(this, encryptionLength,
  226. correctEncryptionLength);
  227. }
  228. return correctEncryptionLength;
  229. }
  230. }
  231. }