klxedu / blog

2 stars 0 forks source link

文档预览转换方案 #35

Open songhuiqing88 opened 6 years ago

songhuiqing88 commented 6 years ago

文档预览转换方案

产品方案是先通过openoffice将文档统一转换成pdf,在使用icepdf将pdf转换成图片。主要实现代码如下。

大数据平台集成

  • (数据平台)按照文档转PDF及PDF转图片过程进行转换并存储,记录统一标识(可根据标识获取一组图片)
  • (项目服务端)增加根据标识获取一组图片接口,返回可播放的图片URL列表。
  • (项目移动端)移动端播放,通过标识获取图片组接口,拿到各图片的访问URL。

文档转PDF

开源方案,使用openoffice做统一类型转换,将doc、xls、ppt格式统一转换成pdf。主要集成步骤如下。

  1. 安装openoffice,可参照官网安装。

  2. 启动服务

    • linux启动
      nohup  /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
    • windows启动
      C:\Program Files (x86)\OpenOffice 4\program\soffice.exe -headless -accept="socket,host=127.0.0.1,port=8100;urp;"
  3. java集成openoffice转换

    • POM增加依赖
      <dependency>
      <groupId>com.artofsolving</groupId>
      <artifactId>jodconverter</artifactId>
      <version>2.2.1</version>
      </dependency>
      <dependency>
      <groupId>org.openoffice</groupId>
      <artifactId>jurt</artifactId>
      <version>3.0.1</version>
      </dependency>
      <dependency>
      <groupId>org.openoffice</groupId>
      <artifactId>ridl</artifactId>
      <version>3.0.1</version>
      </dependency>
      <dependency>
      <groupId>org.openoffice</groupId>
      <artifactId>juh</artifactId>
      <version>3.0.1</version>
      </dependency>
      <dependency>
      <groupId>org.openoffice</groupId>
      <artifactId>unoil</artifactId>
      <version>3.0.1</version>
      </dependency>
      <dependency>
      <groupId>org.javolution</groupId>
      <artifactId>javolution</artifactId>
      <version>5.2.6</version>
      </dependency>
    • 代码转换
      public void convert(String src, String dist) throws Exception {
      // 1: 打开连接
      OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
      connection.connect();
      DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
      // 2:获取Format
      DocumentFormatRegistry factory = new BasicDocumentFormatRegistry();
      DocumentFormat inputDocumentFormat = factory.getFormatByFileExtension(FilenameUtils.getExtension(src));
      DocumentFormat outputDocumentFormat = factory.getFormatByFileExtension(FilenameUtils.getExtension(dist));
      // 3:执行转换
      converter.convert(new File(src), inputDocumentFormat, new File(dist), outputDocumentFormat);
      }

      PDF转图片

  4. POM依赖

    <dependency>
    <groupId>org.icepdf</groupId>
    <artifactId>icepdf-core</artifactId>
    <version>6.2.4</version>
    </dependency>
    <dependency>
    <groupId>org.icepdf</groupId>
    <artifactId>icepdf-viewer</artifactId>
    <version>6.2.4</version>
    </dependency>
    <dependency>
    <groupId>org.icepdf</groupId>
    <artifactId>icepdf-pro</artifactId>
    <version>6.2.4</version>
    </dependency>
    <dependency>
    <groupId>org.icepdf</groupId>
    <artifactId>icepdf-pro-intl</artifactId>
    <version>6.2.4</version>
    </dependency>
  5. 去除水印

    
    package org.icepdf.core.application;

