当前位置: 首页 > news >正文

markdown文件转pdf

步骤:md转html转pdf
pom引入
 <!--markdown 转pdf--><dependency><groupId>com.vladsch.flexmark</groupId><artifactId>flexmark-all</artifactId><version>0.64.8</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>html2pdf</artifactId><version>5.0.4</version></dependency><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.9.2</version></dependency>
java代码

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.layout.font.FontInfo;
import com.itextpdf.layout.font.FontProvider;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.MutableDataSet;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Document.OutputSettings;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;public class MarkdownToPdf {public static void main(String[] args) {String markdownFile = "D:\\markdown\\test.md";String pdfFile = "D:\\markdown\\test_" + System.currentTimeMillis() + ".pdf";toPdf(markdownFile, pdfFile);}private static void toPdf(String markdownFile, String pdfFile) {try {// 读取Markdown文件String markdown = new String(Files.readAllBytes(Paths.get(markdownFile)), StandardCharsets.UTF_8);// 转换Markdown为HTMLMutableDataSet options = new MutableDataSet();Parser parser = Parser.builder(options).build();HtmlRenderer renderer = HtmlRenderer.builder(options).build();Node document = parser.parse(markdown);String html = renderer.render(document);// 使用JSoup解析HTML并转换为XHTMLDocument xhtmlDoc = Jsoup.parse(html);xhtmlDoc.outputSettings(new OutputSettings().syntax(OutputSettings.Syntax.xml));// 调整图片大小,不然图片高度显示不全xhtmlDoc.select("img").forEach(img -> {// 设置图片宽度和高度自适应img.attr("style", "max-width: 100%; height: auto;");});// 定义默认字体ConverterProperties converterProperties = new ConverterProperties();FontProvider fontProvider = new DefaultFontProvider(true, false, false);// 写法1// 宋体// URL url = new MarkdownToPdf().getClass().getClassLoader().getResource("font/SimSun.ttf");// 无衬线// URL url = new MarkdownToPdf().getClass().getClassLoader().getResource("font/NotoSansCJKsc-Regular.otf");// 有衬线// URL url = new MarkdownToPdf().getClass().getClassLoader().getResource("font/NotoSerifCJKsc-Regular.otf");// URL url = new MarkdownToPdf().getClass().getClassLoader().getResource("font/NotoSerifCJKsc-Light.otf");// fontProvider.addFont(url.getPath(), PdfEncodings.IDENTITY_H);// 写法2Resource resource = new ClassPathResource("font/SimSun.ttf");fontProvider.addFont(resource.getInputStream().readAllBytes(), PdfEncodings.IDENTITY_H);converterProperties.setFontProvider(fontProvider);File file = new File(pdfFile);FileOutputStream outputStream;try {outputStream = new FileOutputStream(file);} catch (FileNotFoundException e) {throw new RuntimeException(e);}// Html 转化为 pdfHtmlConverter.convertToPdf(xhtmlDoc.html(), outputStream, converterProperties);System.out.println("PDF创建成功!");} catch (IOException e) {System.err.println("文件读取/写入错误: " + e.getMessage());e.printStackTrace();}}}
字体文件(见资源文件,可下载)

放在resources文件夹下即可

http://www.lryc.cn/news/395498.html

相关文章:

  • 课设:二手车交易管理系统(Java+MySQL)
  • vue3实现无缝滚动 列表滚动 vue3-seamlessscroll
  • Python酷库之旅-第三方库Pandas(012)
  • SpringCloud集成nacos之jasypt配置中心的密码加密的自动解密
  • Python 中将字典内容保存到 Excel 文件使用详解
  • libaom 编码器 aomenc 使用文档介绍
  • 速盾:cdn 缓存图片
  • 移动应用开发课设——原神小助手文档(2)
  • 智能聊天机器人:使用PyTorch构建多轮对话系统
  • 昇思25天学习打卡营第16天 | 文本解码原理-以MindNLP为例
  • Unity之Text组件换行\n没有实现+动态中英互换
  • vue3+ el-tree 展开和折叠,默认展开第一项
  • ProFormList --复杂数据联动ProFormDependency
  • Git、Github、tortoiseGit下载安装调试全套教程
  • 老师怎么快速发布成绩?
  • 央视揭露:上百元的AI填报高考志愿真的靠谱吗?阿里云新增两位AI圈“代言人”!|AI日报
  • TPM管理咨询公司甄选指南
  • 探索 Scikit-Learn:机器学习的强大工具库
  • 音视频质量评判标准
  • 如何在vue3中使用scss
  • Gartner发布采用美国防部模型实施零信任的方法指南:七大支柱落地方法
  • Flutter——最详细(Badge)使用教程
  • SQLServer的系统数据库用别的服务器上的系统数据库替换后做跨服务器连接时出现凭证、非对称金钥或私密金钥的资料无效
  • vue前端面试
  • 【网络安全】Host碰撞漏洞原理+工具+脚本
  • unattended-upgrade进程介绍
  • SpringBoot 中多例模式的神秘世界:用法区别以及应用场景,最后的灵魂拷问会吗?- 第519篇
  • 基于STM32设计的智能婴儿床(ESP8266局域网)_2024升级版_180
  • C++(第四天----拷贝函数、类的组合、类的继承)
  • 第一课:接口配置IP地址:DHCP模式