unit rlcomtestform;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ComObj;

type
  TfrmTestCom = class(TForm)
    Label1: TLabel;
    btnStartServer: TButton;
    StatusBar1: TStatusBar;
    pcTestFunctions: TPageControl;
    tsRML: TTabSheet;
    tsPageCatcher: TTabSheet;
    TabSheet1: TTabSheet;
    Label2: TLabel;
    txtRmlFileName: TEdit;
    cmdChooseRmlFileName: TButton;
    OpenDialog1: TOpenDialog;
    cmdConvert1: TButton;
    cmdConvert2: TButton;
    Memo1: TMemo;
    procedure btnStartServerClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cmdChooseRmlFileNameClick(Sender: TObject);
    procedure cmdConvert1Click(Sender: TObject);
    procedure cmdConvert2Click(Sender: TObject);
  private
    ReportServer: OLEVariant;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmTestCom: TfrmTestCom;

implementation

{$R *.dfm}

procedure TfrmTestCom.btnStartServerClick(Sender: TObject);
begin
  try
    Screen.Cursor := crHourglass;
  try
    ReportServer := CreateOleObject('ReportLab.ReportServer');
    StatusBar1.SimpleText := 'ReportServer is running';
    pcTestFunctions.Visible := True;
    btnStartServer.Visible := false;
  except
    MessageDlg(
      'An instance of the "ReportLab.ReportServer" COM class ' +
      'could not be created. Make sure that the ReportServer application has ' +
      'been registered using a  command line.  If you have modified the ' +
      'source of the server, make very sure all public methods and attributes ' +
      'are spelled correctly',
      mtError, [mbOk], 0);
    Application.Terminate;
  end;
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TfrmTestCom.FormCreate(Sender: TObject);
begin
    pcTestFunctions.Visible := false;

end;

procedure TfrmTestCom.cmdChooseRmlFileNameClick(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    txtRmlFileName.Text := OpenDialog1.FileName;

  end;
end;

procedure TfrmTestCom.cmdConvert1Click(Sender: TObject);
var outFileName: string;
begin
  outFileName := Copy(txtRmlFileName.Text,
                      1,
                      Length(txtRmlFileName.Text) - 4) +
                      '.pdf';
  try
    Screen.Cursor := crHourglass;
    ReportServer.convertRmlFileToPdfFile(
                      txtRmlFileName.Text,
                      outFileName);
    ShowMessage('saved ' + outFileName);
  finally
    Screen.Cursor := crDefault;
  end;


end;

procedure TfrmTestCom.cmdConvert2Click(Sender: TObject);
var
  buf: TStringList;
  rmlText, pdfText, outFileName: string;
begin
  outFileName := Copy(txtRmlFileName.Text,
                      1,
                      Length(txtRmlFileName.Text) - 4) +
                      '_mem.pdf';
  try
    Screen.Cursor := crHourglass;
    buf := TStringList.Create;
    buf.LoadFromFile(txtRmlFileName.Text);
    rmlText := buf.Text;
    pdfText := ReportServer.convertRmlToPdfInMemory(rmlText);
    Memo1.Text := pdfText;
    buf.Text := pdfText;
    buf.SaveToFile(outFileName);
    buf.Free;
    ShowMessage('saved ' + outFileName);
  finally
    Screen.Cursor := crDefault;
  end;


end;

end.
