加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

开源DMS - 文档管理系统 - logicaldoc 里面转换SWF

发布时间:2020-12-15 17:57:36 所属栏目:百科 来源:网络整理
导读:logicaldoc? ? 主页:?http://logicaldoc.com/ 文档:http://docs.logicaldoc.com/ 下载:?http://sourceforge.net/projects/logicaldoc/ ? ? logicaldoc 里面转换SWF用到了三个命令: command.convert=convert command.gs=gs command.pdf2swf=pdf2swf ? ? h

logicaldoc?

?

主页:?http://logicaldoc.com/

文档:http://docs.logicaldoc.com/

下载:?http://sourceforge.net/projects/logicaldoc/

?

?

logicaldoc 里面转换SWF用到了三个命令:

command.convert=convert

command.gs=gs

command.pdf2swf=pdf2swf

?

?

http://forums.logicaldoc.com/viewtopic.php?f=6&t=416&p=1050&hilit=command.convert#p1050

Speacking about the latest release 6.2.3,you need some external command.


GhostScript
ImageMagik
SWFTools

?

?

package com.logicaldoc.web;

import com.logicaldoc.core.document.Document;
import com.logicaldoc.core.document.dao.DocumentDAO;
import com.logicaldoc.core.document.thumbnail.ThumbnailManager;
import com.logicaldoc.core.store.Storer;
import com.logicaldoc.util.Context;
import com.logicaldoc.util.MimeType;
import com.logicaldoc.util.config.ContextProperties;
import com.logicaldoc.web.util.SessionUtil;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DocumentPreview extends HttpServlet
{
  protected static final String SUFFIX = "suffix";
  public static final String DOC_ID = "docId";
  private static final String FILE_VERSION = "fileVersion";
  private static final long serialVersionUID = -6956612970433309888L;
  protected static Log log = LogFactory.getLog(DocumentPreview.class);
  protected static String PDF2SWF = "command.pdf2swf";
  protected static String IMG2PDF = "command.convert";
  protected String SWF_DIRECT_CONVERSION_EXTS = "gif,png,pdf,jpeg,jpg,tiff,tif";

  public void doGet(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,IOException
  {
    String id = request.getParameter("docId");
    String fileVersion = request.getParameter("fileVersion");
    String suffix = request.getParameter("suffix");

    InputStream stream = null;
    try {
      Storer storer = (Storer)Context.getInstance().getBean(Storer.class);

      long docId = Long.parseLong(id);
      if (StringUtils.isEmpty(suffix))
        suffix = "thumb.jpg";
      DocumentDAO docDao = (DocumentDAO)Context.getInstance().getBean(DocumentDAO.class);
      Document doc = (Document)docDao.findById(docId);
      if (StringUtils.isEmpty(fileVersion))
        fileVersion = doc.getFileVersion();

      SessionUtil.validateSession(request.getParameter("sid"));

      String resource = storer.getResourceName(docId,fileVersion,suffix);

      if (!(storer.exists(docId,resource))) {
        log.debug("Need for preview creation");
        createPreviewResource(doc,resource);
      }

      stream = storer.getStream(docId,resource);

      if (stream == null) {
        log.debug("thumbnail not available");
        forwardPreviewNotAvailable(request,response);

        return;
      }
      downloadDocument(request,response,stream,storer.getResourceName(doc,suffix));
    } catch (Throwable t) {
      log.error(t.getMessage(),t);
      new IOException(t.getMessage());
    } finally {
      if (stream != null)
        stream.close();
    }
  }

  protected void createPreviewResource(Document doc,String fileVersion,String resource)
  {
    Storer storer = (Storer)Context.getInstance().getBean(Storer.class);
    String thumbResource = storer.getResourceName(doc,"thumb.jpg");

    if (!(storer.exists(doc.getId(),thumbResource))) {
      ThumbnailManager thumbManager = (ThumbnailManager)Context.getInstance().getBean(ThumbnailManager.class);
      try {
        thumbManager.createTumbnail(doc,fileVersion);
        log.debug("Created thumbnail " + resource);
      } catch (Throwable t) {
        log.error(t.getMessage(),t);
      }
    }

    if (resource.endsWith(".jpg")) {
      return;
    }

    if (!(storer.exists(doc.getId(),resource))) {
      InputStream is = null;
      File tmp = null;
      try {
        tmp = File.createTempFile("preview","");

        String docExtension = FilenameUtils.getExtension(doc.getFileName());
        if (this.SWF_DIRECT_CONVERSION_EXTS.contains(docExtension))
        {
          is = storer.getStream(doc.getId(),null));
          document2swf(tmp,docExtension,is);
        }
        else {
          is = storer.getStream(doc.getId(),thumbResource);

          document2swf(tmp,"jpg",is);
        }

        storer.store(tmp,doc.getId(),resource);
        log.debug("Created preview " + resource);
      } catch (Throwable e) {
        log.error(e.getMessage(),e);
      } finally {
        if (tmp != null)
          FileUtils.deleteQuietly(tmp);
        if (is != null)
          try {
            is.close();
          }
          catch (IOException e) {
          }
      }
    }
  }

  protected void forwardPreviewNotAvailable(HttpServletRequest request,HttpServletResponse response) {
    try {
      RequestDispatcher rd = request.getRequestDispatcher("/skin/images/preview_na.gif");
      rd.forward(request,response);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void downloadDocument(HttpServletRequest request,HttpServletResponse response,InputStream is,String filename)
    throws FileNotFoundException,IOException
  {
    String mimetype = MimeType.getByFilename(filename);

    response.setContentType(mimetype);

    OutputStream os = response.getOutputStream();

    int letter = 0;
    try {
      while ((letter = is.read()) != -1)
        os.write(letter);
    }
    finally {
      os.flush();
      os.close();
      is.close();
    }
  }

  protected void document2swf(File swfCache,String extension,InputStream docInput)
    throws IOException
  {
    File tmpPdf = null;
    try {
      tmpPdf = File.createTempFile("preview",".pdf");
      if ("pdf".equals(extension.toLowerCase())) {
        FileOutputStream fos = null;
        try {
          fos = new FileOutputStream(tmpPdf);
          IOUtils.copy(docInput,fos);
          fos.flush();
        } catch (Throwable e) {
        }
        finally {
          IOUtils.closeQuietly(fos);
        }
      } else {
        img2pdf(docInput,extension,tmpPdf); }
      pdf2swf(tmpPdf,swfCache);
    } finally {
      if (tmpPdf != null)
        FileUtils.deleteQuietly(tmpPdf);
    }
  }

  protected void img2pdf(InputStream is,File output)
    throws IOException
  {
    File tmp = File.createTempFile("preview",extension);
    String inputFile = tmp.getPath() + "[0]";
    FileOutputStream fos = null;
    try
    {
      fos = new FileOutputStream(tmp);
      IOUtils.copy(is,fos);
      fos.flush();
      fos.close();

      ContextProperties conf = (ContextProperties)Context.getInstance().getBean(ContextProperties.class);

      ProcessBuilder pb = new ProcessBuilder(new String[] { conf.getProperty(IMG2PDF),inputFile,"  -compress None -quality 100 ",output.getPath() });

      Process process = pb.start();

      Thread wrapper = new Thread(process)
      {
        public void run()
        {
          try
          {
            this.val$process.waitFor();
          }
          catch (InterruptedException e) {
          }
        }
      };
      wrapper.start();

      for (int i = 0; i < 10; ++i)
        if (wrapper.isAlive())
          try {
            Thread.sleep(1000L);
          }
          catch (InterruptedException e) {
          }
      wrapper.interrupt();

      process.destroy();
    } catch (Throwable e) {
    }
    finally {
      IOUtils.closeQuietly(fos);
      tmp.delete();
    }
  }

  protected void pdf2swf(File input,File output)
    throws IOException
  {
    ContextProperties conf = (ContextProperties)Context.getInstance().getBean(ContextProperties.class);
    String[] cmd = composeCmd(conf.getProperty(PDF2SWF),input,output);
    BufferedReader stdout = null;
    Process process = null;
    try {
      ProcessBuilder pb = new ProcessBuilder(cmd);
      process = pb.start();
      stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));

      while (stdout.readLine() != null);
      process.waitFor();
    } catch (Throwable e) {
      output.delete();
      log.error("Error in PDF to SWF conversion",e);
    } finally {
      if (process != null)
        process.destroy();
      IOUtils.closeQuietly(stdout);
    }
  }

  protected String[] composeCmd(String command,File input,File output)
  {
    String[] standardCmd = { command,"-f","-T 9","-t","-G","-s storeallcharacters",input.getPath(),"-o",output.getPath() };

    String[] imgCmd = { command,"-T 9 -q 30",output.getPath() };
    if (command.endsWith("convert"))
      return imgCmd;

    return standardCmd;
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读