Poi生成pfd

一、前言

如何使用原生的pdf

二、使用原生的poi

效果

在这里插入图片描述

代码


import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

/**
 * @Description
 * @Author saq
 * @Date 2022/9/15 16:29
 */
public class PdfStyleUtils {
    /**
     * 为表格添加头样式加颜色
     *
     * @param value   值
     * @param font    字体
     * @param colspan 占多少列
     * @return 添加的单元格
     */
    public static PdfPCell contCenterBgCell(String value, Font font, int colspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(colspan);
        cell.setBackgroundColor(new BaseColor(251, 188, 21));// 颜色
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    /**
     * 为表格内容添加样式【居中】
     *
     * @param value   值
     * @param font    字体
     * @param colspan 占多少列
     * @return 添加的单元格
     */
    public static PdfPCell contentCell(String value, Font font, int colspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    /**
     * 为表格内容添加样式【居左】
     *
     * @param value   值
     * @param font    字体
     * @param colspan 占多少列
     * @return 添加的单元格
     */
    public static PdfPCell contentCellLeft(String value, Font font, int colspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, font));
        cell.setBorder(0);
        return cell;
    }

    /**
     * 为表格内容添加样式【居右】
     *
     * @param value   值
     * @param font    字体
     * @param colspan 占多少列
     * @return 添加的单元格
     */
    public static PdfPCell contentCellRight(String value, Font font, int colspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, font));
        cell.setBorder(0);
        return cell;
    }


    /**
     * 表格里单元格宽度的控制
     *
     * @param table
     * @param widthPercentage 宽度百分比
     * @param columnWidth     列宽
     * @return
     * @throws DocumentException
     */
    public static PdfPTable cellSize(PdfPTable table, float widthPercentage, float[] columnWidth) throws DocumentException {
        table.setWidthPercentage(widthPercentage);
        table.setTotalWidth(columnWidth);
        return table;
    }
}

package com.zdhr.tgc.commodityrule;

import cn.hutool.core.convert.NumberChineseFormatter;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.zdhr.tgc.TgcApplication;
import com.zdhr.tgc.commodityrule.service.IItemUpShelvesService;
import com.zdhr.tgc.common.util.DateUtils;
import com.zdhr.tgc.common.util.pdf.PdfStyleUtils;
import com.zdhr.tgc.purchase.vo.PoDetailVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.util.UriUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TgcApplication.class)
@Slf4j
@ActiveProfiles("pre")
public class ItemUpShelvesControllerTest {

