最近在工作做一个泰国的项目,应供应商要求,需要将每天的交易生成pdf格式的报表上传到供应商的服务器,特此记录实现方法。废话不多说,直接上代码:
THSarabunNew.ttf该文件是泰国字体自行网上下载即可
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* TOT上报PDF文件生成工具
*/
@Slf4j
@Component
public class TOTReportPdfService {
private String thaiFont = "./font/THSarabunNew.ttf";
//字体格式
Font font = FontFactory.getFont(thaiFont, BaseFont.IDENTITY_H, false,-1.0F, -1);
public void generatorPdf(String outPutFilePath, String headerSuffix){
Document document = new Document(PageSize.A4.rotate(), 20, 20, 50, 20);
File outputFile = createReportFile(outPutFilePath);
try {
//设置输出位置
PdfWriter.getInstance(document, new FileOutputStream(outputFile));
//打开文档
document.open();
float[] columnWidths = {80, 200, 100,100,80,80,80,80,80,80,80,80};//表格每一列的宽度
PdfPTable table = createTable(12, columnWidths);
//表头
generatorHeader(table, headerSuffix);
document.add(table);
} catch (DocumentException e) {
e.printStackTrace();
log.error("TOTReportPdfService.generatorPdf 文件创建失败。{}", e.getMessage());
} catch (FileNotFoundException e) {
e.printStackTrace();
log.error("TOTReportPdfService.generatorPdf 找不到文件{}", e.getMessage());
} catch (IOException e) {
e.printStackTrace();
} finally {
document.close();
}
}
private File createReportFile(String outPutFilePath) {
File file = new File(outPutFilePath);
if (!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
private PdfPTable createTable(int colNum, float[] columnWidths) {
PdfPTable table = new PdfPTable(colNum);
try {
table.setWidths(columnWidths);
} catch (DocumentException e) {
e.printStackTrace();
log.error("创建pdf报表失败{}",e.cause())
}
table.setSpacingBefore(20f);//设置页边距
table.setWidthPercentage(100);//设置表格宽度为100%
return table;
}
/**
* 报表表头 12列 8行
* @param table
*/
private void generatorHeader(PdfPTable table, String headerSuffix) {
table.addCell(createPdfPCell("", 3, 1));
table.addCell(createPdfPCell("บริษัท บลูเพย์ จำกัด", 5, 1, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("", 4, 1));
table.addCell(createPdfPCell("", 3, 1));
table.addCell(createPdfPCell("การรับชำระผ่านช่องทางอิเล็กทรอนิกส์", 5, 1, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("RP0001_" + headerSuffix, 4, 1));
table.addCell(createPdfPCell("", 3, 1));
table.addCell(createPdfPCell("รายงานการรับชำระประจำวัน", 5, 1, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("", 3, 1));
table.addCell(createPdfPCell("Page 1 Of 1", 4, 1, Element.ALIGN_RIGHT));
table.addCell(createPdfPCell("Location 0BBPW", 12, 1, Element.ALIGN_LEFT));
table.addCell(createPdfPCell("ลำดับที่", 1, 3, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("เลขที่เอกสารยืนยันการทำรายการ", 1, 3, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("Account No. - Invoice No.", 2,3, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("อั\u008Dตราภาษี", 1,3, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("มูลค่าสินค้า / บริการ ที่ต้องเสียภาษี", 5, 1, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("มูลค่าสินค้า / บริการ", 1,1, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("รวมทั้งสิ้น", 1, 3, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("ภาษี 7% ", 3, 1, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("ภาษี 0% ", 1 , 2, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("รวม", 1,2, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("ยกเว้นภาษี", 1,2, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("มูลค่าสุทธิ", 1,1, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("ภาษีมูลค่าเพิ่ม", 1, 1, Element.ALIGN_CENTER));
table.addCell(createPdfPCell("รวม", 1,1, Element.ALIGN_CENTER));
}
private PdfPCell createPdfPCell(String text, int colSpan, int rowSpan) {
return createPdfPCell(text, colSpan, rowSpan, Element.ALIGN_RIGHT);
}
private PdfPCell createPdfPCell(String text, int colSpan, int rowSpan, int align) {
PdfPCell pdfPCell = new PdfPCell();
pdfPCell.setColspan(colSpan);
pdfPCell.setRowspan(rowSpan);
pdfPCell.setPhrase(createParagraph(text));
pdfPCell.setHorizontalAlignment(align);
pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
return pdfPCell;
}
private Paragraph createParagraph(String text) {
Paragraph paragraph;
paragraph = new Paragraph(text, font);
paragraph.setAlignment(Phrase.ALIGN_CENTER);
return paragraph;
}
}
java生成复杂word文档
在Web应用中,有时需要按照固定的模板将数据导出到Word,如流程审批单,在流程处理完成后将处理过程按照流程单的要求导出,有时程序中需要实现生成 标准Word文档,要求能够打印,并且保持页面样式不变, ...
使用FastReport报表工具生成标签打印文档
在我们实际开发报表的时候,我们需要按一定的业务规则组织好报表的模板设计,让报表尽可能的贴近实际的需求,在之前的随笔中介绍了FastRe ...
Spring Boot 集成 Swagger 生成 RESTful API 文档
原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...
Swagger+Spring mvc生成Restful接口文档
简介 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集 ...
使用jsdoc-toolkit来自动生成js api文档
近来前端组小盆友开发的类库越来越多,很多情况下彼此不知道写了些什么方法,为了更好的合作提高工作效率,找了个比较好的api文档生成方法.使用jsdoc-toolkit来自动生成js api文档. 一. ...
Java 后台创建word 文档
---恢复内容开始--- Java 后台创建 word 文档 自己总结 网上查阅的文档 分享POI 教程地址:http://www.tuicool.com/articles/emqaEf6 方式一. ...
Java解析word,获取文档中图片位置
前言(背景介绍): Apache POI是Apache基金会下一个开源的项目,用来处理office系列的文档,能够创建和解析word.excel.ppt格式的文档. 其中对word文档的处理有两个技术 ...
CEBX格式的文档如何转换为PDF格式文档、DOCX文档?
方正阿帕比CEBX格式的文档如何转换为PDF格式文档.DOCX文档? 简介: PDF.Doc.Docx格式的文档使用的非常普遍,金山WPS可以直接打开PDF和Doc.Docx文档,使用也很方便. CE ...
Android开发——使用Jword生成本地word文档
本文主要介绍如何使用Jword生成本地word文档,这里涉及到Jword的使用技巧,本文给出相应的代码,需要的朋友可以参考下. 为什么使用Jword呢?因为IText .Freemark在安卓平台上压 ...
随机推荐
Business Unit Lookup in Form
Just add the below code in lookup() of StringEdit control in Form to get the Business Unit Lookup: p ...
Codeforces Round #384 (Div. 2)D - Chloe and pleasant prizes 树形dp
D - Chloe and pleasant prizes 链接 http://codeforces.com/contest/743/problem/D 题面 Generous sponsors of ...
Drupal7模块multiselect使用
Drupal二次开发的时候,我们时常要使用到多选列表,但是官方默认的多选下拉列表,是在不敢恭维如下图所示: 不过难看不可怕,Drupal有两万第三方模块做支撑,只有你想不到,没有找不到的. 功夫不负有 ...
asp.net C# 题目大全
net001在线饰品销售系统 net002鲜花商城 net003商品销售管理系统 net004在线辅导答疑 net005土地税务管理系统 net006旅游管理 net007房产中介 net008房产信 ...
SaberRD之直流工作点分析
直流工作点分析(DC Operating Point Analysis)用于确定电路的静态工作点. 静态工作点的概念来源于三极管的电流放大特性.三极管放大电路中,当交流输入信号为零时,电路处于直流工作 ...
Python小白 哆唻a梦 用turtle绘图
点击观看视频 # -*- coding: utf-8 -*- """ Created on Sat Nov 10 22:02:32 2018 @author: 10029 ...
png 2 icon
http://www.easyicon.net/covert/ 这个网页可以转换png图片为icon格式
JavaScriptDOM操作那些事儿
查询: ①.标准DOM操作API: document.getElementById. document.getElementsByTagName. document.getElementsByName ...
QT 交叉编译工具选择
使用QT交叉编译,生成的都是x86的可执行文件.Zoro告诉我交叉工具配置错了. 参考链接: http://www.cnblogs.com/zengjfgit/p/4744507.html linux ...