分享

FlexPaper SwfTools实现的在线文档功能

 X的世界 2012-08-10

    最近一个项目需要实现一个在线浏览文档的功能。准备使用FlexPaper配合Pdf2Swf实现。

主要需求在于:

    文档页数很多,少则几百页,多则上千页。
    相应的文档大小也在50MB以上。

根据需求,将该功能拆分成了三部分:

    上传:支持大文件,断点续传。
    文件转换服务:在后台进行Pdf转Swf。
    在线浏览:分页加载。

其中文件上传部分是我同事实现的,这里主要讲下后台服务和在线浏览部分。

文件转换服务

大体思路是:
    后台服务定时扫描文件夹PdfPath,如果发现Pdf文件,则调用Pdf2Swf.exe,将PDF逐页转换成Swf文件,存入SwfPath中的与文件名称对应的文件夹里。

    这里使用了SwfTools套件中的Pdf2Swf工具:下载

主要代码:

//PDF转换成SWF
private void ConvertPDFtoSWF(string pdfFile)
{
    using (Process p = new Process())
    {
        SystemLog.CurrentLogger.Debug(string.Format("正在处理 {0} ...", pdfFile));

        string pdf2swfExe = "pdf2swf.exe";
        string savePath = GetSavePathFromName(pdfFile);

        string cmd = pdf2swfExe;
        string args = "  -t \"" + pdfFile + "\"  -o \"" + savePath + pdfFile.Split('\\').Last().Replace(".pdf", "")
            + "%.swf\" -s drawonlyshapes -s flashversion=9";

        p.StartInfo.FileName = cmd;
        p.StartInfo.Arguments = args;
        p.StartInfo.UseShellExecute = false;

        //此类提供的标准output流只有2k,不要重定向
        p.StartInfo.RedirectStandardOutput = false;

        p.StartInfo.CreateNoWindow = true;
        p.Start();
        p.PriorityClass = ProcessPriorityClass.High;
        p.WaitForExit();

        SystemLog.CurrentLogger.Debug(string.Format("{0} 处理完成。", pdfFile));

        if (AppConfiguration.DeleteConvertedPdf)
        {
            //转换完成后删除Pdf
            File.Delete(pdfFile);
            SystemLog.CurrentLogger.Debug(string.Format("{0} 已删除。", pdfFile));
        }
        else
        {
            //重命名Pdf
            File.Move(pdfFile, pdfFile + ".bak");
            SystemLog.CurrentLogger.Debug(string.Format("{0} 已重命名。", pdfFile));
        }
    }
}

gentleface.com free icon set

    写服务的时候遇到一个奇怪的Bug,发布为服务的时候,测试用的Pdf文件每次转换到37页的时候就卡住了,但是用命令行进行调试的时候却一切OK。
    Google后发现,原来是Process的output流问题:output流只有2KB,而Pdf2Swf会产生大量输出,2KB很快就耗尽了。

    之前在测试的时候,发现转换过程中会出现文字丢失的现象。这里使用了-s drawonlyshapes 这个参数,将Pdf全部作为图片转换的。这样虽然保证了兼容性,但是牺牲了空间。作为图片生成的Swf比文本格式的Swf要大不少,不知道大家有没有什么好的解决方法。

在线浏览

    FlexPaper支持分页加载,采用{filename[*,padding],total pages}这种语法即可。

主要代码:

<a id="viewerPlaceHolder" style="width: 800px; height: 600px; display: block"></a>

<script type="text/javascript">
    var fp = new FlexPaperViewer(
    'FlexPaperViewer',
     'viewerPlaceHolder', { config: {
         SwfFile: 'SwfFolder/<%=Folder %>/{<%=Folder %>[*,0].swf,<%=PageNum %>}',
         localeChain: "zh_CN", //中文
         Scale: 1,
         ZoomTransition: 'easeOut',
         ZoomTime: 0.5,
         ZoomInterval: 0.2,
         FitPageOnLoad: false,
         FitWidthOnLoad: false,
         PrintEnabled: true,
         FullScreenAsMaxWindow: false,
         ProgressiveLoading: false,
         MinZoomSize: 0.2,
         MaxZoomSize: 5,
         SearchMatchAll: false,
         InitViewMode: 'Portrait',

         ViewModeToolsVisible: true,
         ZoomToolsVisible: true,
         NavToolsVisible: true,
         CursorToolsVisible: true,
         SearchToolsVisible: true
     }
     });
</script>

warning     FlexPaper无法载入中文文件名。如果右上角的圈圈一直在转,注意是不是文件名称的问题。   

参考文章

c# System.Diagnostics.Process 调用外部程序时WaitForExit锁死问题分析及解决方案

FlexPaper+SWFTools 实现仿百度文库及一些小问题

Pdf2Swf命令行参数

解决FlexPaper分页分段加载问题

源码

分页加载:FlexPaper.zip

文件转换服务:PDFtoSWFService.zip

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多