linux 服务器Excel转PDF中文乱码解决办法

当遇到Linux服务器上Excel转换为PDF时中文显示为乱码,通常是由于缺少字体导致。解决方法包括安装font-config工具,将Windows下的simsun.ttc和simhei.ttf等中文字体文件上传至/usr/share/fonts/chinese/TrueType/目录,然后运行fc-cache命令更新字体缓存,重启服务(如tomcat)以应用更改。
摘要由CSDN通过智能技术生成

linux 服务器Excel转PDF中文乱码解决办法
乱码是由于缺少字体导致
先确定是否安装font-config工具

没有先安装fc工具
下载font-config安装包

官方下载地址:[https://almalinux.pkgs.org/8/almalinux-baseos-aarch64/fontconfig-2.13.1-4.el8.aarch64.rpm.html](https://almalinux.pkgs.org/8/almalinux-baseos-aarch64/fontconfig-2.13.1-4.el8.aarch64.rpm.html)

执行以下命令安装:

rpm -ivh fontconfig-2.13.1-4.el8.aarch64.rpm --nodeps --force

进入目录,没有创建

cd /usr/share/fonts/chinese/TrueType/

在Windows机器的 C:\Windows\Fonts 目录下找到相应的字体上传到该目录
simsun.ttc (新宋体)和 simsun.ttf(宋体),黑体为simhei.ttf

最后在此目录下,执行 fc-cache 或 fc-cache -fv

重启tomcat

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中将Excel换为PDF时,可能会遇到中文乱码的问题。这是因为在处理中文字符时,默认的字符集可能不正确。为了解决中文乱码问题,你可以指定正确的字符集。 在使用iText库将Excel换为PDF时,你可以使用`BaseFont`类指定中文字体。下面是一个修改后的示例代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; public class ExcelToPdfConverter { public static void main(String[] args) { String excelFilePath = "path/to/excel.xlsx"; String pdfFilePath = "path/to/output.pdf"; try { // 加载Excel文件 FileInputStream excelFile = new FileInputStream(new File(excelFilePath)); Workbook workbook = new XSSFWorkbook(excelFile); Sheet sheet = workbook.getSheetAt(0); // 创建PDF文档 Document document = new Document(); OutputStream outputStream = new FileOutputStream(new File(pdfFilePath)); PdfWriter.getInstance(document, outputStream); document.open(); // 指定中文字体 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); com.itextpdf.text.Font fontChinese = new com.itextpdf.text.Font(bfChinese, 12, com.itextpdf.text.Font.NORMAL); // 读取Excel中的内容并写入PDF for (Row row : sheet) { for (Cell cell : row) { String cellValue = ""; if (cell.getCellType() == CellType.STRING) { cellValue = cell.getStringCellValue(); } else if (cell.getCellType() == CellType.NUMERIC) { cellValue = String.valueOf(cell.getNumericCellValue()); } Paragraph paragraph = new Paragraph(cellValue, fontChinese); document.add(paragraph); } } // 关闭文档和文件流 document.close(); outputStream.close(); excelFile.close(); System.out.println("Excel换为PDF成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 在这个示例中,我使用了宋体字体(STSong-Light)来显示中文字符。你可以根据需要替换这个字体。在创建`Paragraph`对象时,我将字体参数设置为`fontChinese`,以确保文本以正确的中文字体显示。 通过这种方式,你应该能够解决Excel换为PDF时的中文乱码问题。如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值