将pdf转成图片时,文字没法显示

26 篇文章 0 订阅
该博客介绍了在CentOS系统中,由于缺少字体导致PDF转图片时文字无法正常显示的问题。解决方案包括从Windows上传字体到CentOS,并在代码中根据系统类型加载字体。此外,还提供了Java代码示例,展示如何使用PDFBox库进行PDF转图片操作,并处理中文乱码。同时,博客还提到了在CentOS上安装中文字体的方法。
摘要由CSDN通过智能技术生成

背景:系统在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();
        }
    }
	
	
}

 

补充:pdfbox 转图片中文乱码处理

centos7安装中文字体

centos7安装中文字体

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值