通过java实现txt,ppt,pdf上传之后转换为swf文件的功能实现

近期因为项目的需求,需要开发一个文档上传功能,和在线预览功能,目前上传的类型限制为txt.ppt,pdf而且需要把上传之后的文件转换为swf文件在前台页面的flash中播放预览。

在网上找的代码然后自己整合和修改之后:

功能实现需要,openoffice,swftools工具。

openoffice下载地址:http://rj.baidu.com/soft/detail/15989.html?ald

swftools下载地址:http://www.swftools.org/download.html(windows环境下下载.exe文件)

需要的jar包:

链接:https://pan.baidu.com/s/1skOyEjr
密码:2l9e

首先下载openoffice和swftools安装到本地,

需要把openoffice配置为window服务(这样可以实现开机自启)——借用其他大佬的代码:http://blog.csdn.net/Hollboy/article/details/50155725

ppt转pdf代码:


import java.io.File;

import java.io.IOException;

import java.net.ConnectException;

import java.util.Date;

import com.artofsolving.jodconverter.DocumentConverter;

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;

import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;

import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class DOCtoPDFUtil extends java.lang.Thread {

private File inputFile;// 需要转换的文件

private File outputFile;// 输出的文件

public DOCtoPDFUtil(File inputFile, File outputFile) {

this.inputFile = inputFile;

this.outputFile = outputFile;

}

public void docToPdf() {

Date start = new Date();

OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);

try {

connection.connect();

DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);

converter.convert(inputFile, outputFile);

} catch (ConnectException cex) {

cex.printStackTrace();

} finally {

// close the connection

if (connection != null) {

connection.disconnect();

connection = null;

}

}

}

public void run() {

this.docToPdf();

}

public File getInputFile() {
return inputFile;
}

public void setInputFile(File inputFile) {
this.inputFile = inputFile;
}

public File getOutputFile() {
return outputFile;
}

public void setOutputFile(File outputFile) {
this.outputFile = outputFile;
}

}




txt转pdf代码:


import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;


/**
 *  此程序主要是将txt转为pdf
 * 
 */


public class JAVAtoPDF {


// 把文件目录下的所有txt转换为pdf
public static void generatePDFByAll(File dir) {
String[] contents = dir.list();// 将文件目录中的文件提取出来
for (int i = 0; i < contents.length; i++) {
String fileName = dir + "\\" + contents[i];
if (fileName.substring(fileName.indexOf("."), fileName.length())
.equals(".txt")) {
toPDF(fileName,null);
}
}
}


// 单个txt转换为pdf
public static void generatePDFByOne(String dir,String name) {
String[] dirLength = dir.split("\\.");
if (dirLength[dirLength.length - 1].equals("txt")) {
System.out.println("文件名:" + dir);
toPDF(dir,name);
}
}


public static void toPDF(String fileName,String name) {
try {
// 首先创建一个字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 14, Font.NORMAL);
String line = null;
Document document;
document = new Document(PageSize.A4, 80, 80, 80, 80);
BufferedReader in = new BufferedReader(new FileReader(fileName));
PdfWriter.getInstance(
document,
new FileOutputStream(name));
document.open();
while ((line = in.readLine()) != null)
document.add(new Paragraph(25, line, FontChinese));
document.close();
in.close();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}


如果是通过txt转换的pdf后转swf需要配置一些东西啦,

借用其他大佬的代码:

因为txt转换的pdf转换swf文件会出现转换失败的问题,经过查找资料发现是因为io的缓存问题,启动两个线程刷新缓存区域解决问题

pdf转swf代码:



import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class PDFtoSWF {
private static final Logger log = LoggerFactory.getLogger(PDFtoSWF.class);


public static boolean convert(File sourceFile, File targetFile) {
try {
/**
* SWFTools_HOME在系统中的安装目录 --window需要指定到 pdf2swf.exe 文件
*/
String SWFTools_HOME = "D:\\SwfTools\\pdf2swf.exe";
String[] cmd = new String[7];
cmd[0] = SWFTools_HOME;
cmd[1] = "-i";
cmd[2] = sourceFile.getAbsolutePath();
cmd[3] = "-o";
cmd[4] = targetFile.getAbsolutePath();

/**

*此处是为了把txt转换的pdf文件转换为swf文件

*实际测试中txt转换的pdf文件转换为swf文件的时候全部为空白

*/

cmd[5]="-s";
//路径为解压的xpdf-chinese-simplified的路径
cmd[6]="languagedir=C://app//xpdf//xpdf-chinese-simplified";
Process pro = Runtime.getRuntime().exec(cmd);
// 获取进程的标准输入流
final InputStream is1 = pro.getInputStream();
// 获取进城的错误流
final InputStream is2 = pro.getErrorStream();
//创建一个线程刷新输入流的缓存区防止程序开始后挂起或者死锁
new Thread(new Runnable() {
@Override
public void run() {
try {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(is1));
while (bufferedReader.readLine() != null) ;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is1.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}


}
}).start();

//才开始的时候使用网上其他的代码导致txt转pdf之后转swf的时候只有debug的时候才能生成swf文件,不是debug启动的时候导致文件无法转换,线程挂起或者//死锁,因为缓存区域的问题

//创建一个线程刷新错误流的缓存区防止程序开始后挂起或者死锁
new Thread(new Runnable() {
@Override
public void run() {
try {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(is2));
while (bufferedReader.readLine() != null);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is1.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}


}
}).start();
pro.waitFor();
pro.exitValue();
} catch (Exception e) {
log.error("pdf转swf失败:", e);
return false;
}
return true;
}
}

在调用上面方法的地方要注意,使用openoffice的ppt转pdf的时候是在其他线程中运行的如果不等待这个线程运行完毕就调用pdf转swf的时候会导致查找不到文件最后没有转换的文件。

//因为此线程执行时候方法已经往下走导致其他方法先运行,所以要获此线程下面进行其他操作
Thread join=new Thread(new DOCtoPDFUtil(new File(uploadPpt), new File(uploadPdf)));
//开启这个线程
join.start();
//等待这个线程执行完毕只够才会执行主线程下面的代码
join.join();


ps:txt转换的pdf文件格式真的不好看,目前还没找到好的方法,先这样吧,以后有机会在分享。

声明:如果因为引用其他大佬的博客造成的侵权请联系删除。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值