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

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