java pdf方案

1、htmltopdf


	io.woo
	htmltopdf
	1.0.8

 

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("

sssssssss

").defaultEncoding(CharsetUtil.UTF_8)) .convert("C:\\siefile\\bak11\\v4\\PdfDemo.pdf"); } }

2、openpdf


	com.github.librepdf
	openpdf
	1.3.35

 java pdf方案_第1张图片

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使用参考下面的博客:

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

你可能感兴趣的:(java,pdf,开发语言)