Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AbstractPSPDFBitmapProducer.java 6.2KB

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