java pdf方案

1、htmltopdf

<dependency>
	<groupId>io.woo</groupId>
	<artifactId>htmltopdf</artifactId>
	<version>1.0.8</version>
</dependency>

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、docx4j

<properties>
    <docx4j.version>8.3.10</docx4j.version>
</properties>

 

<dependency>
	<groupId>org.docx4j</groupId>
	<artifactId>docx4j-JAXB-Internal</artifactId>
	<version>${docx4j.version}</version>
</dependency>
<dependency>
	<groupId>org.docx4j</groupId>
	<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
	<version>${docx4j.version}</version>
</dependency>
<dependency>
	<groupId>org.docx4j</groupId>
	<artifactId>docx4j-export-fo</artifactId>
	<version>${docx4j.version}</version>
</dependency>
public static void main(String[] args) throws Exception {
	String path = "C:/siefile/bak11/v9/";
	String templateFilePath = path + "docx4j-template.docx";
	String outFilePath = path + "docx4j测试.pdf";

	// 加载word模板
	WordprocessingMLPackage wordPackage = WordprocessingMLPackage.load(new File(templateFilePath));
	MainDocumentPart documentPart = wordPackage.getMainDocumentPart();

	// 找到表格第二行,然后进行变量替换
	ClassFinder find = new ClassFinder(Tbl.class);
	new TraversalUtil(documentPart.getContent(), find);
	Tbl table = (Tbl) find.results.get(0);
	List<Object> tableContent = table.getContent();
	Tr templateTr = (Tr) tableContent.get(1);
	String templateTrXml = XmlUtils.marshaltoString(templateTr);
	List<Map> list = getTableData();
	for (Map map : list) {
		Tr newTr = (Tr) XmlUtils.unmarshallFromTemplate(templateTrXml, map);
		tableContent.add(newTr);
	}
	tableContent.remove(1);

	// 普通的变量替换
	Map<String, String> data = new HashMap<>();
	data.put("detail", "用这些语言编写的程序和上面我们讲的命令行客户端");
	data.put("remark", "对mysql-connector-java源码的分析");
	VariablePrepare.prepare(wordPackage);
	documentPart.variableReplace(data);

	Docx4J.toPDF(wordPackage, new FileOutputStream(outFilePath));
	System.out.println("=========done========");
}

private static List<Map> getTableData() {
	List list = new ArrayList();
	for (int index = 0; index < 5; index++) {
		HashMap map = new HashMap();
		map.put("name", "name_" + index);
		map.put("age", "age_" + index);
		list.add(map);
	}
	return list;
}

word模板 :注意中文最好设置字体为宋体或者微软雅黑,不然会乱码

3、openpdf

<dependency>
	<groupId>com.github.librepdf</groupId>
	<artifactId>openpdf</artifactId>
	<version>1.3.35</version>
</dependency>

 

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=========");
    }
}

4、ireport

ireport使用参考下面的博客:

https://blog.csdn.net/zhr19970910/article/details/119741134

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值