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
運行效果: