Creating and showing PDF

<< Click to Display Table of Contents >>

Navigation:  Forum >

Creating and showing PDF

Forum link

 


 

06-14-2022, 03:39 PM:

 

Hello,

 

I am using IW 15.2.57 and Delphi 11.1.

 

I would like to show a dinamically created pdf after a user select an invoice in a TIWjQDBGrid.

 

To create the pdf I am using the export feature of Fast-Report reporting tool.

 

I am using the following code:

 

Code:

 

&nbsp;&nbsp;&nbsp;&nbsp;var AppPath: string := WebApplication.ApplicationPath;

 

&nbsp;&nbsp;&nbsp;&nbsp;var CacheFileName: string := TIWAppCache.NewTempFileName();

 

&nbsp;&nbsp;&nbsp;&nbsp;QryDeliveries.Close;

 

&nbsp;&nbsp;&nbsp;&nbsp;QryOrder.Close;

 

&nbsp;&nbsp;&nbsp;&nbsp;QryOrder.ParamByName('OrderNo').AsString := FSelectedOrderNo;

 

&nbsp;&nbsp;&nbsp;&nbsp;QryOrder.Open;

 

&nbsp;&nbsp;&nbsp;&nbsp;QryDeliveries.Open();

 

&nbsp;&nbsp;&nbsp;&nbsp;Report.EngineOptions.EnableThreadSafe := True;

 

&nbsp;&nbsp;&nbsp;&nbsp;Report.EngineOptions.DestroyForms := False;

 

&nbsp;&nbsp;&nbsp;&nbsp;Report.LoadFromFile(Format('%sreports\Spedizioni.fr3', [AppPath]));

 

&nbsp;&nbsp;&nbsp;&nbsp;try

 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Report.PrepareReport;

 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PDFExport.FileName := CacheFileName;

 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Report.Export(PDFExport);

 

&nbsp;&nbsp;&nbsp;&nbsp;finally

 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Report.Clear;

 

&nbsp;&nbsp;&nbsp;&nbsp;end;

 

&nbsp;&nbsp;&nbsp;&nbsp;var Url: string := TIWAppCache.AddFileToCache(GGetWebApplicationThreadVar(), CacheFileName, 'application/pdf', ctOneTime);

 

&nbsp;&nbsp;&nbsp;&nbsp;WebApplication.NewWindow(Url);

 

 

 

At the beginning selecting different invoice the pdf is correctly shown but after some selection the pdf report the same invoice without correctly update the invoice data basing on the selected invoice.

 

What I am doing wrong?

 

Thank you,

 

Davide

 


 

06-15-2022, 02:49 AM:

 

I don't use Fast Report, but I have followed conversations here regarding threading issues with that lib. It is not intended for multi threading like a web server. The solution is to use the CGIRunner component and place the report code in a single thread project.

 

Check here:

 

https://www.atozed.com/forums/showthread...02#pid2402

 

Note that in post 9 of that thread, Jose has posted a link to sample code.

 

Dan

 


 

06-16-2022, 02:23 PM:

 

(06-15-2022, 02:49 AM)DanBarclay Wrote: [ -> ]I don't use Fast Report, but I have followed conversations here regarding threading issues with that lib.  It is not intended for multi threading like a web server.  The solution is to use the CGIRunner component and place the report code in a single thread project.

 

Check here:

 

  https://www.atozed.com/forums/showthread...02#pid2402

 

Note that in post 9 of that thread, Jose has posted a link to sample code.

 

Dan

 

There is no threading issues with fastreport if you set all of these settings.

 

      // this code is very important and needs to be called right after the report is created.

 

      lFrxReport.EngineOptions.EnableThreadSafe    := True;

 

      lFrxReport.EngineOptions.UseGlobalDataSetList := False;

 

      lFrxReport.ReportOptions.Compressed          := True;

 

      lFrxReport.EngineOptions.SilentMode          := True;

 

      lFrxReport.EngineOptions.DestroyForms        := False;

 

      lFrxReport.EngineOptions.UseFileCache        := False;

 

      lFrxReport.ShowProgress                      := False;

 

 

 

Here is also a procedure for using ReportBuilder if anyone needs it.

 

procedure fxProcess_Report(ARBreport:TppReport;Output_typetring; Send_Stream:boolean=false;AFileNametring='';APdfPasswordtring='');

 

var lFileName,

 

lURL,

 

lFileDirtring;

 

lMS : TMemoryStream;

 

lPDFDevice: TppPDFDevice;

 

begin

 

try

 

with ARBreport do

 

begin

 

AllowPrintToFile := False;

 

ShowPrintDialog := False;

 

TextFileName := lFileDir;

 

DeviceType := 'PDF';

 

ModalCancelDialog := false;

 

ModalPreview := false;

 

ShowCancelDialog := false;

 

lMS := TMemoryStream.Create;

 

lPDFDevice := TppPDFDevice.Create(nil);

 

try

 

lPDFDevice.PDFSettings := ARBreport.PDFSettings;

 

lPDFDevice.OutPutSTream := lMS;

 

lPDFDevice.Publisher := ARBreport.publisher;

 

ARBreport.PrintToDevices;

 

lURL := IWappCache.TIWAppCache.StreamToCacheFile(WebApplication, lMS, TIWMimeTypes.GetAsString('.' + 'PDF'), ctSession);

 

WebApplication.NewWindow( lURL

 

, 'Report'

 

, 0

 

, 0

 

, [woResizable, woDetectBlock]

 

);

 

finally

 

lPDFDevice.free;

 

lMS.free;

 

end;

 

end;

 

except

 

raise;

 

end;

 

end;