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

libharu 中文问题

参考:Haru Free PDF Library

1.libharu两种简体

(SimSun,SimHei)

2.libharu一种繁体

(MingLiU) 

3.下面是结合xlnt生成pdf

#include <iostream>
#include <xlnt/xlnt.hpp>
#include <Windows.h>
#include <wchar.h>
#include <hpdf/hpdf.h>
#include <iostream>using namespace std;//UTF-8编码格式字符串 转普通sting类型
std::string UTF8_To_string(const std::string& str)
{int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);wchar_t* pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴memset(pwBuf, 0, nwLen * 2 + 2);MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);char* pBuf = new char[nLen + 1];memset(pBuf, 0, nLen + 1);WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);std::string retStr = pBuf;delete[]pBuf;delete[]pwBuf;pBuf = NULL;pwBuf = NULL;return retStr;
}void error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no, void* user_data) {std::cerr << "Error: " << error_no << ", detail: " << detail_no << std::endl;
}int main() {// 加载 Excel 文件xlnt::workbook wb;try {wb.load("example.xlsx");}catch (const std::exception& e) {std::cerr << "无法加载 Excel 文件: " << e.what() << std::endl;return 1;}// 创建 PDF 文档HPDF_Doc pdf = HPDF_New(error_handler, nullptr);if (!pdf) {std::cerr << "无法创建 PDF 文档" << std::endl;return 1;}HPDF_UseCNSFonts(pdf);HPDF_UseCNSEncodings(pdf);HPDF_Font hfont = HPDF_GetFont(pdf, "SimSun", "GB-EUC-H");HPDF_SetCompressionMode(pdf, HPDF_COMP_ALL);HPDF_Page page = HPDF_AddPage(pdf);HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);HPDF_Page_SetFontAndSize(page, hfont, 12);// 读取 Excel 内容并写入 PDFfloat y_position = 800;for (auto sheet : wb) {std::string sheet_title_gbk = UTF8_To_string(sheet.title());HPDF_Page_BeginText(page);HPDF_Page_MoveTextPos(page, 50, y_position);HPDF_Page_ShowText(page, sheet_title_gbk.c_str());HPDF_Page_EndText(page);y_position -= 20;for (auto row : sheet.rows()) {std::string row_text;for (auto cell : row) {std::string cell_content_gbk = UTF8_To_string(cell.to_string());row_text += cell_content_gbk + "\t";}HPDF_Page_BeginText(page);HPDF_Page_MoveTextPos(page, 50, y_position);HPDF_Page_ShowText(page, row_text.c_str());HPDF_Page_EndText(page);y_position -= 20;if (y_position < 50) { // 创建新页page = HPDF_AddPage(pdf);HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);HPDF_Page_SetFontAndSize(page, hfont, 12);y_position = 800;}}}// 保存 PDFHPDF_SaveToFile(pdf, "output.pdf");HPDF_Free(pdf);std::cout << "Excel 已成功转换为 PDF。" << std::endl;return 0;
}

其中关键点在于:

    HPDF_UseCNSFonts(pdf);
    HPDF_UseCNSEncodings(pdf);
    HPDF_Font hfont = HPDF_GetFont(pdf, "SimSun", "GB-EUC-H");

样例与格式就参考官方文档


http://www.mrgr.cn/news/59329.html

相关文章:

  • 君正 T31 型号芯片架构模块介绍
  • 在平面模型上提取凹多边形的点云处理
  • 【Canvas与桌面】文山甲密铺桌面壁纸 1920*1080
  • 编写一个简单的Iinput_dev框架
  • 量子变分算法 (python qiskit)
  • 《Contrastive Language-Image Pre-Training with Knowledge Graphs》中文校对版
  • 基于JAVA+SpringBoot+Vue的华府便利店信息管理系统
  • 最新PHP网盘搜索引擎系统源码 附教程
  • 个体化神经调控 Neurolnavigation介绍
  • 基于SSM“多鱼”旧物交易平台的设计与实现
  • shodan2:绕过shodan高级会员限制+metasploit批量验证漏洞
  • Python浪漫之画一个圆月亮
  • 6.Linux按键驱动-阻塞与非阻塞
  • 安全知识见闻-脚本语言对与安全的重要性
  • 使用FRP搭建内网穿透服务(新版toml配置文件,搭配反向代理方便内网网站访问)【使用frp搭建内网穿透】
  • 索引的使用和优化
  • 基于SSM“毛毛宠物店”宠物信息交流平台的设计与实现
  • Linux中C/C++程序编译过程与动静态链接库概述
  • LeetCode438.找到字符串中所有字母异位词
  • Macos系统使用wine安装window的exe软件