txt2pdf.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. program txt2pdf;
  2. {$mode objfpc}{$H+}
  3. uses
  4. Classes, SysUtils, CustApp, fpreport, fpreportpdfexport, fpTTF,
  5. fpreportstreamer, fpjson, jsonparser;
  6. type
  7. { TPrintApplication }
  8. TPrintApplication = class(TCustomApplication)
  9. private
  10. FReport : TFPReport;
  11. FLines : TStringList;
  12. FData : TFPReportUserData;
  13. FLineIndex : Integer;
  14. procedure DoFirst(Sender: TObject);
  15. procedure DoGetEOF(Sender: TObject; var IsEOF: boolean);
  16. procedure DoGetNames(Sender: TObject; List: TStrings);
  17. procedure DoGetNext(Sender: TObject);
  18. procedure DoGetValue(Sender: TObject; const AValueName: string; var AValue: variant);
  19. protected
  20. procedure DoRun; override;
  21. public
  22. Constructor Create(AOwner : TComponent); override;
  23. Destructor destroy; override;
  24. end;
  25. { TPrintApplication }
  26. procedure TPrintApplication.DoGetNames(Sender: TObject; List: TStrings);
  27. begin
  28. List.Add('Line');
  29. end;
  30. procedure TPrintApplication.DoGetEOF(Sender: TObject; var IsEOF: boolean);
  31. begin
  32. isEOF:=FLineIndex>=FLines.Count;
  33. end;
  34. procedure TPrintApplication.DoFirst(Sender: TObject);
  35. begin
  36. FLineIndex:=0;
  37. end;
  38. procedure TPrintApplication.DoGetNext(Sender: TObject);
  39. begin
  40. Inc(FLineIndex);
  41. end;
  42. procedure TPrintApplication.DoGetValue(Sender: TObject; const AValueName: string; var AValue: variant);
  43. begin
  44. Avalue:=FLines[FLineIndex];
  45. end;
  46. procedure TPrintApplication.DoRun;
  47. Var
  48. PG : TFPReportPage;
  49. PH : TFPReportPageHeaderBand;
  50. PF : TFPReportPageFooterBand;
  51. DB : TFPReportDataBand;
  52. M : TFPReportMemo;
  53. PDF : TFPReportExportPDF;
  54. Fnt : String;
  55. begin
  56. Fnt:='DejaVuSans';
  57. Terminate;
  58. FLines.LoadFromFile(ParamStr(1));
  59. gTTFontCache.ReadStandardFonts;
  60. gTTFontCache.BuildFontCache;
  61. PaperManager.RegisterStandardSizes;
  62. // Page
  63. PG:=TFPReportPage.Create(FReport);
  64. PG.Data:=FData;
  65. PG.Orientation := poPortrait;
  66. PG.PageSize.PaperName := 'A4';
  67. PG.Margins.Left := 15;
  68. PG.Margins.Top := 15;
  69. PG.Margins.Right := 15;
  70. PG.Margins.Bottom := 15;
  71. // Page header
  72. PH:=TFPReportPageHeaderBand.Create(PG);
  73. PH.Layout.Height:=10; // 1 cm.
  74. M:=TFPReportMemo.Create(PH);
  75. M.Layout.Top:=1;
  76. M.Layout.Left:=1;
  77. M.Layout.Width:=120;
  78. M.Layout.Height:=7;
  79. M.Text:=ParamStr(1);
  80. M.Font.Name:=Fnt;
  81. M.Font.Size:=10;
  82. M:=TFPReportMemo.Create(PH);
  83. M.Layout.Top:=1;
  84. M.Layout.Left:=PG.Layout.Width-41;
  85. M.Layout.Width:=40;
  86. M.Layout.Height:=7;
  87. M.Text:='[Date]';
  88. M.Font.Name:=Fnt;
  89. M.Font.Size:=10;
  90. // Page footer
  91. PF:=TFPReportPageFooterBand.Create(PG);
  92. PF.Layout.Height:=10; // 1 cm.
  93. M:=TFPReportMemo.Create(PF);
  94. M.Layout.Top:=1;
  95. M.Layout.Left:=1;
  96. M.Layout.Width:=40;
  97. M.Layout.Height:=7;
  98. M.Text:='Page [PageNo]';
  99. M.Font.Name:=Fnt;
  100. M.Font.Size:=10;
  101. // Actual line
  102. DB:=TFPReportDataBand.Create(PG);
  103. DB.Data:=FData;
  104. DB.Layout.Height:=5; // 0.5 cm.
  105. DB.StretchMode:=smActualHeight;
  106. M:=TFPReportMemo.Create(DB);
  107. M.Layout.Top:=1;
  108. M.Layout.Left:=1;
  109. M.Layout.Width:=PG.Layout.Width-41;
  110. M.Layout.Height:=4;
  111. M.Text:='[Line]';
  112. M.StretchMode:=smActualHeight;
  113. M.Font.Name:=Fnt;
  114. M.Font.Size:=10;
  115. // Set up data
  116. FData.OnGetNames:=@DoGetNames;
  117. FData.OnNext:=@DoGetNext;
  118. FData.OnGetValue:=@DoGetValue;
  119. FData.OnGetEOF:=@DoGetEOF;
  120. FData.OnFirst:=@DoFirst;
  121. // Go !
  122. FReport.RunReport;
  123. PDF:=TFPReportExportPDF.Create(Self);
  124. try
  125. PDF.FileName:=ChangeFileExt(Paramstr(1),'.pdf');
  126. FReport.RenderReport(PDF);
  127. finally
  128. PDF.Free;
  129. end;
  130. end;
  131. constructor TPrintApplication.Create(AOwner: TComponent);
  132. begin
  133. Inherited;
  134. FReport:=TFPReport.Create(Self);
  135. FLines:=TStringList.Create;
  136. FData:=TFPReportUserData.Create(Self);
  137. end;
  138. destructor TPrintApplication.destroy;
  139. begin
  140. FreeAndNil(FData);
  141. FreeAndNil(FLines);
  142. FreeAndNil(FReport);
  143. inherited destroy;
  144. end;
  145. var
  146. Application: TPrintApplication;
  147. begin
  148. Application:=TPrintApplication.Create(nil);
  149. Application.Title:='Print File Application';
  150. Application.Run;
  151. Application.Free;
  152. end.