2018年11月11日 星期日

Using FastReport in IntraWeb

 Using FastReport in IntraWeb

This text is a little guide of how to create a pdf document basic in a FastReport template from a IntraWeb server program.

FastReport is a powerfull and popular component library to create reports.
If you are an IntraWeb programmer, then maybe you think in obtain pretty pdf reports, by reusing old desktop reports.

Really is very easy, but if you don’t use some tricks, then it will be difficult for you.

Let’s go to see how create a normal pdf report in a normal Delphi deskop application:


var
pdf: TfrxPDFExport;
frreport: TfrxReport;

procedure CrearReport (const nam,resul:String);
begin
try
frReport.LoadFromFile(nam);
pdf.ShowDialog:=false; //Turn off dialog
pdf.FileName:=resu_;
frReport.PrepareReport(true);
frReport.export (pdf)
end;

This procedure receive as parameter; nam ( which is the template name) and resul which is the pdf result file name.

This procedure (or similar), works fine in a desktop program. But if we want to do the same in a IntraWeb program, to tray to create a report as response to a request command form a Web page, is possible than not work initially.

What happened? In IntraWeb, the response to the commands was processed in a thread. Is well know the problem to create a form in a thread process (not in the main application thread) the forms can be inconsistent.

FastReport need to create some forms (but invisibles) to create the reports. If we use some similar to code 1, our application will crash when intent to create the report.

What is the solution? We will generate a new thread used to synchronize the report creation.




type
TIwFastResport = CLASS(TThread)

protected
procedure Execute; override;
public
na,resu:string;
constructor Create (const nas,resus:string);
procedure crear;
end;




constructor TIwFastResport.Create (const nas,resus:string);
begin
inherited create (true);
freeonterminate:=false;
na:=nas;
resu:=resus;
end;

procedure TIwFastResport.Execute;
begin
synchronize (crear);
end;


procedure TIwFastResport.crear;
begin
CrearReport (na,resu)
end;


How to use the component ?

procedure TFormSample.ExportGen_( na:string; resu:string);
var th:TIwFastResport;
begin
th:=TIwFastResport.create(na,resu);
th.resume;
th.WaitFor;
th.Free;
WebApplication.SendFile(resu,True , 'application/binary', '');
end;


https://sites.google.com/a/miteruel.com/dojo/fastreport-and-intraweb

沒有留言:

張貼留言

Calibre_web administrator password forget

Calibre_web administrator password forget how to reset the admin password: need to specify the app.db docker exec -it calibre-web cd...