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},它们的值是:

KeyValue
${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

 

 

 

 
posted @ 2021-10-01 21:53  天葬  阅读(913)  评论(0编辑  收藏  举报