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
相关推荐
树叶@10 分钟前
Python数据分析7
开发语言·python
wydaicls13 分钟前
十一.C++ 类 -- 面向对象思想
开发语言·c++
白宇横流学长15 分钟前
基于SpringBoot实现的大创管理系统设计与实现【源码+文档】
java·spring boot·后端
fat house cat_43 分钟前
【redis】线程IO模型
java·redis
Biomamba生信基地1 小时前
R语言基础| 下载、安装
开发语言·r语言·生信·医药
姜君竹1 小时前
QT的工程文件.pro文件
开发语言·c++·qt·系统架构
奇树谦1 小时前
使用VTK还是OpenGL集成到qt程序里哪个好?
开发语言·qt
VBA63371 小时前
VBA之Word应用第三章第十节:文档Document对象的方法(三)
开发语言
老胖闲聊1 小时前
Python Rio 【图像处理】库简介
开发语言·图像处理·python
码界奇点2 小时前
Python Flask文件处理与异常处理实战指南
开发语言·python·自然语言处理·flask·python3.11