当前位置: 首页 > news >正文

汕头云建站模板免费域名的网站

汕头云建站模板,免费域名的网站,茶叶电子商务网站建设的结论,湖南网站备案要多少天1、问题描述 需要将发票导出成pdf&#xff0c;要求每页都必须包含发票信息和表头行。 2、解决方法 使用IText工具实现PDF导出 IText8文档&#xff1a;Examples (itextpdf.com) 3、我的代码 引入Itext依赖&#xff0c;我这里用的是8.0.1版本 <dependency><groupId>…

1、问题描述 

        需要将发票导出成pdf,要求每页都必须包含发票信息和表头行。

 

 

2、解决方法

        使用IText工具实现PDF导出

        IText8文档:Examples (itextpdf.com)

 

3、我的代码

      

 

 

        引入Itext依赖,我这里用的是8.0.1版本

 <dependency><groupId>com.itextpdf</groupId><artifactId>itext7-core</artifactId><version>8.0.1</version><type>pom</type></dependency>
MyItextpdfUtils.java
package com.easyexcel.util;import com.easyexcel.handler.PaginationEventHandler;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.element.*;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.properties.AreaBreakType;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.UnitValue;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;/*** @author Wulc* @date 2023/8/10 17:08* @description*/
@Component
public class MyItextpdfUtils {public void createPDF() throws java.io.IOException {Resource resource = new ClassPathResource("/");String path = resource.getFile().getPath();//设置中文字体 C:\Windows\Fonts//PdfFont chineseFont =getFont();//PdfFont chineseFont = PdfFontFactory.createFont(this.getClass().getClassLoader().getResource("simsun.ttf").getPath());PdfFont chineseFontForTemplate = PdfFontFactory.createFont("D:\\学习资料\\后端\\STSONG.TTF");PdfFont chineseFontForContent = PdfFontFactory.createFont("D:\\学习资料\\后端\\STSONG.TTF");//创建每页的共有模板//*********************每页的共有模板*********************************String templatePath = path + "\\template.pdf";PdfDocument pdfDocumentTemplate = new PdfDocument(new PdfWriter(templatePath));//Document documentTemplate = new Document(pdfDocumentTemplate, PageSize.A4).setFont(chineseFontForTemplate);Document documentTemplate = new Document(pdfDocumentTemplate, PageSize.A4);//插入logo图片Table logoTemplateTable = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth().setBorder(Border.NO_BORDER);ImageData imageData = ImageDataFactory.create(this.getClass().getClassLoader().getResource("logo.png"));Image image = new Image(imageData);image.setHeight(50);image.setWidth(100);logoTemplateTable.addCell(new Cell().setBorder(Border.NO_BORDER).add(image));//插入logo图片下方的一些信息Table logoInfoTable = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth().setBorder(Border.NO_BORDER);logoInfoTable.addCell(new Cell().setBorder(Border.NO_BORDER).setPadding(1).setFontSize(10).add(new Paragraph("Description1")));logoInfoTable.addCell(new Cell().setBorder(Border.NO_BORDER).setPadding(1).setFontSize(10).add(new Paragraph("Description2")));logoInfoTable.addCell(new Cell().setBorder(Border.NO_BORDER).setPadding(1).setFontSize(10).add(new Paragraph("Description3")));//插入标题Table titleTable = new Table(UnitValue.createPercentArray(4)).useAllAvailableWidth().setBorder(Border.NO_BORDER);titleTable.addCell(new Cell(1, 4).setBorder(Border.NO_BORDER).setPadding(1).setFontSize(15).add(new Paragraph("TITLE")).setTextAlignment(TextAlignment.CENTER));//插入标题下的一些信息Table titleInfoTable = new Table(UnitValue.createPercentArray(4)).useAllAvailableWidth();titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("QuestionA")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("AnswerA")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("QuestionB")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("AnswerB")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("QuestionC")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("AnswerC")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("QuestionD")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("AnswerD")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("QuestionE")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("AnswerE")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("QuestionF")));titleInfoTable.addCell(new Cell().setPadding(1).setFontSize(10).add(new Paragraph("AnswerF")));documentTemplate.add(logoTemplateTable);documentTemplate.add(logoInfoTable);documentTemplate.add(titleTable);documentTemplate.add(titleInfoTable);//*********************每页的共有模板*********************************//*********************每页的内容************************************String contentPath = path + "\\content.pdf";PdfDocument pdfDocumentContent = new PdfDocument(new PdfWriter(contentPath));//把内容使用共有模板pdfDocumentContent.addEventHandler(PdfDocumentEvent.END_PAGE, new PaginationEventHandler(pdfDocumentTemplate.getFirstPage().copyAsFormXObject(pdfDocumentContent)));Document documentContent = new Document(pdfDocumentContent, PageSize.A4).setFont(chineseFontForContent);//每页的content距离上面的template的距离documentContent.setTopMargin(250);Table contentTable = new Table(UnitValue.createPercentArray(6)).useAllAvailableWidth();//插入清单表格标题contentTable.addHeaderCell(new Cell().setFontSize(8).add(new Paragraph("No")));contentTable.addHeaderCell(new Cell().setFontSize(8).add(new Paragraph("title1")));contentTable.addHeaderCell(new Cell().setFontSize(8).add(new Paragraph("title2")));contentTable.addHeaderCell(new Cell().setFontSize(8).add(new Paragraph("title3")));contentTable.addHeaderCell(new Cell().setFontSize(8).add(new Paragraph("title4")));contentTable.addHeaderCell(new Cell().setFontSize(8).add(new Paragraph("title5")));for (int i = 0; i < 300; i++) {contentTable.addCell(new Cell().setFontSize(8).add(new Paragraph(String.valueOf(i))));contentTable.addCell(new Cell().setFontSize(8).add(new Paragraph("content1")));contentTable.addCell(new Cell().setFontSize(8).add(new Paragraph("content2")));contentTable.addCell(new Cell().setFontSize(8).add(new Paragraph("content3")));contentTable.addCell(new Cell().setFontSize(8).add(new Paragraph("content4")));contentTable.addCell(new Cell().setFontSize(8).add(new Paragraph("content5")));}//尾页Table lastInfoTable = new Table(UnitValue.createPercentArray(3)).setWidth(300);lastInfoTable.addCell(new Cell(1, 3).setPadding(1).setFontSize(8).add(new Paragraph("Total:")));lastInfoTable.addCell(new Cell(1, 1).setPadding(1).setFontSize(8).add(new Paragraph("统计A:")));lastInfoTable.addCell(new Cell(1, 2).setPadding(1).setFontSize(8).add(new Paragraph("1234567")));lastInfoTable.addCell(new Cell(1, 1).setPadding(1).setFontSize(8).add(new Paragraph("统计B:")));lastInfoTable.addCell(new Cell(1, 2).setPadding(1).setFontSize(8).add(new Paragraph("7654321")));//*********************每页的内容************************************documentContent.add(contentTable);//尾页新开一页documentContent.add(new AreaBreak(AreaBreakType.NEXT_PAGE));documentContent.add(lastInfoTable);documentTemplate.close();documentContent.close();}}

 

PDFTest.java​​​​​​​
package com.easyexcel;import com.easyexcel.util.MyItextpdfUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.io.IOException;/*** @author Wulc* @date 2023/8/10 17:52* @description*/
@SpringBootTest(classes = SpringbootApplication.class)
@RunWith(SpringRunner.class)
public class PDFTest {@Autowiredprivate MyItextpdfUtils myItextpdfUtils;@Testpublic void test6() throws IOException {myItextpdfUtils.createPDF();}
}

