1:工具準備
swftools.exe 下載
http://www.swftools.org/download.html
安裝至D磁碟
SWFTools提供了一系列將各種檔案轉換成swf的工具:
font2swf.exe
gif2swf.exe
jpeg2swf.exe
pdf2swf.exe
png2swf.exe
wav2swf.exe
這裡我們只使用pdf2swf.exe
flexpaper下載
http://code.google.com/p/flexpaper/
這裡我們使用已經編譯好的FlexPaper的flash版本
2:範例語言,這裡我使用的兩種開發環境做範例
php範例由pdf產生swf文件
複製代碼代碼如下:
<?php
/*
* Created on 2010-11-17
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
//取得檔案所在目錄
$dir=dirname(__FILE__) ;
//刪除測試文件
@unlink( $dir."//test.swf" );
//使用pdf2swf轉換指令
$command= "D:/SWFTools/pdf2swf.exe -t /"".$dir."//test.pdf/" -o /"".$dir."//test.swf/" -s flashversion= 9 ";
//建立shell對象
$WshShell = new COM("WScript.Shell");
//執行cmd指令
$oExec = $WshShell->Run("cmd /C ". $command, 0, true);
?>
java範例
複製代碼代碼如下:
<%
/*
* Created on 2010-11-17
*/
//取得檔案所在目錄
String path=request.getRealPath("/");
//使用pdf2swf轉換指令
String command= "D:/SWFTools/pdf2swf.exe -t /""+path+"//test.pdf/" -o /""+path+"//test.swf/" -s flashversion=9 ";
//執行cmd指令
Runtime.getRuntime().exec("cmd /c "+command);
%>
以上是php,java將pdf轉換成swf方式,那顯示呢,這樣我們會使用到flexpaper,以下是flexpaper的使用使用flexpaper展現swf
複製代碼代碼如下:
<script type="text/javascript" src="js/swfobject/swfobject.js"></script>
<script type="text/javascript">
var swfVersionStr = "10.0.0";
var xiSwfUrlStr = "playerProductInstall.swf";
var flashvars = {
SwfFile : escape("test.swf"),
Scale : 0.6,
ZoomTransition : "easeOut",
ZoomTime : 0.5,
ZoomInterval : 0.1,
FitPageOnLoad : false,
FitWidthOnLoad : true,
PrintEnabled : true,
FullScreenAsMaxWindow : false,
ProgressiveLoading : true,
PrintToolsVisible : true,
ViewModeToolsVisible : true,
ZoomToolsVisible : true,
FullScreenVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true,
localeChain: "zh_CN"
};
var params = {
}
params.quality = "high";
params.bgcolor = "#ffffff";
params.allowscriptaccess = "sameDomain";
params.allowfullscreen = "true";
var attributes = {};
attributes.id = "FlexPaperViewer";
attributes.name = "FlexPaperViewer";
swfobject.embedSWF(
"FlexPaperViewer.swf", "flashContent",
"650", "500",
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
swfobject.createCSS("#flashContent", "display:block;text-align:left;");
</script>
<body>
<div style="position:absolute;left:10px;top:10px;">
<div id="flashContent">
</div>
</div>
</body>
透過上述方式我們可以將pdf轉換成對應的swf文件,並透過flexpaper顯示,防止使用者下載,或copy。
pdf2swf的詳細參數說明,上網百度或Google都能查到,但是注意的最好指定flashversion 為9,防止出現一些意外的錯誤。
pdf2swf 中並不是所有的pdf都能轉換,加密的pdf pdf2swf轉換不了
flexpaper 的參數詳細說明請參考http://code.google.com/p/flexpaper/wiki/Parameters
flexpaper 常用的API http://code.google.com/p/flexpaper/wiki/API
如果出現亂碼問題可能是字元集,網路上有很多相關的解決方法可以查詢【我沒有碰到字元集堵的問題】。
還有pdf含有圖片可能轉換成swf後圖片不大清晰。
word,wps,txt等文件也可以採用該方式轉換,但工具不一定為swftools
上傳的flexpaper附件中已經包含flexpaper中相關文件不用重新下載,只要下載swftools,並安裝即可。
flexpaper附件中包含了pdf2swf.jsp與pdf2swf.php兩個檔案及flexpaper相關的元件可以在java或php環境中運作。