您的位置:首页 > 运维架构 > Linux

Linux/Centos环境安装配置Swftools pdf转swf 及java代码转化

2014-05-06 20:28 651 查看
1、安装所需的库和组件。安装前准备,主要安装的是下面几个组件。

yum install gcc* automake zlib-devel libjpeg-devel giflib-devel freetype-devel

2、下载编译安装swftools。最新下载地址:http://www.swftools.org/download.html

wget http://www.swftools.org/swftools-2013-04-09-1007.tar.gz tar vxzf swftools-2013-04-09-1007.tar.gz
cd swftools-2013-04-09-1007
./configure --prefix=/usr/local/swftools
make
make install

3、设置swftools环境变量,使pdf2swf成为一个可执行命令

vim /etc/profile
export PATH=$PATH:/usr/local/swftools/bin/

4、安装xpdf语言包。下载xpdf-chinese-simplified.tar.gz文件,解压到/usr/local下,编辑add-to-xpdfrc文件,如下:

wget ftp://ftp.foolabs.com/pub/xpdf/xpdf-chinese-simplified.tar.gz wget http://www.nginxs.com/download/font.zip

tar zxvf xpdf-chinese-simplified.tar.gz

unzip font.zip

mv Gbsn00lp.ttf gkai00mp.ttf xpdf-chinese-simplified/CMap/
##把 xpdf-chinese-simplified 移动到 /usr/share/xpdf
mkdir -p /usr/share/xpdf

mv xpdf-chinese-simplified /usr/share/xpdf
##修改配置文件
cd /usr/share/xpdf/xpdf-chinese-simplified

vi add-to-xpdfrc

#----- begin Chinese Simplified support package (2011-sep-02)

cidToUnicode Adobe-GB1 /usr/share/xpdf/Adobe-GB1.cidToUnicode

unicodeMap ISO-2022-CN /usr/share/xpdf/ISO-2022-CN.unicodeMap

unicodeMap EUC-CN /usr/share/xpdf/EUC-CN.unicodeMap

unicodeMap GBK /usr/share/xpdf/GBK.unicodeMap

cMapDir Adobe-GB1 /usr/share/xpdf/CMap

toUnicodeDir /usr/share/xpdf/CMap

displayCIDFontTT Adobe-GB1 /usr/share/xpdf/CMap/gkai00mp.ttf

displayCIDFontTT Adobe-GB1 /usr/share/xpdf/CMap/Gbsn00lp.ttf
用winscp上传上来一个 pdf 转换看看。

字体文件自己下载…

5、最后使用如下转换命令测试:

/usr/local/swftools/bin/pdf2swf -o /root/download/fenxi.swf -T -z -t /root/download/fenxi.pdf -s languagedir=/usr/share/xpdf/xpdf-chinese-simplified -s flashversion=9

如果在转换过程中出现:

Error: Couldn't find a font for '宋体'

或者

ERROR Internal error: No current splash fontinfo

或者

ERROR Invalid charid 1 for font 0x60000000000ac370 (0 characters)

需要自己在网上,或者本机的c:/Windows/fonts目录下寻找相应的确实字体(例如‘宋体’),然后放置在CMap文件夹内,按照斜线部分在add-to-xpdfrc文件中添加相应的配置,即可。

swftools_swf_Path_Linux=/usr/local/swftools/share/swftools/swfs/rfxview.swf

swftools_pdf2swf_Path_Linux=/usr/local/swftools/bin/pdf2swf

swftools_swfcombine_Path_Linux=/usr/local/swftools/bin/swfcombine

swftools_xpdf_Path_Linux=/usr/share/xpdf/