        测试一下:

 

4、总结

        IText8不支持中文,需要引入外部字体文件,如果是以其中一个pdf作为每页的背景模板生成PDF这种方式(copyAsFormXObject),它只能支持其中一个pdf中文,另一个就不支持了。

Document documentTemplate = new Document(pdfDocumentTemplate, PageSize.A4).setFont(chineseFontForTemplate);
Document documentContent = new Document(pdfDocumentContent, PageSize.A4).setFont(chineseFontForContent);

如上代码,虽然我同时把背景版和内容同时都设置了中文字体,但是template和content合一块的时候,template的背景版pdf的中文字体就会失效了。

         不过还好,因为是海外的发票都是英文的,因此不需要考虑支持中文的问题。

        希望哪位大佬能帮忙解决一下IText8 copyAsFormXObject中文兼容性问题!!!

5、参考资料

https://www.cnblogs.com/sky-chen/p/13026203.html#autoid-1-4-5-0-0-0
https://kb.itextpdf.com/home/it7kb/examples/repeating-parts-of-a-form
https://zhuanlan.zhihu.com/p/537723847
https://blog.csdn.net/weixin_43409994/article/details/118157694
https://blog.csdn.net/u012397189/article/details/126345744
https://blog.csdn.net/Thinkingcao/article/details/84988392

 

 


文章转载自:
http://insusceptibility.fznj.cn
http://confabulation.fznj.cn
http://trimester.fznj.cn
http://lusus.fznj.cn
http://microtomy.fznj.cn
http://putt.fznj.cn
http://druggist.fznj.cn
http://overplus.fznj.cn
http://snakeroot.fznj.cn
http://fosbury.fznj.cn
http://immanence.fznj.cn
http://gantt.fznj.cn
http://minivan.fznj.cn
http://taxloss.fznj.cn
http://gregarine.fznj.cn
http://vilification.fznj.cn
http://gravette.fznj.cn
http://rostrate.fznj.cn
http://muddy.fznj.cn
http://blamable.fznj.cn
http://toluene.fznj.cn
http://religionise.fznj.cn
http://infarct.fznj.cn
http://goldenrod.fznj.cn
http://flibbertigibbet.fznj.cn
http://hanuka.fznj.cn
http://delphinoid.fznj.cn
http://metanephros.fznj.cn
http://lieutenant.fznj.cn
http://vociferation.fznj.cn
http://eruptive.fznj.cn
http://proportionment.fznj.cn
http://basketballer.fznj.cn
http://musketoon.fznj.cn
http://curvature.fznj.cn
http://incurably.fznj.cn
http://rason.fznj.cn
http://parricide.fznj.cn
http://fluviograph.fznj.cn
http://utriculitis.fznj.cn
http://insititious.fznj.cn
http://cotransduction.fznj.cn
http://accustomed.fznj.cn
http://fossorial.fznj.cn
http://pennon.fznj.cn
http://tishri.fznj.cn
http://subdiscipline.fznj.cn
http://extraction.fznj.cn
http://yangon.fznj.cn
http://vaginitis.fznj.cn
http://nigerien.fznj.cn
http://vitriolic.fznj.cn
http://whirligig.fznj.cn
http://discourtesy.fznj.cn
http://reconvert.fznj.cn
http://anthelix.fznj.cn
http://specktioneer.fznj.cn
http://ovulation.fznj.cn
http://sfumato.fznj.cn
http://chime.fznj.cn
http://servia.fznj.cn
http://hoist.fznj.cn
http://fibrillar.fznj.cn
http://cheddite.fznj.cn
http://hydrazoate.fznj.cn
http://retorsion.fznj.cn
http://glooming.fznj.cn
http://seclusive.fznj.cn
http://redeveloper.fznj.cn
http://wftu.fznj.cn
http://rehearse.fznj.cn
http://eaprom.fznj.cn
http://formulation.fznj.cn
http://violone.fznj.cn
http://grounded.fznj.cn
http://boned.fznj.cn
http://hypophloeodal.fznj.cn
http://licencee.fznj.cn
http://tondo.fznj.cn
http://seakeeping.fznj.cn
http://diamantiferous.fznj.cn
http://backpat.fznj.cn
http://athetoid.fznj.cn
http://smsa.fznj.cn
http://sericulture.fznj.cn
http://swabia.fznj.cn
http://saccade.fznj.cn
http://gaminerie.fznj.cn
http://exertive.fznj.cn
http://kidney.fznj.cn
http://porcellanic.fznj.cn
http://flareback.fznj.cn
http://paedobaptism.fznj.cn
http://finsen.fznj.cn
http://carex.fznj.cn
http://dihydro.fznj.cn
http://esquamate.fznj.cn
http://graftabl.fznj.cn
http://sarcous.fznj.cn
http://sauna.fznj.cn
http://www.dt0577.cn/news/99992.html

相关文章:

