天天看點

文檔線上預覽(二)将word、pdf檔案轉html實作文檔線上預覽

作者:知北遊zzz

實作文檔線上預覽的方式除了上篇文章《文檔線上預覽(一)通過将txt、word、pdf轉成圖檔實作線上預覽功能》說的将文檔轉成圖檔的實作方式外,還有轉成pdf,前端通過pdf.js、pdfobject.js等插件來實作線上預覽,以及本文将要說到的将文檔轉成html的方式來實作線上預覽。代碼基于 aspose-words(用于word轉html),pdfbox(用于pdf轉html),是以事先需要在項目裡下面兩個依賴:

<dependency>    
    <groupId>com.luhuiguo</groupId>    
    <artifactId>aspose-words</artifactId>    
    <version>23.1</version></dependency>
<dependency>    
    <groupId>org.apache.pdfbox</groupId>    
    <artifactId>pdfbox</artifactId>    
    <version>2.0.4</version>
</dependency>
           

一、将檔案轉換成html字元串

1、将word檔案轉成html字元串

public static String wordToHtmlStr(String wordPath) {
        try {
            Document doc = new Document(wordPath); // Address是将要被轉化的word文檔
            String htmlStr = doc.toString();
            return htmlStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
           

驗證結果:

文檔線上預覽(二)将word、pdf檔案轉html實作文檔線上預覽

2、将pdf檔案轉成html字元串

public static String pdfToHtmlStr(String pdfPath) throws IOException, ParserConfigurationException {
        PDDocument document = PDDocument.load(new File(pdfPath));
        Writer writer = new StringWriter();
        new PDFDomTree().writeText(document, writer);
        writer.close();
        document.close();
        return writer.toString();
    }
           

驗證結果:

文檔線上預覽(二)将word、pdf檔案轉html實作文檔線上預覽

二、将檔案轉換成html,并生成html檔案

有時我們是需要的不僅僅傳回html字元串,而是需要生成一個html檔案這時應該怎麼做呢?一個改動量小的做法就是使用org.apache.commons.io包下的FileUtils工具類寫入目标位址:

FileUtils類将html字元串生成html檔案示例:

首先需要引入pom:

<dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.8.0</version>
        </dependency>
           

相關代碼:

String htmlStr = FileConvertUtil.pdfToHtmlStr("D:\\書籍\\電子書\\小說\\曆史小說\\最後的可汗.doc");
FileUtils.write(new File("D:\\test\\doc.html"), htmlStr, "utf-8");
           

除此之外,還可以對上面的代碼進行一些調整,已實作生成html檔案,代碼調整如下:

1、将word檔案轉換成html檔案

public static void wordToHtml(String wordPath, String htmlPath) {
        try {
            File sourceFile = new File(wordPath);
            String path = htmlPath + File.separator + sourceFile.getName().substring(0, sourceFile.getName().lastIndexOf(".")) + ".html";
            File file = new File(path); // 建立一個空白pdf文檔
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(wordPath); // Address是将要被轉化的word文檔
            HtmlSaveOptions options = new HtmlSaveOptions();
            options.setExportImagesAsBase64(true);
            options.setExportRelativeFontSize(true);
            doc.save(os, options);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
           

驗證結果:

文檔線上預覽(二)将word、pdf檔案轉html實作文檔線上預覽

2、将pdf檔案轉換成html檔案

public static void pdfToHtml(String pdfPath, String htmlPath) throws IOException, ParserConfigurationException {
        File file = new File(pdfPath);
        String path = htmlPath + File.separator + file.getName().substring(0, file.getName().lastIndexOf(".")) + ".html";
        PDDocument document = PDDocument.load(new File(pdfPath));
        Writer writer = new PrintWriter(path, "UTF-8");
        new PDFDomTree().writeText(document, writer);
        writer.close();
        document.close();
    }
           

圖檔版PDF檔案驗證結果:

文檔線上預覽(二)将word、pdf檔案轉html實作文檔線上預覽

文字版PDF檔案驗證結果:

文檔線上預覽(二)将word、pdf檔案轉html實作文檔線上預覽