栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

1、前端代码

$("#Id").html('');

后端代码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;
   }

   
   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;
   }

   
   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();
      }
   }

   
   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去除水印

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1008584.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号