    @PostMapping("/export/pdf/test")
    @ApiOperation("采购单pdf")
    public void exportPdftest( HttpServletResponse response) {
        try {

            //第一步:申明文档
            Document document = new Document(PageSize.A4);


           
           //第二步:设置返回参数
            String fileName =URLEncoder.encode("pdf文件" , "UTF-8").replaceAll("\\+", "%20");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/pdf");
            response.addHeader("Content-Disposition", "attachment; filename=" + fileName+ ".pdf");
            PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
            document.open();


            //解决中文不显示问题
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);


            //如果有图片,设置图片位置
            URL path = this.getClass().getResource("/");
            //图片放在resources下面
            Image timgCover = Image.getInstance(path + "zdftLogo.png");
            timgCover.setAbsolutePosition(38, 765);
            timgCover.scaleAbsolute(100, 30);
            //加入文档
            document.add(timgCover);


            // 定义字体
            Font topfont = new Font(bfChinese, 15, Font.BOLD);
            Font coustomfont = new Font(bfChinese, 12, Font.NORMAL);
            Font fontChina10 = new Font(bfChinese, 10);
            Font fontChina9 = new Font(bfChinese, 9.5f, Font.BOLD);
            Font fontChina8 = new Font(bfChinese, 9.5f, Font.NORMAL);
            // 设置字体样式
            Paragraph pt0 = new Paragraph("标题", topfont);
            pt0.setAlignment(Element.ALIGN_CENTER);// 设置文字居中 0靠左 1,居中 2,靠右
            pt0.setIndentationLeft(12);// 左缩进
            pt0.setIndentationRight(12);// 右缩进
            pt0.setFirstLineIndent(24);// 首行缩进
            pt0.setSpacingBefore(4f);
            //设置标题到文档
            document.add(pt0);


            // 委托采购单标题
            String str1 = "标题下的文档";
            Paragraph pt1 = new Paragraph(str1, coustomfont);// 设置字体样式
            pt1.setAlignment(Element.ALIGN_CENTER);// 设置文字居中 0靠左 1,居中 2,靠右
            pt1.setIndentationLeft(12);// 左缩进
            pt1.setIndentationRight(12);// 右缩进
            pt1.setFirstLineIndent(24);// 首行缩进
            pt1.setSpacingAfter(12f);
            document.add(pt1);


            //头栏信息
            PdfPTable table = new PdfPTable(2);// 括号参数表示列
            table.setWidthPercentage(100);
            table.getDefaultCell().setBorder(0);

            // 单据号
            table.addCell(PdfStyleUtils.contentCellLeft("供货单位:" +"单位", fontChina10, 0));

            // 入库仓库
            table.addCell(PdfStyleUtils.contentCellLeft("入库仓库:" + "入库仓库", fontChina10, 0));

            // 采购单号
            table.addCell(PdfStyleUtils.contentCellLeft("采购单号:" + "采购单号", fontChina10, 0));

            SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 采购时间
            table.addCell(PdfStyleUtils.contentCellLeft("采购时间:" + simpleDateFormat.format(new Date()), fontChina10, 0));

            // 采购人
            table.addCell(PdfStyleUtils.contentCellLeft("采购人:" + "采购人", fontChina10, 0));

            // 联系电话
            table.addCell(PdfStyleUtils.contentCellLeft("联系电话:" + "联系电话", fontChina10, 0));

            //交付地址
            table.addCell(PdfStyleUtils.contentCellLeft("交付地址:" + "交付地址", fontChina10, 0));

            table.addCell(PdfStyleUtils.contentCellLeft("", fontChina10, 0));

            table.setSpacingBefore(4f);// 设置表格上面空白宽度
            table.setSpacingAfter(4f);// 设置表格下面空白宽度
            document.add(table);

            PdfPTable table1 = new PdfPTable(10);// 括号参数表示列
            table1.setWidthPercentage(100);
            table1.setSpacingBefore(4f);// 设置表格上面空白宽度

            // 打印表格部分
            PdfStyleUtils.cellSize(table1, 100, new float[]{5, 10, 20, 10, 10, 10, 5, 5, 8, 8});
            table1.addCell(PdfStyleUtils.contCenterBgCell("序号", fontChina9, 0));
            table1.addCell(PdfStyleUtils.contCenterBgCell("物料号", fontChina9, 0));
            table1.addCell(PdfStyleUtils.contCenterBgCell("物料名称", fontChina9, 0));
            table1.addCell(PdfStyleUtils.contCenterBgCell("品牌", fontChina9, 0));
            table1.addCell(PdfStyleUtils.contCenterBgCell("型号", fontChina9, 0));

            table1.addCell(PdfStyleUtils.contCenterBgCell("质地", fontChina9, 0));
            table1.addCell(PdfStyleUtils.contCenterBgCell("单位", fontChina9, 0));
            table1.addCell(PdfStyleUtils.contCenterBgCell("数量", fontChina9, 0));
            table1.addCell(PdfStyleUtils.contCenterBgCell("采购单价", fontChina9, 0));
            table1.addCell(PdfStyleUtils.contCenterBgCell("结算额", fontChina9, 0));


            // 采购单信息
            ArrayList<PoDetailVo> list = new ArrayList<>();
            PoDetailVo poDetailVo1 = new PoDetailVo();
            PoDetailVo poDetailVo2 = new PoDetailVo();
            PoDetailVo poDetailVo3 = new PoDetailVo();
            PoDetailVo poDetailVo4 = new PoDetailVo();
            PoDetailVo poDetailVo5 = new PoDetailVo();
            poDetailVo1.setOes("物料号1");
            poDetailVo1.setItemName("物料名称1");
            poDetailVo1.setBrand("品牌1");
            poDetailVo1.setModel("型号1");
            poDetailVo1.setFacTexture("质地1");
            poDetailVo1.setAssociatedZdBarcode("关联号1");
            poDetailVo1.setUnit("单位1");
            poDetailVo1.setQuantity(2);
            poDetailVo1.setUnitCost("1.00");
            poDetailVo1.setTotalCost("2.00");

            BeanUtils.copyProperties(poDetailVo1, poDetailVo2);
            BeanUtils.copyProperties(poDetailVo1, poDetailVo3);
            BeanUtils.copyProperties(poDetailVo1, poDetailVo4);
            BeanUtils.copyProperties(poDetailVo1, poDetailVo5);


            list.add(poDetailVo1);
            list.add(poDetailVo2);
            list.add(poDetailVo3);
            list.add(poDetailVo4);
            list.add(poDetailVo5);
            // 用于序号
            AtomicInteger count = new AtomicInteger(0);
            list.stream().forEach(item -> {
                table1.addCell(PdfStyleUtils.contentCell(count.incrementAndGet() + "", fontChina8, 0));// 序号
                table1.addCell(PdfStyleUtils.contentCell(item.getOes(), fontChina8, 0));// 物料号
                table1.addCell(PdfStyleUtils.contentCell(item.getItemId(), fontChina8, 0));// 物料名称
                table1.addCell(PdfStyleUtils.contentCell(item.getBrand(), fontChina8, 0));// 品牌
                table1.addCell(PdfStyleUtils.contentCell(item.getModel(), fontChina8, 0));// 型号

                table1.addCell(PdfStyleUtils.contentCell(item.getFacTexture(), fontChina8, 0));// 质地
                table1.addCell(PdfStyleUtils.contentCell(StringUtils.isEmpty(item.getUnit()) ? "" : item.getUnit() + "", fontChina8, 0));//单位
                table1.addCell(PdfStyleUtils.contentCell(item.getQuantity().toString(), fontChina8, 0));//数量
                table1.addCell(PdfStyleUtils.contentCell(item.getUnitCost(), fontChina8, 0));// 采购价
                table1.addCell(PdfStyleUtils.contentCell(item.getTotalCost(), fontChina8, 0));// 结算额


            });
            document.add(table1);

            double sum = list.stream().collect(Collectors.summingDouble(poDetailVo -> Double.parseDouble(poDetailVo.getTotalCost().replace(",",""))));

            BigDecimal bd = new BigDecimal(sum);
            sum = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();


            // 合计大写、合计
            PdfPTable table2 = new PdfPTable(3);// 括号参数表示列
            table2.setWidthPercentage(100);
            table2.setSpacingBefore(4f);// 设置表格上面空白宽度
            table2.getDefaultCell().setBorder(0);

            table2.addCell(PdfStyleUtils.contentCellLeft("合计:" + sum, fontChina10, 0));
            table2.addCell(PdfStyleUtils.contentCellLeft("", fontChina10, 0)); // 用于站位

            table2.addCell(PdfStyleUtils.contentCellLeft("合计大写:" + NumberChineseFormatter.format(sum
                    , true, true), fontChina10, 0));
            document.add(table2);

            // 打印时间、客户签收
            PdfPTable table3 = new PdfPTable(3);// 括号参数表示列
            table3.setWidthPercentage(100);
            table3.setSpacingBefore(4f);// 设置表格上面空白宽度
            table3.getDefaultCell().setBorder(0);

            table3.addCell(PdfStyleUtils.contentCellLeft("打印时间:" + DateUtils.format(new Date()), fontChina10, 0));
            table3.addCell(PdfStyleUtils.contentCellLeft("", fontChina10, 0));// 用于站位
            table3.addCell(PdfStyleUtils.contentCellLeft("签收:", fontChina10, 0));
            document.add(table3);

            // 关闭
            document.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码浪人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值