swfTools, flashplayer 浏览器显示

最近在项目中遇到文档预览的需求,和PM商讨了几种解决方案,最终还是选中了转为SWF的方式。下面就稍微记录一下自己的学习成果。

  工具:pdf2swf 下载地址:http://www.swftools.org/download.html

  安装完成后,在安装目录下可以看到N个单独可以运行的exe文件:

  提供了多种格式转swf的功能,不过这里我只用了pdf2swf这一个,在我的项目里有一个service会将上传的文件直接转成pdf保存一个副档,需要预览的时候,直接获取这个pdf的副档就OK。

  下面看C#代码:

  

View Code
 public class PDF2Swf
    {
        #region 
        //根目录
        private static string ROOT_PATH = AppDomain.CurrentDomain.BaseDirectory;
        //pdf转swf
        private static string PDF2SWF_PATH = "Shells\\SWFTools\\pdf2swf.exe";
        //合并swf
        private static string SWFCOMBINE_PATH = "Shells\\SWFTools\\swfcombine.exe";
        //导航
        private static string SWFVIEWER_PATH="Shells\\SWF\\rfxview.swf";

        private static string SWFTEMP_PATH = "Shells\\SWF\\temp.swf";
        //保存转成功的swf文件
        public static string SAVE_SWF_PATH = "Shells\\SWF\\preview.swf";
        //保存FLM上的PDF文档
        public static string SAVE_PDF_PATH = "Shells\\PDF\\preview.pdf";
        //语言包路径
        private static string XPDF_LANG_PATH = ConfigReader.ReadValue<string>("XPDF_LANG_PATH");

        public static string PREVIEW_PAGE_PATH = "Shells\\SWF\\preview.html";
        #endregion 

        ///<summary> 
        /// swf格式文件播放 
        ///</summary> 
        ///<param name="url"></param> 
        ///<param name="width"></param> 
        ///<param name="height"></param> 
        ///<returns></returns>
        public  static string AddSwf(string url) 
        { 
            System.Text.StringBuilder sb = new System.Text.StringBuilder();


            sb.Append("<OBJECT codeBase='http://active.macromedia.com/flash5/cabs/swflash.cab#version=8,0,0,0'"); 
            sb.Append(" height='100%' width='100%' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'>"); 
            sb.Append("<PARAM NAME='Movie' VALUE='" + url + "'>"); 
            sb.Append("<PARAM NAME='Play' VALUE='true'>"); 
            sb.Append("<PARAM NAME='Loop' VALUE='true'>"); 
            sb.Append("<PARAM NAME='Quality' VALUE='High'>"); 
            sb.Append("<PARAM NAME='FLASHVARS' VALUE='zoomtype=3'>"); 
            sb.Append("<embed src='" + url + @"' height='100%' height='100%' play='true' ALIGN='' loop='true' quality='high'  
            pluginspage='http://www.macromedia.com/go/getflashplayer' 
            type='application/x-shockwave-flash' flashvars='zoomtype=3'>"); 
            sb.Append("</embed>"); 
            sb.Append("</OBJECT>"); 

            return sb.ToString(); 
        }

        ///<summary>
        /// 传入PDF的文件路径,以及输出文件的位置,执行pdf2swf的命令
        ///</summary>
        ///<param name="strPDFPath"></param>
        ///<param name="strSwfPath"></param>
        public static bool DoPDF2Swf(string strPDFPath, string strSwfPath)
        {
            bool isSuccess = false;
            //如果PDF不存在
            if (!File.Exists(strPDFPath))
            {
                return false;
            }

            #region 清理之前的记录
            if (File.Exists(strSwfPath))
            {
                //已经存在,删除
                File.Delete(strSwfPath);
            }
            if (File.Exists(GetPath(SWFTEMP_PATH)))
            {
                File.Delete(GetPath(SWFTEMP_PATH));
            }
            #endregion

            //将pdf文档转成temp.swf文件
            string strCommand = String.Format("{0} -T 8  -s languagedir={3} {1} -o {2}",
                GetPath(PDF2SWF_PATH),strPDFPath, GetPath(SWFTEMP_PATH),XPDF_LANG_PATH);
            double spanMilliseconds = RunShell(strCommand);

            //第一步转档失败,则返回
            if (!File.Exists(GetPath(SWFTEMP_PATH)))
            {
                return false;
            }

            //将temp.swf加入到rfxview.swf加入翻页的导航
            strCommand = String.Format("{0} {1} viewport={2} -o {3}",
                GetPath(SWFCOMBINE_PATH),GetPath(SWFVIEWER_PATH),GetPath(SWFTEMP_PATH),strSwfPath);
            spanMilliseconds = RunShell(strCommand);

            if (File.Exists(strSwfPath))
            {
                isSuccess = true;
            }
            return isSuccess;
        }

        ///<summary>
        /// 获取文件全路径
        ///</summary>
        ///<param name="path"></param>
        ///<returns></returns>
        public static string GetPath(string path)
        {
            //HttpContext.Current.Server.MapPath(path);
            return String.Format("{0}{1}", ROOT_PATH, path);
        }

        ///<summary>
        /// 运行命令
        ///</summary>
        ///<param name="strShellCommand">命令字符串</param>
        ///<returns>命令运行时间</returns>
        private static double RunShell(string strShellCommand)
        {
            double spanMilliseconds = 0;
            DateTime beginTime = DateTime.Now;

            Process cmd = new Process();
            cmd.StartInfo.FileName = "cmd.exe";
            cmd.StartInfo.UseShellExecute = false;
            cmd.StartInfo.CreateNoWindow = true;
            cmd.StartInfo.Arguments = String.Format(@"/c {0}", strShellCommand);
            cmd.Start();
            cmd.WaitForExit();
            cmd.Close();

            DateTime endTime = DateTime.Now;
            TimeSpan timeSpan = endTime - beginTime;
            spanMilliseconds = timeSpan.TotalMilliseconds;

            return spanMilliseconds;
        }

        ///<summary>
        /// 根据DownLoadURL从FLM获取资料,保存PDF文档到指定位置,返回文件的路径
        ///</summary>
        ///<param name="strDownLoadUrl"></param>
        ///<returns></returns>
        public static string SavePDF(string strDownLoadUrl)
        {
            try
            {
                //截取FLM的FileID
                string strFileID = strDownLoadUrl.Substring(strDownLoadUrl.LastIndexOf('=')+1);
                string strFileName = "";
                AttachService service = new AttachService();
                byte[] pdfBuffer = service.GetFileByte(strFileID, ref strFileName, "Y",Utility.GetProfile().englishName);

                string strPhysicalPDFPath = GetPath(SAVE_PDF_PATH);
                
                //如果已经有存在则先删掉
                if (File.Exists(strPhysicalPDFPath))
                {
                    File.Delete(strPhysicalPDFPath);
                }

                //建一个PDF文档,将文件流写入文件保存
                FileStream fs = new FileStream(strPhysicalPDFPath, FileMode.Create, FileAccess.Write);
                fs.Write(pdfBuffer, 0, pdfBuffer.Length);
                fs.Close();

                return strPhysicalPDFPath;
            }
            catch (Exception ex)
            {
                throw new Exception("保存PDF文档失败:"+ex.Message);
            }
        }

        ///<summary>
        /// 保证当前的文件名唯一
        ///</summary>
        ///<returns></returns>
        private static string GetPDFName()
        {
            return DateTime.Now.ToLongTimeString().Replace(':','_')+DateTime.Now.Millisecond;
        }
    }

 

  使用的时候调用DoPDF2Swf(string strPDFPath, string strSwfPath)传入pdf以及输出的swf路径,

  任务会先调用pdf2swf.exe将pdf文档转成temp.swf文件:

    pdf2swf [-options] file.pdf -o file.swf

 

  

 

  然后再调用swfcombine.exe合并tmep.swf以及rfxview.swf文件,输出到preview.swf文件:

    swfcombine.exe rfxview.swf viewport={tmep.swf} -o {preview.swf}

  

   最后在页面中呈现。

复制代码
 1 <html>
 2 
 3 <body style="padding: 0px; margin: 0px">
 4 
 5 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
 6  width="100%"
 7  height="100%"
 8  
 9 codebase="http://active.macromedia.com/flash5/cabs/swflash.cab#version=8,0,0,0">
10         
11 <param name="MOVIE" value="preview.swf">
12         
13 <param name="PLAY" value="true">
14         
15 <param name="LOOP" value="true">
16         
17 <param name="QUALITY" value="high">
18         
19 <param name="FLASHVARS" value="zoomtype=3">
20           
21 <embed src="preview.swf" width="100%" 
22 height="100%"
23  play="true" ALIGN="" loop="true" quality="high"
24  type="application/x-shockwave-flash"
25  
26 flashvars="zoomtype=3"
27  pluginspage="http://www.macromedia.com/go/getflashplayer">
28           
29 </embed>
30 </object>
31 </body>
32 </html>
复制代码

最终页面呈现效果:



第二篇  此文所参考 文章

前几天使用PDF2SWF实现了项目中的一个预览的小功能,后期遇到中文内容无法成功显示的Bug,困扰的不轻,网上也搜到不少资料,但是当时是在公司里处理问题,很多网上资源被公司屏蔽,导致迟迟不能奏效,最后折腾了大半天,才实现功能,下面详细总结一下。

  准备资料:

  1.xpdfbin-win-3.03.zip、xpdf-chinese-simplified.tar.gz 

  下载地址:http://www.foolabs.com/xpdf/download.html

  2.两个中文字体文件:gkai00mp.ttf、Gbsn00lp.ttf

  下载地址:http://code.google.com/p/atyu30/downloads/detail?name=gbsn00lp.ttf.tar.gz&can=2&q=

       http://download.csdn.net/detail/blackjack2007u/1841186(不要骂我,被逼无奈好不容易在CSDN找到这个下载地址)

  3.加上PDF2SWF工具http://www.swftools.org/download.html

  首先安装SWFTools,不考虑中文的情况下,一个pdf2swf.exe已经足够我们使用转换任务。

  下面集中精力解决中文字符的问题。

  1.解压缩xpdfbin-win-3.03.zip到指定目录(C:\xpdf)

  2.解压缩xpdf-chinese-simplified.tar.gz 到上面的目录下(C:\xpdf\xpdf-chinese-simplified)

  3.拷贝两个字体文件gkai00mp.ttf、Gbsn00lp.ttf到CMap目录下(C:\xpdf\xpdf-chinese-simplified\CMap)

  4.修改C:\xpdf\xpdf-chinese-simplified下的add-to-xpdfrc文件<注意相关路径配置>


在普通的pdf2swf -o {0} -t {1} 形式的命令后,加上一个-s 指定languagedir,就可以成功解决中文PDF2SWF的问题啦!



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值