Typora導出html圖片轉base64


Typora 導出html圖片轉base64

  1. Typora 中圖片使用絕對路徑
  2. 圖片路徑不要使用中文,否則可能會不成功
  3. 打包 jarjar放在到出 html 同級目錄下
  4. 必須要有 jdk 環境

一、實現代碼

 
 
 
xxxxxxxxxx
 
 
 
 
import java.io.*;
import java.util.Base64;
 
                 
public class Main {
    /**
     * @param src img src 內容
     * @param end 下次查找字符串起始位置
     * @return java.lang.String
     * @throws
     * @description 遞歸執行查找同一行字符串多個 img 標簽
     * @date 2021/10/1 11:07
     * @Author Mr.Fang
     */
    public static String execute(String src, int end) {
        String result = matchImg(src, end);
        System.out.println("文件路徑:-----" + result);
        if (result.isEmpty()) {
            return src;
        } else {
            String[] split = result.split(",");
            String s1 = fileToBase64(split[0]);
            if (s1.isEmpty()) {
                return src;
            } else {
                String replace = src.replace(split[0], s1);
                return execute(replace, Integer.valueOf(split[1]) + 20);
            }
        }
    }
 
                 
    /**
     * @param str 原始字符串
     * @return java.lang.String
     * @Description 匹配 img src 內容
     * @date 2021/9/30 0030 16:32
     * @auther Mr.Fang
     **/
    public static String matchImg(String str, int start) {
        int img = str.indexOf("<img", start); // 起始位置
        if (img == -1) {
            return "";
        }
        int l = str.indexOf("\"", img) + 1; // src 左側 雙引號
        int r = str.indexOf("\"", l); // src 右側 雙引號
        String substring = str.substring(l, r);
        if (substring.startsWith("data")) { // 跳過已經 base64 編碼的文件 和 http 地址
            return matchImg(str, r);
        }
        return substring + "," + r; // src 地址 返回 src 內容以及最后的位置 使用逗號拼接
    }
 
                 
    /**
     * @param path 文件路徑
     * @return java.lang.String
     * @Description 文件轉 base64
     * @date 2021/9/30 0030 16:37
     * @auther Mr.Fang
     **/
    public static String fileToBase64(String path) {
        File file = new File(path);
        if (!file.exists()) {
            System.err.printf("文件不存在");
            return "";
        }
        byte bytes[] = null;
        try (FileInputStream fileInputStream = new FileInputStream(path);) {
            bytes = new byte[fileInputStream.available()];
            fileInputStream.read(bytes);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("圖片轉 base64 失敗");
        }
        // 文件后綴處理
        String suffix = getSuffix(path);
        return "data:image/" + suffix + ";base64," + Base64.getEncoder().encodeToString(bytes);
    }
 
                 
    /**
     * @param str
     * @return java.lang.String
     * @throws
     * @description 獲取文件后綴
     * @date 2021/10/1 16:43
     * @Author Mr.Fang
     */
    public static String getSuffix(String str) {
        return str.substring(str.lastIndexOf(".") + 1);
    }
 
                 
 
                 
    // 主方法
    public static void main(String[] args) {
        // 獲取文件路徑
        if (args.length == 0) {
            System.out.println("No parameters passed");
            return;
        }
        String arg = args[0];
        // 獲取文件后綴
        String suffix = getSuffix(arg);
        try (BufferedReader bfr = new BufferedReader(new FileReader(arg));
             BufferedWriter bfw = new BufferedWriter(new FileWriter(arg.concat("-bast64.").concat(suffix)))
        ) {
            String len = "";
            while ((len = bfr.readLine()) != null) {
                String result = "";
                if (len.indexOf("<img") != -1) {
                    result = execute(len, 0);
                }
                if (result.equals("")) {
                    bfw.write(len);
                } else {
                    bfw.write(result);
                }
            }
 
                 
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("error");
        }
        System.out.println("success");
 
                 
    }
}
 
                
 

二、打包 jar

image-20211001212617042

image-20211001212641401

image-20211001212738195

image-20211001212755676

image-20211001212819356

image-20211001212850523

image-20211001212859089

image-20211001212952433

image-20211001213017526

image-20211001213036399

image-20211001213058578

三、cmd 執行 jar

  1. 到出需要本地圖片轉base64html 文件
  2. typora-img.jar 放到 文件同級目錄下
  3. 執行命令 java -jar typora-img.jar 加文件絕對路徑 E:\makerdown\測試圖片轉換PDF.html
 
 
 
xxxxxxxxxx
 
 
 
 
java -jar typora-img.jar E:\makerdown\測試圖片轉換PDF.html
 

image-20211001213814130

四、轉換后結果

image-20211001213931319

五、typora 配置

  1. typora提供導出后執行自定義命令
  2. 左上角 文件->偏好設置->導出->html->導出后運行自定義命令
 
 
 
x
 
 
 
 
java -jar typora-img.jar "${outputPath}"
 

image-20211001214701770

六、其他參數

更多參數信息查看官網 https://support.typora.io/Export/

您可以在自定義頁眉/頁腳文本和自定義導出命令中使用 ${variables},它們的值是:

Key Value
${outputPath} Output file path after export. For example, if you export to location /User/aaa/Documents/test.pdf, then ${outputPath} will be replaced to that path.
${outputFileName} File name (without extension) of the saved exported file. It will be test in above case.
${outputFileFullName} File name (with extension) of the saved exported file. It will be test.md in above case.
${currentPath} Path of currently edited file. For example, if you are editing /User/aaa/Document/readme.md, then the value will be /User/aaa/Document/readme.md.
${currentFileName} Filename without extension of currently edited file. It will be readme in above case.
${currentFileFullName} Filename with extension of currently edited file. It will be readme.md in above case.
${today} Current date, for example: 2020-01-19
${pageNo} Current page number. Only available for PDF format.
${pageCount} / ${totalPages} Total page counts. Only available for PDF format.
${title} Article title, should be defined in YAML Front Matter.
${author} Article author, defined in export options for PDF format, can be overwritten in YAML Front Matter.
${a.b} If a is an object defined in YAML Front Matter which contains b, then you can use a.b to access value for b.
Other variables You can use keyword: value in YAML Front Matter, then uses ${keyword} variables in export configs.

其他

typora 官網 https://www.typora.io/

jar下載地址 https://864000.lanzoui.com/iRX5suqya6b

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM