* * *

Author Topic: Generate PDF with JPFpdf  (Read 1005 times)

menegaz

  • New member
  • *
  • Posts: 7
Generate PDF with JPFpdf
« on: April 06, 2018, 04:29:55 pm »
In the code below, the accented words in the generated pdf are not coming out correctly. What can it be?

procedure TFrm_cad_auxiliares.SpeedButton2Click(Sender: TObject);
begin
  nomerel:='Área de Atuação';
  pdf:=Tpdf.Create(poPortrait,puMM,pfA4);
  pdf.SetDisplayMode(dmZoom);
  pdf.AddPage;
  pdf.SetFont(ffTimes,fsNormal,10);
  pdf.SetX(15);
  pdf.Cell(0,0,'Sistema Auditor');
  pdf.Ln(3);
  pdf.SetX(15);
  pdf.Cell(0,0,nomerel);
  pdf.Line(15,16,200,16);
  pdf.Ln(5);
  pdf.SaveToFile('d:\teste.pdf');
  pdf.Free;
end; 

Àrea de Atuação is shown as Õrea de Atuação


Tanks.

engkin

  • Hero Member
  • *****
  • Posts: 1972
Re: Generate PDF with JPFpdf
« Reply #1 on: April 06, 2018, 07:21:33 pm »
Try calling SetUTF8(True). From its README file:
Quote
This is version 1.33 Stable of the library "Free JPDF Pascal".

New's in Version 1.33:
- Added the method SetUTF8 to support UTF8. This method automatically applies the text function UTF8Decode. See the end of the second page of the PDF sample.
« Last Edit: April 06, 2018, 07:30:23 pm by engkin »

menegaz

  • New member
  • *
  • Posts: 7
Re: Generate PDF with JPFpdf
« Reply #2 on: April 06, 2018, 08:10:33 pm »
I changed the code:

  nomerel:='Área de Atuação';
  pdf:=Tpdf.Create(poPortrait,puMM,pfA4);
  pdf.SetUTF8(true);
  pdf.AliasNbPages('+n+');
  pdf.SetDisplayMode(dmZoom);

  pdf.AddPage;
  pdf.SetFont(ffTimes,fsNormal,10);
  pdf.SetX(15);
  pdf.Cell(0,0,'Sistema Auditor');
  pdf.Ln(3);
  pdf.SetX(15);
  pdf.Cell(0,0,nomerel);
  pdf.Line(15,16,200,16);
  pdf.Ln(5);
  pdf.SetX(35);
  pdf.Writer(5,'Área de Atuação');

  pdf.SaveToFile('d:\teste.pdf');
  pdf.Free;   

but the error continued.

I am using Windows 7 64bits, Lazarus 1.8.2

Thanks engkin.

engkin

  • Hero Member
  • *****
  • Posts: 1972
Re: Generate PDF with JPFpdf
« Reply #3 on: April 06, 2018, 11:08:10 pm »
It came with a sample PDF thats shows the effect of using SetUTF8. 

When I opened the PDF file using Lazarus with UTF8 encoding. It contains:
Quote
q 0 g BT 11 -222.942 Td (SetUTF8\(False\) = ) Tj ET Q
BT /F5 5.64 Tf ET
q 0 g BT 68.53 -222.942 Td (Avião Índio Carroça) Tj ET Q
BT /F4 5.64 Tf ET
q 0 g BT 11 -229.442 Td (SetUTF8\(True\) = ) Tj ET Q
BT /F5 5.64 Tf ET
q 0 g BT 65.14 -229.442 Td (Avi?o ?ndio Carro?a) Tj ET Q

Using CP1252 to display the same file:
Quote
q 0 g BT 11 -222.942 Td (SetUTF8\(False\) = ) Tj ET Q
BT /F5 5.64 Tf ET
q 0 g BT 68.53 -222.942 Td (Avião Índio Carroça) Tj ET Q
BT /F4 5.64 Tf ET
q 0 g BT 11 -229.442 Td (SetUTF8\(True\) = ) Tj ET Q
BT /F5 5.64 Tf ET
q 0 g BT 65.14 -229.442 Td (Avião Índio Carroça) Tj ET Q

My conclusion is that text with SetUTF8(True) is ANSI encoded.

Try the following sample:
Code: Pascal  [Select]
  1. program testproj;
  2.  
  3. {$mode objfpc}{$H+}
  4. { $Codepage UTF8}
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   libjpfpdf{, sysutils, LazUTF8};
  10.  
  11.  
  12. var
  13.   pdf: TJPFpdf;
  14.   nomerel: String;
  15. begin
  16.   DefaultSystemCodePage := 1252;
  17.   nomerel:='Área de Atuação';
  18.   pdf:=TJPFpdf.Create(poPortrait,puMM,pfA4);
  19.   pdf.SetUTF8(true);
  20.   pdf.AliasNbPages('+n+');
  21.   pdf.SetDisplayMode(dmZoom);
  22.  
  23.   pdf.AddPage;
  24.   pdf.SetFont(ffTimes,fsNormal,10);
  25.   pdf.SetX(15);
  26.   pdf.Cell(0,0,'Sistema Auditor');
  27.   pdf.Ln(3);
  28.   pdf.SetX(15);
  29.   pdf.Cell(0,0,nomerel);
  30.   pdf.Line(15,16,200,16);
  31.   pdf.Ln(5);
  32.   pdf.SetFont(ffTimes,fsNormal,10);
  33.   pdf.SetX(35);
  34.   pdf.Writer(5,'Área de Atuação');
  35.  
  36.   pdf.SaveToFile('teste.pdf');
  37.   pdf.Free;
  38. end.

I don't think JPFpdf supports Unicode. Not the copy I have, anyway.

menegaz

  • New member
  • *
  • Posts: 7
Re: Generate PDF with JPFpdf
« Reply #4 on: April 09, 2018, 10:43:37 pm »
 :D :D :D

It worked correctly.

Thank you engkin.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus