Aspose实现word转pdf预览,并指定服务器字体路径并实现现在预览

1、前端代码

$("#Id").html('<embed src='+action+' type="application/pdf" style="width:100%; height:750px"/>');

后端代码license.xml

package com.jan.base.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.multipdf.PDFMergerUtility;

import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.jan.base.web.actions.util.DownloadFileAction;
import com.jan.base.web.actions.util.UploadFileAction;

public class Word2PdfAsposeUtil
{
   private static Log log = LogFactory.getLog( Word2PdfAsposeUtil.class );
   private final static String pdfTemp = JANUtil.basePath + File.separator + "temp" + File.separator + "pdf";
   public final static String pdfPreviewTemp = JANUtil.basePath + File.separator + "temp" + File.separator + "pdfPreviewTemp";
   static
   {
      if ( JANUtil.filterEmpty( pdfTemp ) != null )
      {
         if ( new File( pdfTemp ).exists() )
         {
            new File( pdfTemp ).delete();
         }
         new File( pdfTemp ).mkdirs();
      }
   }

   public static boolean getLicense()
   {
      boolean result = false;
      try
      {
         // JANUtil.basePath + File.separator + "WEB-INF" + File.separator + "license.xml"
         InputStream is = new FileInputStream( new File( JANUtil.basePath + File.separator + "WEB-INF" + File.separator + "license.xml" ) );
         License aposeLic = new License();
         aposeLic.setLicense( is );
         result = true;
      }
      catch ( Exception e )
      {
         e.printStackTrace();
      }
      return result;
   }

   /**
    * word 转pdf并上传服务器
    * @param inPath
    * @param fileName
    * @param accountId
    * @return
    */
   public static String word2PDF( String inPath, String accountId )
   {
      FileOutputStream os = null;
      String pdfPath = null;
      String serverPdfPath = null;
      InputStream in = null;
      String tempFilePath = null;
      try
      {
         if ( !getLicense() )
         { // 验证License 若不验证则转化出的pdf文档会有水印产生  
            return null;
         }
         // 先下载临时文件夹
         tempFilePath = DownloadFileAction.download( inPath, accountId, pdfTemp + File.separator );
         long startTime = System.currentTimeMillis();
         String docName = JANUtil.getFileName( inPath ).substring( 0, JANUtil.getFileName( inPath ).lastIndexOf( "." ) );
         pdfPath = pdfTemp + File.separator + docName + ".pdf";
         File file = new File( pdfPath ); // 新建一个空白pdf文档
         os = new FileOutputStream( file );
         // 如果非linux系统去除前缀/
         if ( !File.separator.equals( "/" ) )
         {
            tempFilePath = tempFilePath.substring( 1 );
         }
         else
         {
            FontSettings.setFontsFolder( "/usr/share/fonts/winfonts", true );
         }
         Document doc = new Document( tempFilePath ); // Address是将要被转化的word文档  
         doc.save( os, SaveFormat.PDF );// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,  
                                        // EPUB, XPS, SWF 相互转换  
         long endTime = System.currentTimeMillis();
         log.info( "pdf转换成功,共耗时:" + ( ( endTime - startTime ) / 1000.0 ) + "秒" );// 转化用时

         in = new FileInputStream( new File( pdfPath ) );
         docName = docName + new Date().getTime();
         serverPdfPath = new UploadFileAction().uploadRemoteFile( in, "/pdf/" + accountId + "/" + new Date().getTime(), docName + ".pdf", accountId );
         serverPdfPath = serverPdfPath + "/" + docName + ".pdf";
      }
      catch ( Exception e )
      {
         e.printStackTrace();
      }
      finally
      {
         if ( os != null )
         {
            try
            {
               os.flush();
               os.close();
               if ( in != null )
               {
                  in.close();
               }
               JANUtil.deleteFile( pdfPath );
               JANUtil.deleteFile( tempFilePath );
            }
            catch ( IOException e )
            {
               e.printStackTrace();
            }
         }
      }
      return serverPdfPath;
   }

