java pdf方案

1、htmltopdf

XML 复制代码
<dependency>
	<groupId>io.woo</groupId>
	<artifactId>htmltopdf</artifactId>
	<version>1.0.8</version>
</dependency>
java 复制代码
package org.lyy.security.demo;

import java.io.InputStream;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import io.woo.htmltopdf.HtmlToPdf;
import io.woo.htmltopdf.HtmlToPdfObject;
import io.woo.htmltopdf.PdfPageSize;

public class PdfDemo {

    public static void main(String[] args) {
        htmlToPdf();
        System.out.println("===========done=========");
    }

    private static void htmlToPdf() {
        InputStream stream = PdfDemo.class.getClassLoader().getResourceAsStream("index.html");
        String htmlStr = IoUtil.read(stream, CharsetUtil.CHARSET_UTF_8);
        HtmlToPdf.create()
                .pageSize(PdfPageSize.A3)
                .object(HtmlToPdfObject.forHtml(htmlStr).defaultEncoding(CharsetUtil.UTF_8))
                .object(HtmlToPdfObject.forHtml("<h1 style=\"color:red;\">sssssssss</h1>").defaultEncoding(CharsetUtil.UTF_8))
                .convert("C:\\siefile\\bak11\\v4\\PdfDemo.pdf");
    }
}

2、openpdf

XML 复制代码
<dependency>
	<groupId>com.github.librepdf</groupId>
	<artifactId>openpdf</artifactId>
	<version>1.3.35</version>
</dependency>
java 复制代码
package org.lyy.security;

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.draw.DottedLineSeparator;

import lombok.extern.slf4j.Slf4j;

import static com.lowagie.text.Font.BOLD;

@Slf4j
public class PdfUtil {
    public static BaseFont MSYH;
    public static BaseFont MSYHL;
    public static Font MSYHL_TEXT_BOLD;
    public static Font MSYHL_TEXT;

    static {
        try {
            MSYH = BaseFont.createFont("fonts/msyh.ttc,0", BaseFont.IDENTITY_H, true);
            MSYHL = BaseFont.createFont("fonts/msyhl.ttc,0", BaseFont.IDENTITY_H, true);
            MSYHL_TEXT_BOLD = new Font(MSYHL, 10f, BOLD);
            MSYHL_TEXT = new Font(MSYHL, 10f);
        } catch (IOException ex) {
            log.info("font not find", ex);
        }
    }

    public static void main(String[] args) throws Exception {
        String pdfFile = "C:\\siefile\\bak11\\v3\\demo.pdf";
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(pdfFile));

        HeaderFooter footer = new HeaderFooter(true, new Phrase(" page"));
        footer.setAlignment(Element.ALIGN_CENTER);
        footer.setBorder(Rectangle.NO_BORDER);
        document.setFooter(footer);

        document.open();
        document.add(new Paragraph("hello world", MyFontUtil.SIMSUN_H2));

        document.newPage();
        document.add(new DottedLineSeparator());
        document.add(Chunk.NEWLINE);
        document.add(new DottedLineSeparator());
        document.add(Chunk.NEWLINE);
        document.add(new DottedLineSeparator());
        document.close();

        System.out.println("=========done=========");
    }
}

3、ireport

ireport使用参考下面的博客:

java 复制代码
https://blog.csdn.net/zhr19970910/article/details/119741134
相关推荐
默默coding的程序猿19 分钟前
3.前端和后端参数不一致,后端接不到数据的解决方案
java·前端·spring·ssm·springboot·idea·springcloud
虾球xz23 分钟前
CppCon 2018 学习:EFFECTIVE REPLACEMENT OF DYNAMIC POLYMORPHISM WITH std::variant
开发语言·c++·学习
Allen_LVyingbo28 分钟前
Python常用医疗AI库以及案例解析(2025年版、上)
开发语言·人工智能·python·学习·健康医疗
小哈龙32 分钟前
裸仓库 + Git Bash 搭建 本地 Git 服务端与客户端
开发语言·git·bash
在未来等你33 分钟前
JVM调优实战 Day 15:云原生环境下的JVM配置
java·jvm·性能优化·虚拟机·调优
funnycoffee12334 分钟前
Huawei 6730 Switch software upgrade example版本升级
java·前端·华为
Java初学者小白34 分钟前
秋招Day15 - Redis - 缓存设计
java·数据库·redis·缓存
缘来是庄34 分钟前
设计模式之组合模式
java·设计模式·组合模式
DKPT36 分钟前
Java组合模式实现方式与测试方法
java·笔记·学习·设计模式·组合模式
G探险者1 小时前
《如何在 Spring 中实现 MQ 消息的自动重连:监听与发送双通道策略》
java·开发语言·rpc