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
相关推荐
KIDAKN6 分钟前
java--怎么定义枚举类
java·开发语言
何中应7 分钟前
第一个人工智能(AI)问答Demo
java·人工智能·语言模型
海天胜景8 分钟前
C# 中常用的 字符串截取方法
开发语言·c#
东阳马生架构42 分钟前
商品中心—3.商品可采可补可售的技术文档
java
bxlj_jcj1 小时前
解锁Java线程池:性能优化的关键
java·性能优化·多线程
海棠一号1 小时前
JAVA理论第七章-MYSQL
java·数据库·mysql
tkevinjd1 小时前
C++中线程库的基本操作
开发语言·c++
CodeWithMe2 小时前
【C/C++】不同防止头文件重复包含的措施
c语言·开发语言·c++
子豪-中国机器人2 小时前
C++ 信息学奥赛总复习题答案解析
开发语言·c++·算法