public class ProductInfo { public static String COMPANY = "ICEsoft Technologies, Inc."; public static String PRODUCT = "ICEpdf-pro"; public static String PRIMARY = "6"; public static String SECONDARY = "2"; public static String TERTIARY = "4"; public static String RELEASE_TYPE = ""; public static String BUILD_NO = "170"; public static String REVISION = "";

public String toString() { StringBuilder localStringBuilder = new StringBuilder(); localStringBuilder.append("\n"); localStringBuilder.append(COMPANY); localStringBuilder.append("\n"); localStringBuilder.append(PRODUCT); localStringBuilder.append(" "); localStringBuilder.append(PRIMARY); localStringBuilder.append("."); localStringBuilder.append(SECONDARY); localStringBuilder.append("."); localStringBuilder.append(TERTIARY); localStringBuilder.append(" "); localStringBuilder.append(RELEASE_TYPE); localStringBuilder.append("\n"); localStringBuilder.append("Build number: "); localStringBuilder.append(BUILD_NO); localStringBuilder.append("\n"); localStringBuilder.append("Revision: "); localStringBuilder.append(REVISION); localStringBuilder.append("\n"); return localStringBuilder.toString(); }

public String getVersion() { StringBuilder localStringBuilder = new StringBuilder(); /localStringBuilder.append(PRIMARY); localStringBuilder.append("."); localStringBuilder.append(SECONDARY); localStringBuilder.append("."); localStringBuilder.append(TERTIARY); localStringBuilder.append(" "); localStringBuilder.append(RELEASE_TYPE);/ return localStringBuilder.toString(); }

public static void main(String[] paramArrayOfString) { ProductInfo localProductInfo = new ProductInfo(); System.out.println(localProductInfo.toString()); } }

```java
package org.icepdf.core.pobjects.graphics;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import org.icepdf.core.application.ProductInfo;

public class Padding
{
  /*private static byte[] padding1 = { 54, 48, 50, 93, 81, 83, 13 };
  private static byte[] padding3 = { 51, 92, 95, 13, 50, 99, 78, 89, 98, 78, 97, 86, 92, 91, 13, 61, 98, 95, 93, 92, 96, 82, 96, 13, 60, 91, 89, 102 };*/

  private static byte[] padding1={};
  private static byte[] padding3 ={};

  public static final void getPadding(Graphics2D paramGraphics2D, Rectangle2D.Float paramFloat)
  {
    paramGraphics2D.scale(1.0D, -1.0D);
    paramGraphics2D.setColor(new Color(186, 0, 0));
    String str1 = new ProductInfo().getVersion();
    String str2 = new String(padding1) + str1;
    String str3 = new String(padding3);
    int i = 5;
    paramGraphics2D.setFont(new Font("Dialog", 1, 14));
    FontMetrics localFontMetrics = paramGraphics2D.getFontMetrics();
    Rectangle2D localRectangle2D = localFontMetrics.getStringBounds(str3.toCharArray(), 0, str3.length(), paramGraphics2D);
    int j = (int)(paramFloat.x + (paramFloat.width - localRectangle2D.getWidth()) / 2.0D);
    int k = -1 * (int)(paramFloat.getY() - paramFloat.getHeight() + i);
    paramGraphics2D.drawString(str3, j, k);
    localRectangle2D = localFontMetrics.getStringBounds(str2.toCharArray(), 0, str2.length(), paramGraphics2D);
    j = (int)(paramFloat.x + (paramFloat.width - localRectangle2D.getWidth()) / 2.0D);
    k = -1 * (int)(paramFloat.y - localRectangle2D.getHeight() - i);
    paramGraphics2D.drawString(str2, j, k);
  }

  static
  {
    for (int i = 0; i < padding1.length; ++i)
    {
      int tmp226_225 = i;
      byte[] tmp226_222 = padding1;
      tmp226_222[tmp226_225] = (byte)(tmp226_222[tmp226_225] + 19);
    }
    for (int i = 0; i < padding3.length; ++i)
    {
      int tmp253_252 = i;
      byte[] tmp253_249 = padding3;
      tmp253_249[tmp253_252] = (byte)(tmp253_249[tmp253_252] + 19);
    }
  }
}
  1. 代码转换
    public void convert(String src, String dist) throws Exception {
    Document document = new Document();
    document.setFile(src);
    float scale = 2f;// 缩放比例
    float rotation = 0f;// 旋转角度
    String pdfName = FilenameUtils.getBaseName(src);
    for (int i = 0; i < document.getNumberOfPages(); i++) {
        BufferedImage image = (BufferedImage) document.getPageImage(i,
                GraphicsRenderingHints.SCREEN,
                org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation,
                scale);
        RenderedImage rendImage = image;
        //生成临时文件
        File folderFile = new File(dist);
        if (!folderFile.exists()) {
            folderFile.mkdirs();
        }
        File file = new File(dist,pdfName+"_"+(i+1)+".jpg");
        try {
            ImageIO.write(rendImage, "jpg", file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        image.flush();
    }
    document.dispose();
    }