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.6KB

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