public class PdfToSwf {

private static final Object OBJECT=new Object();

private static final Logger LOGGER=Logger.getLogger(PdfToSwf.class);

//可以从配置文件获取

private String CMD_PDF2SWF;

private String CMD_SWFCOMBINE;

private String XPDF_PATH;

private String PDF2SWF_SWF;

/**

*

* @param cmd_pdf2swf pdf2swf程序路径

* @param cmd_swfcombine swfcombine程序路径

* @param path_xpdf XPDF的路径

* @param default_swf SWF模板

*/

public PdfToSwf(String cmd_pdf2swf,String cmd_swfcombine,String path_xpdf,String default_swf){

this.CMD_PDF2SWF=cmd_pdf2swf;

this.CMD_SWFCOMBINE=cmd_swfcombine;

this.XPDF_PATH=path_xpdf;

this.PDF2SWF_SWF=default_swf;

}

/**

* pdf转swf

* @param sourcePath pdf源文件路径

* @param destPath swf文件统一转换的目录

* @param fileName swf文件名

* @return 是否成功

* @throws IOException

*/

public boolean convertPDF2SWF(String sourcePath, String destPath,

String fileName) throws IOException {

synchronized (OBJECT) {

File dest = new File(destPath);

if (!dest.exists()) {

dest.mkdirs();

}

File source = new File(sourcePath);

if (!source.exists()) {

return false;

}

LOGGER.info("1 -> sourcePath:"+sourcePath+"\tdestPath:"+destPath+"\tfileName:"+fileName);

String command = CMD_PDF2SWF+" -z -s flashversion=9 -t " + sourcePath

+ " -o " + destPath + fileName + ""+" -s languagedir="+XPDF_PATH;

LOGGER.info("1 ->command:"+command);

Process pro = Runtime.getRuntime().exec(command);

final InputStream inputStream = pro.getInputStream();

final InputStream errStream = pro.getErrorStream();

new Thread(){

@Override

public void run() {

BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

try {

String line=null;

while((line=br.readLine()) != null) {

LOGGER.info("PDF2SWF INFO:"+line);

}

} catch (IOException e) {

LOGGER.info("PDF2SWF ERROR:"+e.getMessage());

e.printStackTrace();

}

}

}.start();

new Thread(){

@Override

public void run() {

BufferedReader br = new BufferedReader(new InputStreamReader(errStream));

try {

String line=null;

while((line=br.readLine()) != null) {

LOGGER.info("PDF2SWF ERROR:"+line);

}

} catch (IOException e) {

LOGGER.info("PDF2SWF ERROR:"+e.getMessage());

e.printStackTrace();

}

}

}.start();

try {

int r=pro.waitFor();

if(r!=0){

return false;

}

} catch (InterruptedException e) {

e.printStackTrace();

return false;

}

LOGGER.info("2 -> sourcePath"+sourcePath+"destPath:"+destPath+"fileName:"+fileName);

command = CMD_SWFCOMBINE+" -z -X 800 -Y 600 "+PDF2SWF_SWF+" viewport="

+ destPath + fileName + " -o " + destPath + fileName + "";

LOGGER.info("2 ->command:"+command);

pro = Runtime.getRuntime().exec(command);

final InputStream inputStream2 = pro.getInputStream();

final InputStream errStream2 = pro.getErrorStream();

new Thread(){

@Override

public void run() {

BufferedReader br = new BufferedReader(new InputStreamReader(inputStream2));

try {

String line=null;

while((line=br.readLine()) != null) {

LOGGER.info("PDF2SWF INFO:"+line);

}

} catch (IOException e) {

LOGGER.info("PDF2SWF ERROR:"+e.getMessage());

e.printStackTrace();

}

}

}.start();

new Thread(){

@Override

public void run() {

BufferedReader br = new BufferedReader(new InputStreamReader(errStream2));

try {

String line=null;

while((line=br.readLine()) != null) {

LOGGER.info("PDF2SWF Error:"+line);

}

} catch (IOException e) {

LOGGER.info("PDF2SWF ERROR:"+e.getMessage());

e.printStackTrace();

}

}

}.start();

try {

int r=pro.waitFor();

if(r!=0){

return false;

}

} catch (InterruptedException e) {

e.printStackTrace();

return false;

}

return pro.exitValue()==0;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: