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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2005-2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.visual;
  18. import java.awt.image.BufferedImage;
  19. import java.io.BufferedOutputStream;
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. import java.text.MessageFormat;
  25. import javax.xml.transform.Transformer;
  26. import javax.xml.transform.sax.SAXResult;
  27. import javax.xml.transform.stream.StreamSource;
  28. import org.apache.avalon.framework.configuration.Configurable;
  29. import org.apache.avalon.framework.configuration.Configuration;
  30. import org.apache.avalon.framework.configuration.ConfigurationException;
  31. import org.apache.commons.io.IOUtils;
  32. import org.apache.fop.apps.FOUserAgent;
  33. import org.apache.fop.apps.Fop;
  34. import org.apache.fop.apps.FopFactory;
  35. /**
  36. * BitmapProducer implementation that uses the PS or PDF renderer and an external converter
  37. * to create bitmaps.
  38. * <p>
  39. * Here's what the configuration element looks like for the class:
  40. * <p>
  41. * <pre>
  42. * <producer classname="<concrete class>">
  43. * <converter>mypdf2bmp {0} {1} {2}</converter>
  44. * <delete-temp-files>false</delete-temp-files>
  45. * </producer>
  46. * </pre>
  47. * <p>
  48. * You will notice the three parameters in curly braces (java.util.MessageFormat style) to the
  49. * converter call:
  50. * <ul>
  51. * <li>0: the input file
  52. * <li>1: the output bitmap file
  53. * <li>2: the requested resolution in dpi for the generated bitmap
  54. * </ul>
  55. * <p>
  56. * The "delete-temp-files" element is optional and defaults to true.
  57. */
  58. public abstract class AbstractPSPDFBitmapProducer extends AbstractBitmapProducer
  59. implements Configurable {
  60. // configure fopFactory as desired
  61. private FopFactory fopFactory = FopFactory.newInstance();
  62. private String converter;
  63. private boolean deleteTempFiles;
  64. /** @see org.apache.avalon.framework.configuration.Configurable */
  65. public void configure(Configuration cfg) throws ConfigurationException {
  66. this.converter = cfg.getChild("converter").getValue();
  67. this.deleteTempFiles = cfg.getChild("delete-temp-files").getValueAsBoolean(true);
  68. }
  69. /**
  70. * Calls an external converter to convert the file generated by FOP to a bitmap.
  71. * @param inFile the generated output file to be converted
  72. * @param outFile the target filename for the bitmap
  73. * @param context context information (required bitmap resolution etc.)
  74. * @throws IOException in case the conversion fails
  75. */
  76. public void convert(File inFile, File outFile, ProducerContext context) throws IOException {
  77. outFile.delete();
  78. //Build command-line
  79. String cmd = MessageFormat.format(converter,
  80. new Object[] {inFile.toString(), outFile.toString(),
  81. Integer.toString(context.getTargetResolution())});
  82. ConvertUtils.convert(cmd, null, null, log);
  83. if (!outFile.exists()) {
  84. throw new IOException("The target file has not been generated");
  85. }
  86. }
  87. /**
  88. * @return the native extension generated by the output format, ex. "ps" or "pdf".
  89. */
  90. protected abstract String getTargetExtension();
  91. /**
  92. * @return the output format for the FOP renderer, i.e. a MIME type.
  93. */
  94. protected abstract String getTargetFormat();
  95. /** @see org.apache.fop.visual.BitmapProducer */
  96. public BufferedImage produce(File src, ProducerContext context) {
  97. try {
  98. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  99. userAgent.setTargetResolution(context.getTargetResolution());
  100. userAgent.setBaseURL(src.getParentFile().toURL().toString());
  101. File tempOut = new File(context.getTargetDir(),
  102. src.getName() + "." + getTargetExtension());
  103. File tempPNG = new File(context.getTargetDir(),
  104. src.getName() + "." + getTargetExtension() + ".png");
  105. try {
  106. OutputStream out = new FileOutputStream(tempOut);
  107. out = new BufferedOutputStream(out);
  108. try {
  109. Fop fop = fopFactory.newFop(getTargetFormat(), userAgent, out);
  110. SAXResult res = new SAXResult(fop.getDefaultHandler());
  111. Transformer transformer = getTransformer(context);
  112. transformer.transform(new StreamSource(src), res);
  113. } finally {
  114. IOUtils.closeQuietly(out);
  115. }
  116. convert(tempOut, tempPNG, context);
  117. BufferedImage img = BitmapComparator.getImage(tempPNG);
  118. return img;
  119. } finally {
  120. if (deleteTempFiles) {
  121. if (!tempOut.delete()) {
  122. log.warn("Can't delete " + tempOut);
  123. tempOut.deleteOnExit();
  124. }
  125. if (!tempPNG.delete()) {
  126. log.warn("Can't delete " + tempPNG);
  127. tempPNG.deleteOnExit();
  128. }
  129. }
  130. }
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. log.error(e);
  134. return null;
  135. }
  136. }
  137. }