利用openoffice+jodconverter-code-3.0-bate4 把ppt轉圖片

安裝openoffice4  (用於把文檔(ppt)轉成pdf)根據系統的位數安裝
使用jodconverter-core3.0-beta-4(要上傳maven本地倉庫)
安裝ImageMagick:yum install ImageMagick(用於pdf轉圖片)
安裝pdftotext  用於提取文字大綱  yum install poppler-utils
perl腳本(用於提取pdf文檔的文字大綱)

使用jodconverter調用OpenOffice 將office文檔轉換爲PDF時。如果轉換程序異常中止而OpenOffice並沒有停止運行的話。

openoffice   

1、啓動tomcat時,啓動openoffice服務(個人感覺有風險問題)

2、手工用命令,啓動openoffice服務,在使用鏈接服務(推薦)

package com.document.servers.impl;

import java.io.File;
import java.net.ConnectException;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.ExternalOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeConnectionProtocol;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;



//import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
//import com.artofsolving.jodconverter.DocumentConverter;
//import com.artofsolving.jodconverter.DocumentFamily;
//import com.artofsolving.jodconverter.DocumentFormat;
//import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
//import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
//import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.document.servers.OfficeService;

/**
 *  linux下:
 *  cd  /opt/openoffice4/program 
 * ./soffice "-accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" -nologo -headless -nofirststartwizard &
 */
@Service("officeService")
public class OfficeServiceImpl implements OfficeService {

    private static final Logger logger = LoggerFactory.getLogger(OfficeServiceImpl.class);

    private OfficeManager officeManager;
    private OfficeDocumentConverter documentConverter;

//    @PostConstruct
//    public void init() throws Exception {
//        // TODO Auto-generated method stub
//        officeManager = new DefaultOfficeManagerConfiguration().setOfficeHome("/opt/openoffice4").buildOfficeManager();
//
//        documentConverter = new OfficeDocumentConverter(officeManager);
//        // officeManager.stop();
//
//        logger.warn("openoffice starting....");
//        try {
//            officeManager.start();
//            logger.warn("openoffice started");
//        } catch (Exception e) {
//            logger.error("office start failed:{}", e);
//        }
//    }
//
//    @PreDestroy
//    public void destroy() throws Exception {
//        // TODO Auto-generated method stub
//        logger.info("shutdown office service....");
//        if (officeManager != null) {
//            try {
//                
//                officeManager.stop();
//                logger.info("office closed");
//            } catch (Exception e) {
//                logger.error("office close failed:{}", e);
//            }
//        }
//    }

//    public void convert(String inputfilename, String outputfilename) {
//        logger.info("convert...." + inputfilename + " to " + outputfilename);
//        documentConverter.convert(new File(inputfilename), new File(outputfilename));
//    }
    
    
    public void manualConvert(String inputfilename, String outputfilename) {
        logger.info("convert...." + inputfilename + " to " + outputfilename);
        // connect to an OpenOffice.org instance running on port 8100 
        ExternalOfficeManagerConfiguration externalProcessOfficeManager = new  
                ExternalOfficeManagerConfiguration();  
        externalProcessOfficeManager.setConnectOnStart(true);  
        externalProcessOfficeManager.setPortNumber(8100);  
        officeManager = externalProcessOfficeManager.buildOfficeManager();  
        officeManager.start(); 
        logger.info("openoffice服務已鏈接");
        documentConverter = new OfficeDocumentConverter(officeManager);
        documentConverter.convert(new File(inputfilename), new File(outputfilename));
    }
    
 

}


轉換處理方法

package com.document.servers.impl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.document.defined.model.ImagePPT;
import com.document.servers.OfficeService;
import com.document.servers.PPTConvertServers;
import com.document.tool.ImageMagickUtils;
import com.document.tool.SystemConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ppt.util.Command;

@Service("pPTConvertServers")
public class PPTConvertServersImpl implements PPTConvertServers {

    private static final Logger logger = LoggerFactory.getLogger(PPTConvertServersImpl.class);

    @Autowired
    private OfficeService officeService;

