Typora导出html图片转base64
Typora 导出html图片转base64
Typora
中图片使用绝对路径- 图片路径不要使用中文,否则可能会不成功
- 打包
jar
,jar
放在到出 html 同级目录下- 必须要有
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
三、cmd 执行 jar
- 到出需要本地图片转
base64
的html
文件- typora-img.jar 放到 文件同级目录下
- 执行命令
java -jar typora-img.jar
加文件绝对路径E:\makerdown\测试图片转换PDF.html
xxxxxxxxxx
java -jar typora-img.jar E:\makerdown\测试图片转换PDF.html
四、转换后结果
五、typora 配置
typora
提供导出后执行自定义命令- 左上角 文件->偏好设置->导出->html->导出后运行自定义命令
x
java -jar typora-img.jar "${outputPath}"
六、其他参数
更多参数信息查看官网 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
哇!又赚了一天人民币
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库