使用 Itext 生成PDF

一、生成PDF,所需jar包(itext-2.0.8.jar,iTextAsian.jar)

在springboot中只需要引入依赖即可,依赖代码如下:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.0.6</version>
</dependency>

二、生成落地pdf及pdf转字节数组

package com.ulic.yyl.customerCenter.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
 * 生成pdf落地文件
 * 将落地pdf文件转成字节数组
 * 2019年7月12日
 */
@Controller
public class CreatePdf {

    @RequestMapping(value="/testPdf")
    public void testPdf() throws IOException {
        String filename = "D:/testpdf/testTable3.pdf";
        createPDF(filename);
        System.out.println("打印完成");
    }
      /*public void createPDF(String filename) throws IOException {
            Document document = new Document(PageSize.A4);
            try {
                PdfWriter.getInstance(document, new FileOutputStream(filename));
                document.addTitle("example of PDF");
                document.open();
                document.add(new Paragraph("Hello World!"));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            } finally {
                document.close();
            }
          }*/
    public static PdfPTable createTable(PdfWriter writer) throws DocumentException, IOException{
        PdfPTable table = new PdfPTable(2);//生成一个两列的表格
        PdfPCell cell;
        int size = 15;
        cell = new PdfPCell(new Phrase("one"));
        cell.setFixedHeight(size*2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("two"));
        cell.setFixedHeight(size*2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("three"));
        cell.setFixedHeight(size*2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("four"));
        cell.setFixedHeight(size*2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("five"));
        cell.setColspan(2);//设置所占列数
        cell.setFixedHeight(size*2);//设置高度
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
        table.addCell(cell);
        return table;
      }
      
    public void createPDF(String filename) throws IOException {
        Document document = new Document(PageSize.A4);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
            document.addTitle("example of PDF");
            document.open();
            PdfPTable table = createTable(writer);
            document.add(table);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
        
      }
     
    /**
     * 获取字节数组
     * @throws IOException
     */
    @RequestMapping(value="/downLoadByUrl")
    public void  downLoadByUrl() throws IOException {
        
        String urlStr = "D:/testpdf/testTable3.pdf";
        
        InputStream inputStream = new FileInputStream(new File(urlStr));
        byte[] getData = readInputStream(inputStream);
        if(inputStream!=null){
            inputStream.close();
        }
        System.out.println("byte[]:{}"+getData.length);
        
    }


        /**
         * 从输入流中获取字节数组
         * @param inputStream
         * @return
         * @throws IOException
         */
        public static  byte[] readInputStream(InputStream inputStream) throws IOException {
            byte[] buffer = new byte[1024];
            int len = 0;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            while((len = inputStream.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            bos.close();
            return bos.toByteArray();
        }
}
原文地址:https://www.cnblogs.com/yinyl/p/11175531.html