    /**
     * (non-Javadoc)
     * 利用openoffice把ppt轉圖片
     */
    public Map<String, Object> deal_ppt(HttpServletRequest request, String filePath, String extension, String title, String filename)
            throws Exception {

        logger.info("ppt轉pdf,{}");

        // ppt文件地址
        String ppt_target_file = filePath;

        // pdf文件地址
        String path = filePath.substring(0, filePath.lastIndexOf("."));
        String pdf_target_file = path + ".pdf";
        // 輸出jpg文件地址
        String images_target_file = path + "/jpg" + "-%d.jpg";

        // if(exists(pdf_target_file)){
        // unlink(pdf_target_file);//刪除
        // }
        // copy(new File(ppt_target_file), ppt_target_file, true);

        if (!extension.equals(".pdf")) {
            officeService.manualConvert(ppt_target_file, pdf_target_file);// 轉成pdf文件
        }

        StringWriter writer = new StringWriter();

        // 提取文字大綱
        String[] pdf_lines = extractOutLineFromPDF(pdf_target_file);

        File filepath = new File(images_target_file);
        File parentFile = filepath.getParentFile();
        if (!parentFile.exists()) {
            logger.info("創建圖片目錄");
            parentFile.mkdirs();
        }

        Command.run("convert " + pdf_target_file + " " + images_target_file, writer);// 轉成圖片

        String basePath = request.getScheme() + "://" + request.getServerName() + "/";
        PDDocument document = PDDocument.load(new File(pdf_target_file));
        int pageCount = document.getNumberOfPages();
        document.close();

        List<ImagePPT> list = new ArrayList<ImagePPT>();
        String pathUrl = filename.substring(0, filename.lastIndexOf("."));
        if (pageCount > 0) {
            for (int i = 0; i < pageCount; i++) {
                ImagePPT imagePPT = new ImagePPT();
                imagePPT.setId(i + 1);
                if (pdf_lines.length > 0) {
                    try {
                        imagePPT.setTitle(pdf_lines[i]);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        imagePPT.setTitle(title);
                        logger.info("title,數組越界");
                        //e.printStackTrace();
                    }
                } else {
                    imagePPT.setTitle(title);
                }
                imagePPT.setUrl(basePath + "images/" + pathUrl + "/jpg-" + i + ".jpg");
                imagePPT.setPreviewUrl(basePath + "preview/images/" + pathUrl + "/preview/pjpg-" + i + ".jpg");

//                String oimgDir = SystemConfig.getBlobDirectory() + pathUrl + "/jpg-" + i + ".jpg";
//                String pimgDir = SystemConfig.getBlobDirectory() + pathUrl + "/preview/pjpg-" + i + ".jpg";
//                File pfilepath = new File(pimgDir);
//                File pf = pfilepath.getParentFile();
//                if (!pf.exists()) {
//                    pf.mkdirs();
//                }
                //ImageMagickUtils.scale(oimgDir, pimgDir, 240, 180);//預覽圖
                list.add(imagePPT);
            }
        }
        // 拼接json字符串

        ObjectMapper objectMapper = new ObjectMapper();
        String jsonlist = objectMapper.writeValueAsString(list);
        // logger.info(jsonlist);
        Map<String, Object> map=new HashMap<String, Object>();
        map.put("json", jsonlist.toString());
        map.put("totalPage", pageCount);
        return map;

    }

