首页 技术 正文
技术 2022年11月21日
0 收藏 565 点赞 3,237 浏览 4317 个字

最近在工作做一个泰国的项目,应供应商要求,需要将每天的交易生成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;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774