编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

2-LINUX HTML 转 PDF-itext5、8(2页pdf转word)

wxchong 2024-08-01 03:11:03 开源技术 12 ℃ 0 评论

itext

官网地址 The Leading PDF Library for Developers | iTextSelect a value to filter the results.QuoteQuoteQuoteQuoteQuoteQuoteQuoteQuoteQuoteQuoteiText pdf on facebookiText pdf on twitteriText pdf on youtubeiText pdf on linkediniText pdf on stackoverflow

重要说明

主要依赖

        <dependency>
            <!-- 会自动引用 itext 其他库,kernel,commons,io,forms,layout,svg,styled-xml-parser -->
            <groupId>com.itextpdf</groupId>
            <artifactId>html2pdf</artifactId>
            <version>5.0.2</version>
        </dependency>

测试代码

        // 获取 java 版本
        String version = System.getProperty("java.specification.version");

        // 获取系统类型
        String platform = System.getProperty("os.name", "");
        platform = platform.toLowerCase().contains("window") ? "win" : "linux";

        // 当前程序目录
        String current = System.getProperty("user.dir");

        System.out.println(String.format("current=%s", current));

        // html 文件路径
        File index = Paths.get(current, "..", "index.html").toFile();
        if (!index.exists()) {
            System.out.println(String.format("file not exist,file=%s", index.getAbsolutePath()));
            return;
        }

        try {
            // 保存 pdf 文件路径
            File file = Paths.get(current, String.format("java%s_%s.pdf", version, platform)).toFile();

            // 转换设置
            ConverterProperties options = new ConverterProperties();

            // 设置根目录类型
            String baseUri = Paths.get(current, "..").toUri().toString();
            options.setBaseUri(baseUri);

            // 设置字体
            FontProvider fontProvider = new FontProvider();
            fontProvider.addStandardPdfFonts();
            fontProvider.addSystemFonts();
            options.setFontProvider(fontProvider);

            // 转换 html 文件
            HtmlConverter.convertToPdf(index, file, options);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

效果预览

itext-demo/java1.8_win.pdf · yjihrp/linux-html2pdf-demo - Gitee.com

itext-demo/java11_linux.pdf · yjihrp/linux-html2pdf-demo - Gitee.com

测试结果

下一篇 3-LINUX HTML 转 OPENPDF

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表