    /**
     * 文件已經上傳過 ,進行替換性轉換
     */
    public Map<String, Object> replace_ppt(HttpServletRequest request, String filePath, String extension, String title,
            String filename) throws Exception {

        logger.info("替換,ppt轉pdf,{}");

        // ppt文件地址
        String ppt_target_file = filePath;

        // pdf文件地址
        String path = filePath.substring(0, filePath.lastIndexOf("."));
        String pdf_target_file = path + ".pdf";
        // 輸出jpg文件地址
        String images_target_file = path + "/jpg" + "-%d.jpg";

        if (!extension.equals(".pdf")) {
            officeService.manualConvert(ppt_target_file, pdf_target_file);// 轉成pdf文件
        }

        StringWriter writer = new StringWriter();

        // 提取文字大綱
        String[] pdf_lines = extractOutLineFromPDF(pdf_target_file);

        File filepath = new File(images_target_file);
        File parentFile = filepath.getParentFile();
        if (!parentFile.exists()) {
            logger.info("替換創建圖片目錄");
            parentFile.mkdirs();
        }
        Command.run("convert " + pdf_target_file + " " + images_target_file, writer);// 轉成圖片

        String basePath = request.getScheme() + "://" + request.getServerName() + "/";
        PDDocument document = PDDocument.load(new File(pdf_target_file));
        int pageCount = document.getNumberOfPages();
        document.close();

        List<ImagePPT> list = new ArrayList<ImagePPT>();
        String pathUrl = filename.substring(0, filename.lastIndexOf("."));
        if (pageCount > 0) {
            for (int i = 0; i < pageCount; i++) {
                ImagePPT imagePPT = new ImagePPT();
                imagePPT.setId(i + 1);
                if (pdf_lines.length > 0) {
                    try {
                        imagePPT.setTitle(pdf_lines[i]);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        imagePPT.setTitle(title);
                        logger.info("title,數組越界");
                        // e.printStackTrace();
                    }
                } else {
                    imagePPT.setTitle(title);
                }
                imagePPT.setUrl(basePath + "images/" + pathUrl + "/jpg-" + i + ".jpg");
                imagePPT.setPreviewUrl(basePath + "preview/images/" + pathUrl + "/preview/pjpg-" + i + ".jpg");

//                String oimgDir = SystemConfig.getBlobDirectory() + pathUrl + "/jpg-" + i + ".jpg";
//                String pimgDir = SystemConfig.getBlobDirectory() + pathUrl + "/preview/pjpg-" + i + ".jpg";
//                File pfilepath = new File(pimgDir);
//                File pf = pfilepath.getParentFile();
//                if (!pf.exists()) {
//                    pf.mkdirs();
//                }
//                ImageMagickUtils.scale(oimgDir, pimgDir, 240, 180);
                list.add(imagePPT);
            }
        }
        // 拼接json字符串

        ObjectMapper objectMapper = new ObjectMapper();
        String jsonlist = objectMapper.writeValueAsString(list);
        // logger.info(jsonlist);
        Map<String, Object> map=new HashMap<String, Object>();
        map.put("json", jsonlist.toString());
        map.put("totalPage", pageCount);
        return map;

    }

    /**
     * 提取pdf文字大綱
     * @param pdf_file
     * @return
     * @throws UnsupportedEncodingException
     * @throws Exception
     */
    public static String[] extractOutLineFromPDF(String pdf_file) throws UnsupportedEncodingException {
        String svndir = PPTConvertServersImpl.class.getResource("").getPath();
        svndir = svndir.split("WEB-INF")[0];
        svndir = svndir.replaceFirst("file:", "");
        logger.info(svndir);

        String command = "/usr/bin/perl " + svndir + "WEB-INF/sh/pdf_outline.pl " + pdf_file;
        logger.info(command);

        ByteArrayOutputStream writer = new ByteArrayOutputStream();

        Command.run2(command, writer);
        String outline = writer.toString("utf-8");
        logger.info("title pdf,{}", outline);
        String[] items = outline.split("///");

        return items;
    }

