fastreport(FastReport VCL 2023_1_3)在linux导出pdf中文乱码(lazarus)
2024-11-24:
修正在深度Linux导出pdf时显示“Cant swap font”导致导出失败的问题。
原因:
1.导出时没找到对应字体的linux字体目录
2.设置的字体名称不能用中文,需转换为对应的英文字体名称
修改方法:
1.打开\frxExportPDFHelpers.pas,移到initialization,添加红色代码.
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(暂时支持仿宋、楷体、幼圆、黑体、宋体,其他字体可以自己添加).。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
2021-11-18 银河麒麟(aarch64)安装Lazarus步骤