Lazarus

Programming => General => Topic started by: xinyiman on July 03, 2020, 01:40:35 pm

Title: (SOLVED) fpreport problem
Post by: xinyiman on July 03, 2020, 01:40:35 pm
Hi guys, I have created a small example to work with fpreport. There is only one button to click. But as you will see the PDF does not create me as I look. Who explains what I'm wrong? It appears that it does not find fonts on the system. But from the examples it seemed to me that this was enough.
Title: Re: fpreport problem
Post by: Handoko on July 03, 2020, 09:13:29 pm
I cannot open your project. I tried to open the lpi and lpr files, but nothing happened. Maybe because I use Lazarus 2.0.8.
Title: Re: fpreport problem
Post by: xinyiman on July 06, 2020, 08:17:50 am
Lazarus 2.1.0 r62688M FPC 3.3.1 x86_64-darwin-cocoa (beta)
Title: Re: fpreport problem
Post by: af0815 on July 06, 2020, 08:26:12 am
Did you reload the the font buffer ?

Is the example project inside of the lazarus-examples working for you ?
Title: Re: fpreport problem
Post by: xinyiman on July 06, 2020, 08:45:48 am
The examples working perfectly. But my test no!
Title: Re: fpreport problem
Post by: jamie on July 06, 2020, 01:13:24 pm
I am going to shoot from the hip here.

I am not trying your app because it seems those that are using the currently RC IDE/COMPILER isn't switching on the option to make it for us older users to look at the project..

 But putting that aside. Since this is a PDF operation and you claim the examples work which I believe you and yours does not. The PDF I assume is a Printer function you are using, did you included the "Printer4Lazarus" package in your project? It needs to be there for any printer operations to work.

 Just asking
Title: Re: fpreport problem
Post by: xinyiman on July 06, 2020, 01:46:00 pm
The problem is that it cannot find the fonts. But from the example it seems that what is in the example is enough. (See code below). It's just not working.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.  
  10.   fppdf,fpjsonreport,jsonparser,
  11.   fpreportpdfexport
  12.   ;
  13.  
  14. type
  15.  
  16.   { TForm1 }
  17.  
  18.   TForm1 = class(TForm)
  19.     Button1: TButton;
  20.     procedure Button1Click(Sender: TObject);
  21.     procedure FormCreate(Sender: TObject);
  22.   private
  23.  
  24.   public
  25.  
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. uses
  34.     fpTTF
  35.     ;
  36.  
  37.  
  38. {$R *.lfm}
  39.  
  40. { TForm1 }
  41.  
  42. procedure TForm1.Button1Click(Sender: TObject);
  43. var
  44.   filename           : string;
  45.   destinationpdf     : string;
  46.   rpt                : TFPJSONReport;
  47.   RptExporter        : TFPReportExportPDF;
  48. begin
  49.  
  50.      filename        := Application.Location {$IFDEF DARWIN}+ '../../../' {$ENDIF} + 'REPORT' + System.DirectorySeparator + 'test.json';
  51.  
  52.      destinationpdf  := Application.Location {$IFDEF DARWIN}+ '../../../' {$ENDIF} + 'REPORT' + System.DirectorySeparator + 'abc.pdf';
  53.  
  54.      try
  55.         try
  56.  
  57.            if not fileexists(filename) then
  58.               ShowMessage('Report json not exists');
  59.  
  60.           rpt := TFPJSONReport.Create(Self);
  61.  
  62.           rpt.LoadFromFile(filename);
  63.           rpt.RunReport;
  64.  
  65.           RptExporter           := TFPReportExportPDF.Create(Self);
  66.           RptExporter.FileName  := destinationpdf;
  67.           rpt.RenderReport(RptExporter);
  68.           RptExporter.SaveToFile;
  69.  
  70.         finally
  71.  
  72.           if Assigned(RptExporter) then
  73.           begin
  74.              RptExporter.Free;
  75.              RptExporter := nil
  76.           end;
  77.  
  78.           if Assigned(rpt) then
  79.           begin
  80.              rpt.Free;
  81.              rpt := nil;
  82.           end;
  83.  
  84.           if FileExists(destinationpdf) then
  85.           begin
  86.                ShowMessage('File exists: ' + destinationpdf);
  87.           end
  88.           else
  89.           begin
  90.                ShowMessage('File not exists: ' + destinationpdf);
  91.           end;
  92.  
  93.        end;
  94.      except
  95.            on E: Exception do
  96.            begin
  97.                 ShowMessage(E.Message);
  98.            end;
  99.      end;
  100.  
  101. end;
  102.  
  103. procedure TForm1.FormCreate(Sender: TObject);
  104. begin
  105.      {$IFDEF UNIX}
  106.        {$IFDEF DARWIN}
  107.               gTTFontCache.SearchPath.Add(ExtractFilePath(ParamStr(0))+'../../../../demos/fonts/');
  108.               gTTFontCache.SearchPath.Add(GetUserDir + 'Library/Fonts/');
  109.               gTTFontCache.SearchPath.Add('/Library/Fonts/');
  110.               gTTFontCache.SearchPath.Add('/System/Library/Fonts/');
  111.               gTTFontCache.SearchPath.Add('/System/Library/Fonts/');
  112.        {$ELSE}
  113.               gTTFontCache.SearchPath.Add(ExtractFilePath(ParamStr(0))+'../demos/fonts/');
  114.               gTTFontCache.SearchPath.Add(GetUserDir + '.fonts/');
  115.               gTTFontCache.SearchPath.Add('/usr/share/fonts/truetype/ubuntu-font-family/');
  116.               gTTFontCache.SearchPath.Add('/usr/share/fonts/truetype/dejavu/');
  117.        {$ENDIF}
  118.      {$ENDIF}
  119. end;
  120.  
  121. end.
  122.  
