Unigui中用Grid++report生成pdf实现打印报表

chorme,firefox这两大浏览器都自带了pdf文件阅读功能,不需要另外的插件,我们可以在unigui中利用grid++report的导出文件功能,在服务器端导出pdf文件,供前台展示及预览。

代码如下:

程序代码:

unit untPdfPrint;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIForm, uniGUIBaseClasses, uniPanel, uniURLFrame,
  Datasnap.DBClient,
  Vcl.OleServer,
  grproLib_TLB;

type
  TfrmPdfPrint = class(TUniForm)
    UniURLFrame1: TUniURLFrame;
    procedure UniFormShow(Sender: TObject);
  private
    PdfFileName: string;
    { Private declarations }
  public

    procedure CDSExportData(CDS: TClientDataSet; ReportFileName: string;
      FileName: string; FileType: GRExportType);

    { Public declarations }
  end;

function frmPdfPrint: TfrmPdfPrint;

implementation

{$R *.dfm}

uses
  MainModule, uniGUIApplication;

function frmPdfPrint: TfrmPdfPrint;
begin
  Result := TfrmPdfPrint(UniMainModule.GetFormInstance(TfrmPdfPrint));
end;

procedure TfrmPdfPrint.CDSExportData(CDS: TClientDataSet;
  ReportFileName, FileName: string; FileType: GRExportType);
var
  Grexport: IGRExportOption;
  Report1: Tgridppreport;
  cds1: TClientDataSet;
  FieldName: string;
  I: Integer;
begin
  Report1 := Tgridppreport.Create(nil);
  cds1 := TClientDataSet.Create(nil);
  try

    Report1.LoadFromFile(ReportFileName);
    cds1.Data := CDS.Data; // 复制一份cds的数据,使用复制的数据来转换为报表数据
    case FileType of
      gretPDF:  Grexport := Report1.PrepareExport(gretPDF);// 导出pdf文档

    end;

    Grexport.AbortShowOptionDlg := False; // 因为是服务器,所以不显示导出对话框,否则会有错误
    Grexport.AbortOpenFile := False; // 因为是服务器,所以导出完成后不打开导出文件.
    Grexport.FileName := FileName;
    Self.PdfFileName := FileName;

    Report1.PrepareRecordset;
    cds1.First;
    // 以下将tclientdataset数据集转化为Grid++REport数据集
    while cds1.Eof = False do
    begin
      Report1.DetailGrid.Recordset.Append;

      for I := 0 to cds1.FieldCount - 1 do
      begin
        FieldName :=   cds1.Fields[I].FieldName;
       if Report1.FieldByName(cds1.Fields[I].FieldName) <> nil  then
       begin
        Report1.FieldByDBName(cds1.Fields[I].FieldName).Value :=   cds1.Fields[I].Value;

       end;

      end;
      Report1.DetailGrid.Recordset.Post;
      cds1.Next;
    end;

    Report1.Export(False); // 执行导出
    Report1.UnprepareExport;
  finally
    Report1.Free;
    cds1.Free;
  end;

end;

procedure TfrmPdfPrint.UniFormShow(Sender: TObject);
begin
  UniURLFrame1.URL := PdfFileName;
end;

end.

TfrmPdfPrint文件:

object frmPdfPrint: TfrmPdfPrint
  Left = 0
  Top = 0
  Caption = #25171#21360
  ClientHeight = 391
  ClientWidth = 765
  Color = clBtnFace
  OldCreateOrder = False
  WindowState = wsMaximized
  OnShow = UniFormShow
  MonitoredKeys.Keys = <>
  RTL = False
  PixelsPerInch = 96
  TextHeight = 13
  object UniURLFrame1: TUniURLFrame
    Left = 0
    Top = 0
    Width = 765
    Height = 391
    Hint = ''
    Align = alClient
    Anchors = [akLeft, akTop, akRight, akBottom]
    TabOrder = 0
    ParentColor = False
    Color = clWindow
  end
end

运行效果:

Unigui中用Grid++report生成pdf实现打印报表_第1张图片


你可能感兴趣的:(Unigui中用Grid++report生成pdf实现打印报表)