秋·风

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  278 随笔 :: 0 文章 :: 308 评论 :: 20万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

      lazarus linux下使用powerPDF中文乱码按网上的修改方法还是存在出现乱码问题,经跟踪powerpdf源码,除启用PRreort.pas {$DEFINE USE_GBFONTS}外,还要修改pdfdoc.pas TPdfCanvas.ShowText(const s: string),并添加LConvEncoding单元,使用UTF8ToCP936将UTF8转为CP936。

复制代码
pdfdoc.pas:
procedure
TPdfCanvas.ShowText(const s: string); var FString: string; begin if _HasMultiByteString(s) then FString := '<' + _StrToHex(s) + '>'; else FString := '(' + _EscapeText(s) + ')'; WriteString(FString + ' Tj'#10); end;
复制代码

改为:

复制代码
uses LConvEncoding;//添加这个单元

procedure
TPdfCanvas.ShowText(const s: string); var FString: string; begin FString := '<' + _StrToHex(LConvEncoding.UTF8ToCP936(s,true)) + '>'; WriteString(FString + ' Tj'#10); end;
复制代码

       按上面的方法就可以解决中文乱码的问题,但存在英文和中文字体等宽的问题,要解决英文宽度和中文等宽的问题还需在pdfGBFonts.pas增加英文字符宽度定义及procedure TPdfGBFixedFont.AddDescendantFontItem(ADescendantFont: TPdfDictionary)修改为:

复制代码

const
//2022-04-06 LBZ 增加字符宽度定义,解决英文和中文等宽的问题

FIXED_GB_W_ARRAY: array[32..200] of Integer = (
600,600,600,600,600,600,600,600,600,600,600,600,600,600,
600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,
600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,
600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,
600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,
600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,
600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,
600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,
600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,
600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,600,
600,600,600,600,600,600,600,600,600,600,600);

 
procedure TPdfGBFixedFont.AddDescendantFontItem(ADescendantFont: TPdfDictionary);
var
  FWidths,FWidthsRoot: TPdfArray;
begin
  FWidths := TPdfArray.CreateNumArray(nil, CIDTYPE2_GB_FONT_WIDTH_ARRAY);
  ADescendantFont.AddItem('W', FWidths);
 //以下是修正英文字体宽度 2022.04.06 LBZ

   FWidthsRoot := TPdfArray.CreateArray(nil);
   FWidthsRoot.AddItem(TPdfNumber.CreateNumber(780));
   FWidths := TPdfArray.CreateNumArray(nil,FIXED_GB_W_ARRAY);
   FWidthsRoot.AddItem(FWidths);
   ADescendantFont.AddItem('W', FWidthsRoot);
   //以上是修正英文字体宽度 2022.04.06 LBZ

end;
复制代码

   修改后的powerpdf在windows和linux都可以正确保存含中文的pdf,也解决了英文宽度和中文等宽的问题。

 解决英文与中文等宽后的PDF:

 

修改后的PowerPDF下载链接:

https://pan.baidu.com/s/1UzmzyAB1mqegyzUasSiLtg
提取码: vvqn

 

posted on   秋·风  阅读(562)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示