相关jar包,字体文件下载
链接:https://pan.baidu.com/s/1jMSHGwTKcHGAzgTmvCnyWA
提取码:8866
最近项目中用到PDF打印功能,要求生成PDF直接在页面打印,于是写了这个简单的功能
代码实现PDF生成,返回给前端url地址直接打开,(IE浏览器不支持需要单独下载后打开PDF)
import com.itextpdf.text.Document;
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;
public static String getPdf(JSONObject jsonObject) {
String serverUrl="";
try{
//获取serverUrl,浏览器直接访问的路径
String pathTemp=st.format(new Date())+"/";
String tomcatPath = System.getProperty("catalina.home");
String saveDirectory = tomcatPath + File.separator + "webapps/uploadpdf/"+pathTemp;
File file = new File(saveDirectory);
if (!file.exists()) {
file.mkdirs();
}
String pdfName=StringUtil.getUUID();
serverUrl=pf.getServerUrl()+"uploadpdf/"+pathTemp+pdfName+".pdf";
Document document = new Document(PageSize.A4);//创建PDF文件
PdfWriter.getInstance(document, new FileOutputStream(saveDirectory+"/"+pdfName+".pdf"));
document.open();
//document.add(PdfFontUtils.getFont(1, pf.getTitle()));
document.add(PdfFontUtils.getFont(1, "标题"));
PdfPTable table = new PdfPTable(5);
table.setWidths(new int[] { 1, 2, 1, 1, 1 });
PdfPCell cell = null;
// 由5列变成1列
//getPdfPCell(字体大小,内容,跨列数,跨行数,行高,cell.ALIGN_MIDDLE,水平对齐,垂直对齐,边框显示)
cell=getPdfPCell(6,"项目名称:",5,1,30,cell.ALIGN_MIDDLE,Element.ALIGN_LEFT,Element.ALIGN_MIDDLE,0);
table.addCell(cell);
cell=getPdfPCell(12,"测是",5,1,30,cell.ALIGN_MIDDLE,Element.ALIGN_LEFT,Element.ALIGN_MIDDLE,0);
table.addCell(cell);
cell=getPdfPCell(12,"依据性文件:",5,1,50,cell.ALIGN_MIDDLE,Element.ALIGN_LEFT,Element.ALIGN_MIDDLE,0);
table.addCell(cell);
cell=getPdfPCell(12,"联系人",1,3,60,cell.ALIGN_MIDDLE,Element.ALIGN_CENTER,Element.ALIGN_MIDDLE,0);
table.addCell(cell);
HashMap<String, String> infoMap = new HashMap<String, String>();
for(int i=0;i<infoCnt.length;i++){
infoMap= (HashMap<String, String>) infoCnt[i];
String supName=(i+1)+""; cell=getPdfPCell(12,supName,2,1,25,cell.ALIGN_MIDDLE,Element.ALIGN_LEFT,Element.ALIGN_MIDDLE,0);
table.addCell(cell); cell=getPdfPCell(12,supName,1,1,25,cell.ALIGN_MIDDLE,Element.ALIGN_RIGHT,Element.ALIGN_MIDDLE,0);
table.addCell(cell); cell=getPdfPCell(12,supName,1,1,25,cell.ALIGN_MIDDLE,Element.ALIGN_RIGHT,Element.ALIGN_MIDDLE,0);
table.addCell(cell);
}
document.add(table);
document.add(PdfFontUtils.getFont(21, "备注信息000000000000000000"));
document.close();
}catch (Exception e) {
e.printStackTrace();
}
return serverUrl;
}
工具类
public static PdfPCell getPdfPCell(int font,String text,int colspan,int rowspan,int rowHeight,int ALIGN_MIDDLE,int ALIGN_CENTER
,int chuizhicenter,int biankuang){
PdfPCell cell = null;
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(font, text)));
if(rowspan==0){rowspan=1;}
cell.setRowspan(rowspan);
if(colspan==0){colspan=1;}
cell.setColspan(colspan);
if(rowHeight==0){rowHeight=30;}
cell.setFixedHeight(rowHeight);
cell.setUseAscender(true);
if(ALIGN_MIDDLE!=0){
cell.setVerticalAlignment(ALIGN_MIDDLE);
}
if(ALIGN_CENTER!=0){
cell.setHorizontalAlignment(ALIGN_CENTER); //水平居中
}
if(chuizhicenter!=0){
cell.setVerticalAlignment(chuizhicenter); //垂直居中
}
if(biankuang!=0){
cell.disableBorderSide(biankuang);
}
return cell;
}
import java.io.File;
import java.io.IOException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
public class PdfFontUtils {
// 字体
private static BaseFont baseFont = null;
static{
try {
/**
* 设置字体
*
* windows路径字体
* FONT_TYPE=C:/Windows/fonts/simsun.ttc
* linux路径字体 宋体 (如果没有这个字体文件,就将windows的字体传上去)
* FONT_TYPE=/usr/share/fonts/win/simsun.ttc
*/
//可以用配置文件读取
//获取配置
//PropertiesLoader pl = new PropertiesLoader("/config/config.properties");
//拼接文件web访问路径
//String FONT_TYPE = pl.getProperty("FONT_TYPE");
//解决中文问题 幼圆
String tomcatPath = System.getProperty("catalina.home");
String saveDirectory = tomcatPath + File.separator + "webapps/default/cost/simsun.ttc";
System.out.println(saveDirectory);
baseFont = BaseFont.createFont(saveDirectory+",1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 文档超级 排版
* @param type 1-标题 2-标题一 3-标题二 4-标题三 5-正文 6-左对齐
*/
public static Paragraph getFont(int type, String text){
Font font = new Font(baseFont);
if(1 == type){//1-标题
font.setSize(20f);
font.setStyle(Font.BOLD);
} else if(2 == type){//2-标题一
font.setSize(16f);
font.setStyle(Font.BOLD);
} else if(3 == type){//3-标题二
font.setSize(14f);
font.setStyle(Font.BOLD);
} else if(4 == type){//4-标题三
font.setSize(14f);
} else if(5 == type){//5-正文
font.setSize(14f);
} else if(6 == type){//6-左对齐
font.setSize(14f);
} else if(7 == type){
font.setSize(14f);//默认大小
} else if(8 == type){
font.setSize(8f);//默认大小
} else if(10 == type){
font.setSize(10f);//默认大小
}else if(12 == type){
font.setSize(12f);//默认大小
}else if(21 == type){
font.setSize(12f);//备注信息
}
//注: 字体必须和 文字一起new
Paragraph paragraph = new Paragraph(text, font);
if(1 == type){
paragraph.setAlignment(Paragraph.ALIGN_CENTER);//居中
paragraph.setSpacingBefore(10f);//上间距
paragraph.setSpacingAfter(10f);//下间距
} else if(2 == type){//2-标题一
paragraph.setAlignment(Element.ALIGN_JUSTIFIED); //默认
paragraph.setSpacingBefore(2f);//上间距
paragraph.setSpacingAfter(2f);//下间距
} else if(3 == type){
paragraph.setSpacingBefore(2f);//上间距
paragraph.setSpacingAfter(1f);//下间距
} else if(4 == type){//4-标题三
//paragraph.setAlignment(Element.ALIGN_RIGHT);//右对齐
paragraph.setSpacingBefore(2f);//上间距
paragraph.setSpacingAfter(2f);//下间距
} else if(5 == type){
paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
paragraph.setFirstLineIndent(24);//首行缩进
paragraph.setSpacingBefore(1f);//上间距
paragraph.setSpacingAfter(1f);//下间距
} else if(6 == type){//左对齐
paragraph.setAlignment(Element.ALIGN_LEFT);
paragraph.setSpacingBefore(1f);//上间距
paragraph.setSpacingAfter(1f);//下间距
} else if(7 == type){//右对齐
paragraph.setAlignment(Element.ALIGN_RIGHT);
paragraph.setSpacingBefore(2f);//上间距
paragraph.setSpacingAfter(2f);//下间距
}else if(8 == type){//右对齐
paragraph.setAlignment(Element.ALIGN_CENTER);
paragraph.setSpacingBefore(2f);//上间距
paragraph.setSpacingAfter(2f);//下间距
}else if(21 == type){//右对齐
paragraph.setAlignment(Element.ALIGN_LEFT);
paragraph.setSpacingBefore(2f);//上间距
paragraph.setSpacingAfter(2f);//下间距
paragraph.setIndentationLeft(50);//整体缩进左边
paragraph.setIndentationRight(50);
}
//paragraph.setIndentationLeft(50);//整体缩进左边
//paragraph.setFirstLineIndent(40);//首行缩进
return paragraph;
}
}

该博客分享了一个使用iTextPDF库在Java中生成PDF文件并返回URL供前端直接打印的简单功能。代码示例展示了如何创建PDF表格,设置字体和样式,并处理IE浏览器的兼容性问题。此外,还提供了字体工具类以支持中文显示。
2144

被折叠的 条评论
为什么被折叠?