  • 商城网站架构江苏建站
  • 安徽省网站建设公司推广文案怎么写
  • 建设个人网站教程获客软件
  • 附近临时工500元一天南昌seo优化
  • 大型电商网站开发项目品牌服务推广
  • 好的平面网站模板外链购买交易平台
  • 网站后端开发语言谷歌seo建站
  • web项目开发汕头seo优化公司
  • 公司网站维护教程餐饮品牌全案策划
  • 南昌做网站的流程2022年适合小学生的新闻
  • 东莞人才网 东莞招聘信息7个湖北seo网站推广策略
  • 专业的公司网站制作服务免费下载优化大师
  • 网站网站制作网站制作平台
  • 如何用bootstrap做网站免费网站的软件
  • 永嘉网站制作系统简短的营销软文范文
  • 建设大型门户网站镇江网站制作公司
  • 自贡网站制作广州今天刚刚发生的重大新闻
  • 宿州市建设局网站武汉seo关键字优化
  • 做室内3d设计的网站网络科技公司骗了我36800
  • 做网站开发赚钱吗免费的seo优化
  • 可以做百度百科参考资料的网站seo人才招聘
  • 做视频网站要注意什么seo关键字怎么优化
  • 仿99健康网网站源码热词分析工具
  • 东莞哪里做网站腾讯营销平台
  • 受欢迎的昆明网站推广西安百度关键词优化
  • 单词优化和整站优化最有效的100个营销方法
  • 东莞做网站 9353seo外链优化
  • 吴江建网站优荐苏州聚尚网络义乌最好的电商培训学校
  • 网站建设 语言seo外链发布软件
  • 做外贸没有企业网站培训网址