Title: Re: fpreport problem
Post by: jamie on July 06, 2020, 01:58:33 pm
Look at a working example project and check the Project inspector to see what packages it has included..

 I would bet you are missing something like "FontsForLazarus" or something on that order.

Title: Re: fpreport problem
Post by: xinyiman on July 06, 2020, 03:06:32 pm
I took a few steps forward. Now get over the problem but export to PDF doesn't work. Try pressing on button2 with the check set to true. You will see the preview of the report correctly. Then set the check to false and hit the button button2 again. An exception will come up. But I don't know how to solve it.
Title: Re: fpreport problem
Post by: af0815 on July 06, 2020, 06:57:02 pm
what exception ?
Title: Re: fpreport problem
Post by: xinyiman on July 07, 2020, 09:13:58 am
List index(-1) out of bounds.
Title: Re: fpreport problem
Post by: af0815 on July 07, 2020, 01:29:34 pm
Have you called RenderReport with a valid Exporter on the Report ?
Title: Re: fpreport problem
Post by: Pascal on July 07, 2020, 01:54:23 pm
List index(-1) out of bounds.

Congrats! You've found a bug. I am going to fix this.
Title: Re: fpreport problem
Post by: Pascal on July 07, 2020, 02:31:29 pm
See: https://bugs.freepascal.org/view.php?id=37309

When this is fixed your program runs as expected.

Btw: You can move this code to initialization part of your unit:
Code: Pascal  [Select][+][-]
  1. {$IFDEF UNIX}
  2.   {$IFDEF DARWIN}
  3.          //gTTFontCache.SearchPath.Add(ExtractFilePath(ParamStr(0))+'../../../../demos/fonts/');
  4.          gTTFontCache.SearchPath.Add(GetUserDir + 'Library/Fonts/');
  5.          gTTFontCache.SearchPath.Add('/Library/Fonts/');
  6.          gTTFontCache.SearchPath.Add('/System/Library/Fonts/');
  7.          gTTFontCache.SearchPath.Add('/System/Library/Fonts/');
  8.   {$ELSE}
  9.          gTTFontCache.SearchPath.Add(ExtractFilePath(ParamStr(0))+'../demos/fonts/');
  10.          gTTFontCache.SearchPath.Add(GetUserDir + '.fonts/');
  11.          gTTFontCache.SearchPath.Add('/usr/share/fonts/truetype/ubuntu-font-family/');
  12.          gTTFontCache.SearchPath.Add('/usr/share/fonts/truetype/dejavu/');
  13.   {$ENDIF}
  14. {$ENDIF}
  15. gTTFontCache.BuildFontCache;
Title: Re: fpreport problem
Post by: xinyiman on July 08, 2020, 11:22:29 am
Thank you  :)
Title: Re: fpreport problem
Post by: Pascal on July 09, 2020, 09:31:13 am
Fixed in r45744
Title: Re: fpreport problem
Post by: xinyiman on July 09, 2020, 10:36:56 am
Thank you
TinyPortal © 2005-2018