   /**
    * word转pdf预览
    * @param inPath
    * @param fileName <a class="media" href="" target="_blank">预览</a>
    * @param accountId
    * @return
    */
   public static void wordPreviewPDF( String inPath, String fileName, final HttpServletResponse response, String accountId )
   {
      FileOutputStream os = null;
      String pdfPath = null;
      FileInputStream fis = null;
      OutputStream out = null;
      String tempFilePath = null;
      try
      {
         if ( !getLicense() )
         { // 验证License 若不验证则转化出的pdf文档会有水印产生  
            return;
         }
         // 从服务器下载临时文件夹
         tempFilePath = DownloadFileAction.download( inPath, accountId, pdfTemp + File.separator );
         long startTime = System.currentTimeMillis();

         String name = fileName.substring( 0, fileName.lastIndexOf( "." ) );
         pdfPath = pdfTemp + File.separator + name + ".pdf";
         File file = new File( pdfPath );
         os = new FileOutputStream( file );
         // 如果非linux系统去除前缀/
         if ( !File.separator.equals( "/" ) )
         {
            tempFilePath = tempFilePath.substring( 1 );
         }
         else
         {
            FontSettings.setFontsFolder( "/usr/share/fonts/winfonts", true );
         }
         Document doc = new Document( tempFilePath ); // Address是将要被转化的word文档  
         doc.save( os, SaveFormat.PDF );// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,  
         os.flush(); // EPUB, XPS, SWF 相互转换          
         long endTime = System.currentTimeMillis();
         log.info( "pdf转换成功,共耗时:" + ( ( endTime - startTime ) / 1000.0 ) + "秒" );// 转化用时
         out = response.getOutputStream();
         response.setContentType( "application/pdf" );
         response.setCharacterEncoding( "UTF-8" );
         out.write( FileUtils.readFileToByteArray( new File( pdfPath ) ) );

         out.flush();
      }
      catch ( Exception e )
      {
         e.printStackTrace();
      }
      finally
      {
         if ( os != null )
         {
            try
            {
               if ( os != null )
               {
                  os.close();
               }
               if ( out != null )
               {
                  out.close();
               }
               if ( fis != null )
               {
                  fis.close();
               }
               if ( pdfPath != null )
               {
                  JANUtil.deleteFile( pdfPath );
               }
               if ( tempFilePath != null )
               {
                  JANUtil.deleteFile( tempFilePath );
               }
            }
            catch ( IOException e )
            {
               e.printStackTrace();
            }

         }
      }
   }

   public void downLoadPdf( final HttpServletResponse response, String pdfPreviewTemp )
   {
      try
      {
         String mergePdfFileName = new Date().getTime() + ".pdf";
         String filePath = pdfPreviewTemp + File.separator + mergePdfFileName;
         getMergePdf( mergePdfFileName );
         // 如果非linux系统去除前缀/
         if ( !File.separator.equals( "/" ) )
         {
            pdfPreviewTemp = pdfPreviewTemp.substring( 1 );
         }
         // 下载PDF到页面
         new DownloadFileAction().download( response, new FileInputStream( new File( filePath ) ), mergePdfFileName );
         // 删除临时文件
         File myFilePath = new File( pdfPreviewTemp );
         deleteFile( myFilePath );
      }
      catch ( FileNotFoundException e )
      {
         e.printStackTrace();
      }
      catch ( JANException e )
      {
         e.printStackTrace();
      }
   }

   /**
    * 删除文件夹下的所有文件
    * 
    * @param oldPath
    */
   public void deleteFile( File oldPath )
   {

      if ( oldPath.isDirectory() )
      {
         File[] files = oldPath.listFiles();
         for ( File file : files )
         {
            deleteFile( file );
         }
      }
      else
      {
         oldPath.delete();
      }
   }

   public static void getMergePdf( String mergePdfFileName )
   {
      // pdf合并工具类
      PDFMergerUtility mergePdf = new PDFMergerUtility();
      try
      {
         File myFilePath = new File( pdfPreviewTemp );
         File[] files = myFilePath.listFiles();
         for ( File file : files )
         {
            mergePdf.addSource( file.getAbsolutePath() );
         }
         // 设置合并生成pdf文件名称
         mergePdf.setDestinationFileName( pdfPreviewTemp + File.separator + mergePdfFileName );
         // 合并pdf
         mergePdf.mergeDocuments();
      }
      catch ( FileNotFoundException e )
      {
         e.printStackTrace();
      }
      catch ( IOException e )
      {
         e.printStackTrace();
      }
   }

   public static void main( String[] args )
   {
      String docPath = "D:\\1\\b\\aa.docx";
      String pdfPath = pdfTemp;
      // boolean res = Word2PdfAsposeUtil.word2PDF( docPath,"aa.docx" );
      String fileName = "aa.docx";
      String name = fileName.substring( 0, fileName.lastIndexOf( "." ) );
      //String pdfPath = pdfTemp + File.separator + name + ".pdf";
      System.out.println( pdfPath );

   }
}

license.xml去除水印

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值