背景:系统在Windows系统正常的将pdf转成了图片,但是在centos系统上,文字没法显示。
问题所在:centos系统上缺少字体。
解决方案:
1、将字体从Windows上上传至centos。(c盘下载,rz命令)
2、代码层面
/**
* 获取中文字体位置
* @return
*/
private static String getChineseFont(){
//宋体(对应css中的 属性 font-family: SimSun; /*宋体*/)
String font1 ="C:/Windows/Fonts/simsun.ttc";
//判断系统类型,加载字体文件
if (SystemTypeUtils.getSystem()==SystemTypeUtils.LINUX) {
font1="/usr/share/fonts/zhongwen/simsun.ttc";
}
if(!new File(font1).exists()){
throw new RuntimeException("字体文件不存在,影响导出pdf中文显示!"+font1);
}
return font1;
}
public class SystemTypeUtils {
public final static int LINUX=1;
public final static int WINDOWS=2;
/**
* @Des:判断系统类型
*/
public static int getSystem(){
//判断系统类型
java.util.Properties prop = System.getProperties();
String osName = prop.getProperty("os.name").toLowerCase();
System.out.println("SystemTypeUtils系统类型:"+osName);
if (osName.indexOf("linux")>-1) {
return LINUX;
}
return WINDOWS ;
}
}
生成pdf文件使用字体时的部分代码片段
//新建文档对象,页大小为A4纸,然后设置4个边距
Document document = new Document(PageSize.A4,20,20,30,30);
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(outputPath));
document.open();
//字体
String font_cn = getChineseFont();
BaseFont baseFont = BaseFont.createFont(font_cn+",1", //注意这里有一个,1
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//字体对象
Font size14font = new Font(baseFont,14,Font.NORMAL); //大小为14的正常字体
pdf转图片代码
<!-- pdf转图片 -->
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
/**
* @Desc Java pdf文件转图片
*/
public class JavaPdfToImg {
public static void main(String[] args) {
pdf2png("E:", "test1", "jpg");
}
/**
* 转换全部的pdf
* @param fileAddress 文件地址
* @param filename PDF文件名
* @param type 图片类型
*/
public static void pdf2png(String fileAddress,String filename,String type) {
// 将pdf装图片 并且自定义图片得格式大小
File file = new File(fileAddress+"\\"+filename+".pdf");
try {
PDDocument doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI
// BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
ImageIO.write(image, type, new File(fileAddress+"\\"+filename+"_"+(i+1)+"."+type));
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static String pdf2png(String path,String type,String domain,String userId,String name) {
// 将pdf装图片 并且自定义图片得格式大小
File file = new File(path+".pdf");
try {
PDDocument doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI
// BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
ImageIO.write(image, type, new File(path+"."+type));
}
File foder = new File(path+"."+type);
if (foder.exists()) {
return UploadFileToCloud.uploadFileToTencent(domain, foder,userId,name);
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
/**
*自由确定起始页和终止页
* @param fileAddress 文件地址
* @param filename pdf文件名
* @param indexOfStart 开始页 开始转换的页码,从0开始
* @param indexOfEnd 结束页 停止转换的页码,-1为全部
* @param type 图片类型
*/
public static void pdf2png(String fileAddress,String filename,int indexOfStart,int indexOfEnd,String type) {
// 将pdf装图片 并且自定义图片得格式大小
File file = new File(fileAddress+"\\"+filename+".pdf");
try {
PDDocument doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = indexOfStart; i < indexOfEnd; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI
// BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
ImageIO.write(image, type, new File(fileAddress+"\\"+filename+"_"+(i+1)+"."+type));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}