    /**
     * 文件是否存在
     * 
     * @param filename
     * @return @throws IOException
     */
    public static boolean exists(String filename) {
        try {
            File file = new File(filename);
            return file.exists();
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 刪除文件
     * 
     * @param filename
     * @return
     */
    public static boolean unlink(String filename) {
        try {
            File file = new File(filename);
            if (file.isFile()) {
                file.delete();
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 拷貝文件
     * 
     * @param file
     * @param newname
     * @param overwrite
     * @return
     */
    public static boolean copy(File file, String newname, boolean overwrite) {
        try {
            if (!overwrite && new File(newname).exists()) {
                return false;
            }
            FileInputStream input = new FileInputStream(file);
            File dest = new File(newname);
            if (!mkdir(dest.getParent())) {
                return false;
            }
            FileOutputStream output = new FileOutputStream(newname);
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = input.read(b)) != -1) {
                output.write(b, 0, len);
            }
            output.flush();
            output.close();
            input.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 創建目錄
     * 
     * @param dirname
     * @return
     */
    public static boolean mkdir(String dir) {
        try {
            File file = new File(dir);
            if (!file.exists()) {
                file.mkdirs();
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

上傳ppt文件處理類:

package com.document.handle.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.document.servers.PPTConvertServers;
import com.document.tool.FilenameUtils;
import com.document.tool.SystemConfig;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

@Controller
public class PptToImageController {

    private static final Logger logger = LoggerFactory.getLogger(PptToImageController.class);

    private static final String TYPE_BLOB = "BLOB";

    private static final String CALLBACK = "callback"; // 回調函數的參數名

    @Autowired
    private PPTConvertServers pPTConvertServers;

    @RequestMapping(value = "/convert/upload")
    public ModelAndView updateFile(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("name", "Hello Word");
        mav.setViewName("/ppt/uploadFile");
        logger.info("/convert/upload");
        return mav;
    }

    /**
     * 顯示上傳文件的頁面表單。
     */
    @SuppressWarnings("unchecked")
    private ModelAndView showUploadForm(HttpServletRequest request, String type) {
        // 所有請求參數
        Map<String, String> params = new HashMap<String, String>();
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String name = paramNames.nextElement();
            String value = request.getParameter(name);
            if (null != value) {
                params.put(name, value);
            }
        }

        ModelAndView mav = new ModelAndView();
        mav.setViewName("/upload/" + type.toLowerCase());
        mav.addObject("parameters", params);
        return mav;
    }

    /**
     * 保存用戶上傳的文件。
     * @throws UnsupportedEncodingException
     */
    private Map<String, Object> saveUploadedFile(HttpServletRequest request, MultipartFile file, String type) {

        // 文件內容MD5串,避免文件重複上傳
        String md5 = null;
        try {
            md5 = DigestUtils.md5Hex(file.getBytes());
            logger.info("文件內容MD5串,{}", md5);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        String originalFilename = file.getOriginalFilename();

        String extension = FilenameUtils.getExtension(originalFilename); // 文件擴展名

        String filename = null;
        if (md5 != null) {
            filename = FilenameUtils.generateFileNameMd5(extension, md5);
        } else {
            filename = FilenameUtils.generateFileName(extension);
        }
        

        
        String filenameUrl = null; // 文件訪問的URL
        String absoluteFilename = null; // 文件存儲的絕對路徑

        filenameUrl = SystemConfig.getBlobUrl() + filename;
        absoluteFilename = SystemConfig.getBlobDirectory() + filename;

        // 檢查是否需要創建目錄
        File filepath = new File(absoluteFilename);
        File parentFile = filepath.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        
        Map<String, Object> params = new HashMap<String, Object>();
        // 所有請求參數
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String name = paramNames.nextElement();
            String value = request.getParameter(name);
            if (null != value) {
                params.put(name, value);
            }
        }
        
        String pdftitle = originalFilename.substring(0, originalFilename.lastIndexOf("."));
        params.put("title", pdftitle);
        Map<String, Object> officeMap=new HashMap<String, Object>();
        if (filepath.exists()) {
            // 文件已上傳過,文件進行替換
            try {
                officeMap = pPTConvertServers.replace_ppt(request, absoluteFilename, extension, pdftitle, filename);
                params.put("totalPage", officeMap.get("totalPage"));
                params.put("data", officeMap.get("json"));
                params.put("status", "success");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                logger.info("把ppt文件轉pdf失敗,{}", e);
                params.put("status", "fail");
                params.put("data", "把ppt文件轉pdf失敗");
                params.put("totalPage", 0);
                e.printStackTrace();
            }
           
            return params;
        }
        
        
        // 保存文件
        BufferedOutputStream bos = null;
        try {
            byte[] fileBytes = file.getBytes();
            bos = new BufferedOutputStream(new FileOutputStream(filepath));
            bos.write(fileBytes);
        } catch (IOException e) {
            logger.error("保存'" + originalFilename + "'時發生異常,Cause: ", e);
        } finally {
            if (null != bos) {
                try {
                    bos.close();
                } catch (IOException e) {
                }
            }
        }

        // params.put("url", filenameUrl);
        // params.put("originalFilename", originalFilename);
        // params.put("filesize", file.getSize());

        // 把ppt文件轉pdf,pdf轉圖片
        try {
            officeMap = pPTConvertServers.deal_ppt(request, absoluteFilename, extension, pdftitle, filename);
            params.put("totalPage", officeMap.get("totalPage"));
            params.put("data", officeMap.get("json"));
            params.put("status", "success");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.info("把ppt文件轉pdf失敗,{}", e);
            params.put("status", "fail");
            params.put("data", "把ppt文件轉pdf失敗");
            params.put("totalPage", 0);
            e.printStackTrace();
        }

        return params;
    }

    /**
     * 處理文件上傳。
     * @throws IOException
     * 
     */
    @RequestMapping(value = "/convert/upload", method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    public @ResponseBody String uploadFilePost(HttpServletRequest request,
            @RequestParam("file") MultipartFile file) throws IOException {

        String callback = request.getParameter(CALLBACK); // 回調函數的函數名
        String json = "請上傳文件";
        Map<String, Object> params = new HashMap<String, Object>();

        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
        ObjectWriter writer = mapper.writerWithType(Map.class);

        if (!file.isEmpty()) {
            params = saveUploadedFile(request, file, TYPE_BLOB);
            if (params == null) {
                params = new HashMap<String, Object>();
                json = "文件已上傳過";
                params.put("status", "fail");
                params.put("data", json);
                json = writer.writeValueAsString(params);
                return json.toString();
            }

            try {
                json = writer.writeValueAsString(params);
                // json = (String) params.get("data");
            } catch (Exception e) {
                logger.error("轉換Blob上傳參數爲JSON時發生異常,Cause: ", e);
            }

            if (StringUtils.isBlank(callback)) {
                return json.toString();
            } else {
                return callback + "(" + json.toString() + ");";
            }

        }

        // 還沒上傳文件的
        params.put("status", "fail");
        params.put("data", json);
        json = writer.writeValueAsString(params);
        return json.toString();
    }

}

預覽圖代理輸出-----處理類:

package com.document.handle.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.document.tool.ImageMagickUtils;
import com.document.tool.SystemConfig;



@Controller
public class ImageAgentController {

    private static final Logger LOG = LoggerFactory.getLogger(ImageAgentController.class);
    
    /**
     * ppt預覽圖片代理輸出
     * @throws IOException 
     */
    @RequestMapping("/preview/images/{year}/{month}/{md5id}/{preview}/{filename}.{ext}")
    public void cropImage(@PathVariable String year, @PathVariable String month, @PathVariable String md5id,@PathVariable String preview, @PathVariable String filename, @PathVariable String ext, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //String rootDir = "/data05/ovp/images/";
        String rootDir = SystemConfig.getBlobDirectory();
        String oname = filename.substring(1,filename.length());//原圖文件名
        String dirString = rootDir + year+"/" +month + "/" + md5id + "/"+oname+"." + ext;
        String targetFileString = rootDir + year+"/" +month + "/" + md5id + "/preview/" + filename + "." + ext;
        
        LOG.info("corpImage..." + dirString + " -> " +targetFileString );
        File newfile = new File(targetFileString);
        String pathString = newfile.getParent();
        LOG.info("pathString...{} {}" , pathString);
        File pathFile = new File(pathString);
        if(!pathFile.exists()){
            LOG.info("---create file---");
            pathFile.mkdirs();
        }
        boolean status = ImageMagickUtils.scale(dirString, targetFileString, 240, 180);
        if(status){
            response.reset(); 
            response.setContentType("image/" + ext);
            
            java.io.InputStream in = new java.io.FileInputStream(targetFileString);
            //FilenameUrlUtils.getImageFilename(targetFileString);
            
            if ( in != null )
            {
                byte[] b = new byte[1024]; 
                int len;
                while( (len = in.read(b)) != -1 )
                {
                    response.getOutputStream().write(b);     
                }
            
                in.close(); 
            }
        }
    }
    

}

提取文字大綱的perl腳本:

use strict;
use warnings;
use utf8;
use open ':encoding(utf8)';
binmode(STDOUT, ":utf8");
sub trim($)
{
        my $string = shift;
        $string =~ s/^\s+//;
        $string =~ s/\s+$//;
        return $string;
}
if(!$ARGV[0]){
	die;
}
open my $fh, "pdftotext -layout -enc UTF-8 $ARGV[0] - |" or die $!;
my $firstline=<$fh>;
print trim($firstline);
my $pageNum = 1;

while ( my $line = <$fh> ) {
    if ( $line =~ /\xC/ ) {
        my $count = ($line =~ tr/\xC//);
        for(my $i=0;$i<$count-1;$i++){
                print "///".$pageNum;
                $pageNum++;
        }
        if(trim($line)){
                print "///".trim($line);
        }
        $pageNum++;
    }

}

close $fh;


可能遇到的問題:

1、ppt轉pdf時,遇到啓動失敗(不清楚是不是再次啓動引起的)

2、轉換後的pdf 表格裏的中文會出現亂碼

3、有時會出現關閉服務器的所用服務(尚不清楚什麼原因引起的)

4、處理請求時,經常出現超時504




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章