#1 2024-09-27 05:03:12

jianwt
Member
Registered: 2024-09-14
Posts: 1

mORMot2 export PDF file output Chinese string prompt error problem.

Please consult everyone, in windows10 platform lazarUS3.0, export PDF file with mORMot2 output Chinese string prompt error.

procedure TForm1.Button1Click(Sender: TObject);
var
  Pdf: TPdfDocumentGDI;
begin
  Pdf := TPdfDocumentGDI.Create;
  try
// Pdf.EmbeddedTTF:=True;
    Pdf.AddPage;
    with Pdf.VCLCanvas do
    begin
      Pen.Color := clRed;
      Pen.Width := 2;
      Brush.Color := clInfoBk;
      Rectangle(100, 100, 400, 200);
      Font.Name := '宋体';
      Font.Size := 20;
 
      TextOut(200, 120, '测试内容'); {An error is reported as long as the input is a Chinese string. How to solve it? }
      //TextOut(200, 120, 'AAAA123');  {This output is normal}
      Pen.Color := clYellow;
      Pen.Width := 5;
      MoveTo(100, 250);
      LineTo(400, 250);
    end;
    Pdf.SaveToFile(ExtractFilePath(Application.ExeName) +'ceshi.pdf');
  finally
    Pdf.Free;
  end;
end;     

The error message is:
项目project1提引异常类'External: ACCESS
VIOLATION'带有信息:
Access violation reading from address $00000000.
在文件'mormot,ui.pdf,pas'在行6167:
result :=
fUsedWide[FindOrAddUsedWidechar(aWidechar...

Offline

#2 2024-09-27 08:21:06

rvk
Member
Registered: 2022-04-14
Posts: 114

Re: mORMot2 export PDF file output Chinese string prompt error problem.

Yes, there seems to be some problems with Chinese characters (widestring conversion?) in mORMot2 in Lazarus.

This seems to work fine in Delphi (both Canvas.TextOut and Windows.TextOutW):

var
  pdf: TPdfDocumentGDI;
  S: WideString;
begin
  pdf := TPdfDocumentGDI.Create;
  try
    pdf.AddPage;

    // pdf.UseUniscribe := True; // not needed?
    // pdf.AddTrueTypeFont('SimSun'); // not needed?

    pdf.VCLCanvas.Font.Name := 'SimSun'; // 宋体
    pdf.VCLCanvas.Font.size := 20;
    S := WideString('测试内容');

    pdf.VCLCanvas.TextOut(200, 120, S);

    Windows.TextOutW(pdf.VCLCanvas.Handle, 200, 160, PWidechar(S), Length(S));

    pdf.SaveToFile(ExtractFilePath(Application.ExeName) + 'ceshi.pdf');
    ShellExecute(Application.Handle, 'open', pChar(ExtractFilePath(Application.ExeName) + 'ceshi.pdf'), '', '', SW_SHOWNORMAL);
  finally
      pdf.Free;
  end;

end;

I thought I'd try direct TPdfDocument, skipping the GDI entirely... but that also only works in Delphi and crashes on Lazarus.
(With TextOut instead of TextOutW it works but produces garbage characters logically)

var
  pdf: TPdfDocument;
  S: WideString;
begin
  pdf := TPdfDocument.Create;
  try
    pdf.AddPage;

    S := '发现双引号不正确“问题”';
    pdf.Canvas.SetFont('SimSun', 12, []);
    Pdf.Canvas.TextOutW(100, 600, PWideChar(S));

    pdf.SaveToFile(ExtractFilePath(Application.ExeName) + 'ceshi.pdf');
    ShellExecute(Application.Handle, 'open', pChar(ExtractFilePath(Application.ExeName) + 'ceshi.pdf'), '', '', SW_SHOWNORMAL);
  finally
      pdf.Free;
  end;
end;

So yes, definitely a problem with that code on Lazarus.

Offline

Board footer

Powered by FluxBB