秋·风

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  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
fastreport(FastReport VCL 2023_1_3)在linux导出pdf中文乱码(lazarus) 
2024-11-24:
修正在深度Linux导出pdf时显示“Cant swap font”导致导出失败的问题。

原因:
1.导出时没找到对应字体的linux字体目录
2.设置的字体名称不能用中文,需转换为对应的英文字体名称
修改方法:
1.打开\frxExportPDFHelpers.pas,移到initialization,添加红色代码.
复制代码
initialization

  EnableTpPtCorrection;

  PDFFontSimulationList := TPDFFontSimulationList.Create;
  AddStyleSimulation(#$FF2D#$FF33#$0020#$30B4#$30B7#$30C3#$30AF, [fsBold, fsItalic]);
  AddStyleSimulation(#$FF2D#$FF33#$0020#$FF30#$30B4#$30B7#$30C3#$30AF, [fsBold, fsItalic]);
  AddStyleSimulation(#$FF2D#$FF33#$0020#$660E#$671D, [fsBold, fsItalic]);
  AddStyleSimulation(#$FF2D#$FF33#$0020#$FF30#$660E#$671D, [fsBold, fsItalic]);
  AddStyleSimulation('KaiTi', [fsBold, fsItalic]); //lbz+
  AddStyleSimulation('SimHei', [fsBold, fsItalic]); //lbz+
  AddStyleSimulation('YouYuan', [fsBold, fsItalic]); //lbz+
  AddStyleSimulation('FangSong', [fsBold, fsItalic]); //lbz+
  AddStyleSimulation('STSong', [fsBold, fsItalic]); //lbz+
  AddStyleSimulation('FangSong_GB2312', [fsBold, fsItalic]); //lbz+
  AddStyleSimulation('KaiTi_GB2312', [fsBold, fsItalic]); //lbz+
  AddStyleSimulation('STZhongsong', [fsBold, fsItalic]); //lbz+
  AddStyleSimulation('MS UI Gothic', [fsBold, fsItalic]);
  AddStyleSimulation('Arial Black', [fsBold, fsItalic]);
  AddStyleSimulation('Tahoma', [fsItalic]);

  PDFMonospacedFontCorrectionList := TPDFMonospacedFontCorrectionList.Create;
  StretchFont('Consolas',  6, 0.986); StretchFont('Consolas',  7, 1.002);
  StretchFont('Consolas',  8, 1.012); StretchFont('Consolas',  9, 0.997);
  StretchFont('Consolas', 10, 1.008); StretchFont('Consolas', 11, 1.016);
  StretchFont('Consolas', 12, 1.002); StretchFont('Consolas', 13, 0.994);
  StretchFont('Consolas', 14, 1.002); StretchFont('Consolas', 15, 1.007);

  StretchFont('Courier New',  6, 1.004); StretchFont('Courier New',  7, 1.005);
  StretchFont('Courier New',  8, 1.002); StretchFont('Courier New',  9, 1.003);
  StretchFont('Courier New', 10, 1.004); StretchFont('Courier New', 11, 1.002);
  StretchFont('Courier New', 12, 1.004); StretchFont('Courier New', 13, 1.004);
  StretchFont('Courier New', 14, 1.004); StretchFont('Courier New', 15, 1.003);

  StretchFont('Lucida Console',  8, 0.998); StretchFont('Lucida Console',  9, 0.998);
  StretchFont('Lucida Console', 10, 0.998); StretchFont('Lucida Console', 11, 0.998);
  StretchFont('Lucida Console', 12, 0.999); StretchFont('Lucida Console', 13, 0.999);
  StretchFont('Lucida Console', 14, 1.000); StretchFont('Lucida Console', 15, 0.998);

  EMFPDFFontCorrectionList := TEMFPDFFontCorrectionList.Create;
  StretchFont('Arial', 1.0);
  StretchFont('Calibri', 1.0);
  StretchFont('Calibri Light', 1.0);
  StretchFont('Cambria', 1.0);
  StretchFont('Garamond', 1.0);
  StretchFont('Georgia', 1.0);
  StretchFont('Gotham', 1.0);
  StretchFont('Gotham Light', 1.0);
  StretchFont('Meiryo', 1.0);
  StretchFont('Tahoma', 1.0);
  StretchFont('Times New Roman', 1.0);
  StretchFont('Trebuchet MS', 0.998);
  StretchFont('Verdana',  0.999);

finalization

  PDFFontSimulationList.Free;
  PDFMonospacedFontCorrectionList.Free;
  EMFPDFFontCorrectionList.Free;

end.
复制代码

2.打开frxLinuxFonts.pas

1)注释掉:

{.$DEFINE UseExtraLists}//use always

2)添加字体目录:

复制代码
  procedure FillFontPathes(var AList: TStringList);  //TODO: Rewrite to parse  '/etc/fonts/fonts.conf'
  begin
   {$IFDEF linux}
    AList.Add('/usr/share/cups/fonts/');
    AList.Add('/usr/share/fonts/truetype/');
    AList.Add('/usr/share/fonts/msttcore/');
    AList.Add('/usr/local/lib/X11/fonts/');
    AList.Add(GetUserDir+'.local/share/fonts');  //lbz
    AList.Add(GetUserDir + '.fonts/');
   {$ENDIF}
   {$IFDEF LCLCarbon}
    AList.Add('/Library/Fonts/');
    AList.Add('/System/Library/Fonts/');
    AList.Add('/Network/Library/Fonts/');
    AList.Add('~/Library/Fonts/');
   {$ENDIF}
   {$IFDEF LCLCocoa}
    AList.Add('/Library/Fonts/');
    AList.Add('/System/Library/Fonts/');
    AList.Add('/Network/Library/Fonts/');
    AList.Add('~/Library/Fonts/');
   {$ENDIF}
  end;
复制代码

3)转换中文字体名称

复制代码
function TLFonts.GetFontName(Font: TFont): String;
var
  familyItem: TCustomFamilyCollectionItem;
  i: Integer;
begin
  {$IFDEF UseExtraLists}
  if (UsedFonts.Find(Font.Name, i)) then
  begin
    Result := TCustomFamilyCollectionItem(UsedFonts.Objects[i]).FamilyName;
    Exit;
  end;
  {$ENDIF}
  //familyItem := FontCollection.Family[Font.Name];
  //if familyItem = nil then
  //begin
  //  familyItem := FontCollection.Family[SwapFontName];
  //  if familyItem = nil then
  //    raise Exception.Create('Cant swap font');
  //end;
  //Result := familyItem.FamilyName;
  Result := Font.Name;
  if Result='FreeSans' then Result:='FangSong';
  if Font.Name='仿宋' Then Result:='FangSong';
  if Font.Name='楷体' Then Result:='KaiTi';
  if Font.Name='幼圆' Then Result:='YouYuan';
  if Font.Name='黑体' Then Result:='SimHei';
  if Font.Name='宋体' Then Result:='STSong';
  if Font.Name='仿宋_GB2312' Then Result:='FangSong_GB2312';
  if Font.Name='楷体_GB2312' Then Result:='KaiTi_GB2312';
  if Font.Name='华文中宋' Then Result:='STZhongsong';

  {$IFDEF UseExtraLists}
  UsedFonts.AddObject(Font.Name, familyItem);
  if (familyItem.FamilyName <> Font.Name) and not (UsedFonts.Find(familyItem.FamilyName, i)) then
    UsedFonts.AddObject(familyItem.FamilyName, familyItem);
  {$ENDIF}
end;
复制代码

 

 

修改后重新编译安装frxe_lazarus.lpk
最后重新编译应用就可以正确导出包含中文的PDF(暂时支持仿宋、楷体、幼圆、黑体、宋体,其他字体可以自己添加).。

 

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