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.

AbstractPSPDFBitmapProducer.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.visual;
  19. import java.awt.image.BufferedImage;
  20. import java.io.BufferedOutputStream;
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.OutputStream;
  25. import java.text.MessageFormat;
  26. import javax.xml.transform.Transformer;
  27. import javax.xml.transform.sax.SAXResult;
  28. import javax.xml.transform.stream.StreamSource;
  29. import org.apache.avalon.framework.configuration.Configurable;
  30. import org.apache.avalon.framework.configuration.Configuration;
  31. import org.apache.avalon.framework.configuration.ConfigurationException;
  32. import org.apache.commons.io.IOUtils;
  33. import org.apache.fop.apps.FOUserAgent;
  34. import org.apache.fop.apps.Fop;
  35. import org.apache.fop.apps.FopFactory;
  36. /**
  37. * BitmapProducer implementation that uses the PS or PDF renderer and an external converter
  38. * to create bitmaps.
  39. * <p>
  40. * Here's what the configuration element looks like for the class:
  41. * <p>
  42. * <pre>
  43. * <producer classname="<concrete class>">
  44. * <converter>mypdf2bmp {0} {1} {2}</converter>
  45. * <delete-temp-files>false</delete-temp-files>
  46. * </producer>
  47. * </pre>
  48. * <p>
  49. * You will notice the three parameters in curly braces (java.util.MessageFormat style) to the
  50. * converter call:
  51. * <ul>
  52. * <li>0: the input file
  53. * <li>1: the output bitmap file
  54. * <li>2: the requested resolution in dpi for the generated bitmap
  55. * </ul>
  56. * <p>
  57. * The "delete-temp-files" element is optional and defaults to true.
  58. */
  59. public abstract class AbstractPSPDFBitmapProducer extends AbstractBitmapProducer
  60. implements Configurable {
  61. // configure fopFactory as desired
  62. private FopFactory fopFactory = FopFactory.newInstance();
  63. private String converter;
  64. private boolean deleteTempFiles;
  65. /** the bitmap producer's target format */
  66. protected String targetFormat;
  67. /** {@inheritDoc} */
  68. public void configure(Configuration cfg) throws ConfigurationException {
  69. this.converter = cfg.getChild("converter").getValue();
  70. this.deleteTempFiles = cfg.getChild("delete-temp-files").getValueAsBoolean(true);
  71. if (cfg.getChild("target-format", false) != null) {
  72. this.targetFormat = cfg.getChild("target-format").getValue();
  73. }
  74. }
  75. /**
  76. * Calls an external converter to convert the file generated by FOP to a bitmap.
  77. * @param inFile the generated output file to be converted
  78. * @param outFile the target filename for the bitmap
  79. * @param context context information (required bitmap resolution etc.)
  80. * @throws IOException in case the conversion fails
  81. */
  82. public void convert(File inFile, File outFile, ProducerContext context) throws IOException {
  83. outFile.delete();
  84. //Build command-line
  85. String cmd = MessageFormat.format(converter,
  86. new Object[] {inFile.toString(), outFile.toString(),
  87. Integer.toString(context.getTargetResolution())});
  88. ConvertUtils.convert(cmd, null, null, log);
  89. if (!outFile.exists()) {
  90. throw new IOException("The target file has not been generated");
  91. }
  92. }
  93. /**
  94. * @return the native extension generated by the output format, ex. "ps" or "pdf".
  95. */
  96. protected abstract String getTargetExtension();
  97. /**
  98. * @return the output format for the FOP renderer, i.e. a MIME type.
  99. */
  100. protected String getTargetFormat() {
  101. return this.targetFormat;
  102. }
  103. /** {@inheritDoc} */
  104. public BufferedImage produce(File src, int index, ProducerContext context) {
  105. try {
  106. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  107. userAgent.setTargetResolution(context.getTargetResolution());
  108. userAgent.setBaseURL(src.getParentFile().toURL().toString());
  109. File tempOut = new File(context.getTargetDir(),
  110. src.getName() + "." + index + "." + getTargetExtension());
  111. File tempPNG = new File(context.getTargetDir(),
  112. src.getName() + "." + index + "." + getTargetExtension() + ".png");
  113. try {
  114. OutputStream out = new FileOutputStream(tempOut);
  115. out = new BufferedOutputStream(out);
  116. try {
  117. Fop fop = fopFactory.newFop(getTargetFormat(), userAgent, out);
  118. SAXResult res = new SAXResult(fop.getDefaultHandler());
  119. Transformer transformer = getTransformer(context);
  120. transformer.transform(new StreamSource(src), res);
  121. } finally {
  122. IOUtils.closeQuietly(out);
  123. }
  124. convert(tempOut, tempPNG, context);
  125. BufferedImage img = BitmapComparator.getImage(tempPNG);
  126. return img;
  127. } finally {
  128. if (deleteTempFiles) {
  129. if (!tempOut.delete()) {
  130. log.warn("Can't delete " + tempOut);
  131. tempOut.deleteOnExit();
  132. }
  133. if (!tempPNG.delete()) {
  134. log.warn("Can't delete " + tempPNG);
  135. tempPNG.deleteOnExit();
  136. }
  137. }
  138. }
  139. } catch (Exception e) {
  140. e.printStackTrace();
  141. log.error(e);
  142. return null;
  143. }
  144. }
  145. }