Forum > Beginners

SAVE StringGrid to PDF

(1/7) > >>

seghele0:
The procedures below work well.
But I'm trying to export the Stringgrid to a “PDF” file to set the font size to Dejavu sans mono 15pt.
Can someone please give me the code?
The 'PowerPDF' component is already installed.
Thanks already.
 ;)

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---//  SAVE StringGrid TO TXTprocedure SAVEStringGridTXT(StringGrid1: TStringGrid);var  varScoreBestandsnaam: String;  varCellLen: Integer;  varLijn: String;  varRow: Integer;  varCol: Integer;  varFile1: TextFile;  tmpValue: Single;  tmpColCountArr: array of Integer;   // col length array  PageWidth: Integer;begin  // Show vertical and horizontal lines  StringGrid1.Options := StringGrid1.Options + [goVertLine, goHorzLine];  // Adjust column widths automatically  StringGrid1.AutoSizeColumns;  // Check max length for each column  if StringGrid1.ColCount > 0 then    SetLength(tmpColCountArr, StringGrid1.ColCount)  else    raise Exception.Create('PROBLEM !');  for varCol := 0 to StringGrid1.ColCount - 1 do  begin    for varRow := 0 to StringGrid1.RowCount - 1 do    begin      varCellLen := Length(StringGrid1.Cells[varCol, varRow]);      if varRow = 0 then        tmpColCountArr[varCol] := varCellLen      else if tmpColCountArr[varCol] < varCellLen then        tmpColCountArr[varCol] := varCellLen;    end;  end;  // Calculate page width  PageWidth := 0;  for varCol := 0 to High(tmpColCountArr) do    PageWidth := PageWidth + tmpColCountArr[varCol] + 3; // Kolombreedtes + spaties  PageWidth := PageWidth + 2; // For the lines at the beginning/end of each row  varScoreBestandsnaam := 'WIEZEN.txt';  // Write to the file  AssignFile(varFile1, varScoreBestandsnaam);  try    Rewrite(varFile1);    // Write headline    varLijn := StringOfChar(' ', (PageWidth - Length('WIEZEN.txt')) div 2) + 'WIEZEN.txt';    Writeln(varFile1, varLijn);    Writeln(varFile1, StringOfChar('-', PageWidth));    Writeln(varFile1, 'Resultaat Wiezen - ' + FormatDateTime('dd/mm/yyyy HH:nn', Now));    Writeln(varFile1, StringOfChar('-', PageWidth));    // Write data    for varRow := 0 to StringGrid1.RowCount - 1 do    begin      varLijn := '|';      for varCol := 0 to StringGrid1.ColCount - 1 do      begin        if TryStrToFloat(StringGrid1.Cells[varCol, varRow], tmpValue) then          // Align numerical values to the "right"          varLijn := varLijn + ' ' + Format('%' + IntToStr(tmpColCountArr[varCol]) +            's', [FormatFloat('0.####', tmpValue)]) + ' |'        else          // Align text values to the "right"          varLijn := varLijn + ' ' + Format('%' + IntToStr(tmpColCountArr[varCol]) +            's', [StringGrid1.Cells[varCol, varRow]]) + ' |';      end;      Writeln(varFile1, varLijn);    end;    // Footer writing    Writeln(varFile1, StringOfChar('-', PageWidth));  finally    CloseFile(varFile1);  end;end;      *****************************// SAVE the files "txt" and "xlsx".procedure TFmain.SBUTTONSAVEClick(Sender: TObject);begin  SAVEStringGridTXT(StringGrid1);  //execute procedureend;                          *****************************
Via Google I got the code below, but apparently this doesn't work, starting with uses:  fpdf.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses  FPReport, FPCanvas, FPImage, fpdf; procedure BewaarStringGridPDF(StringGrid1: TStringGrid);var  PDF: TPDFDocument;  Page: TPDFPage;  Font: TPDFFont;  x, y, i, j: Integer;  CellText: String;  CellWidth, CellHeight: Double;  ColWidths: array of Double;begin  PDF := TPDFDocument.Create;  try    PDF.Info.Title := 'Wiezen Resultaten';    PDF.StartDocument;    // Nieuwe pagina    Page := PDF.AddPage;    Page.Height := 842; // A4 Hoogte in punten    Page.Width := 595;  // A4 Breedte in punten    // Lettertype instellen    Font := Page.AddFont('DejaVu Sans Mono', fpdaSubset);    Font.Size := 16; // Lettergrootte 16 pt    Page.SetFont(Font);    // Marges en startpositie    x := 50; // Begin van links    y := 780; // Begin van boven    // Kolombreedtes berekenen    SetLength(ColWidths, StringGrid1.ColCount);    for i := 0 to StringGrid1.ColCount - 1 do    begin      ColWidths[i] := 100; // Standaard kolombreedte      for j := 0 to StringGrid1.RowCount - 1 do      begin        if Length(StringGrid1.Cells[i, j]) * Font.Size / 2 > ColWidths[i] then          ColWidths[i] := Length(StringGrid1.Cells[i, j]) * Font.Size / 2;      end;    end;    // Celhoogte instellen    CellHeight := 20;    // Header schrijven    for i := 0 to StringGrid1.ColCount - 1 do    begin      CellText := StringGrid1.Cells[i, 0];      Page.DrawText(x, y, CellText);      x := x + Round(ColWidths[i]);    end;    x := 50;    y := y - Round(CellHeight); // Naar beneden    // Gegevens schrijven    for j := 1 to StringGrid1.RowCount - 1 do    begin      for i := 0 to StringGrid1.ColCount - 1 do      begin        CellText := StringGrid1.Cells[i, j];        Page.DrawText(x, y, CellText);        x := x + Round(ColWidths[i]);      end;      x := 50;      y := y - Round(CellHeight); // Naar beneden    end;    PDF.SaveToFile('WIEZEN.pdf');  finally    PDF.Free;  end;end;

Fred vS:

--- Quote from: seghele0 on December 06, 2024, 06:27:02 pm ---Via Google I got the code below, but apparently this doesn't work, starting with uses:  fpdf.

--- End quote ---

Hello.
Shouldn't it be fppdf instead?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses..., fppdf;

seghele0:
OK, I've changed into fppdf.
But, there are many errors:

--- Quote ---Compile Project, Target: C:\Lazarus3\LAZARUSPROGS\WIEZEN-20241206\WIEZEN.exe: Exit code 1, Errors: 16
umain.pas(189,26) Error: Incompatible type for arg no. 1: Got "TFPReportVertTextAlignment", expected "TTextLayout"
umain.pas(316,23) Error: Wrong number of parameters specified for call to "Create"
fppdf.pp(5683,26) Error: Found declaration: constructor Create(TComponent);
umain.pas(318,9) Error: identifier idents no member "Info"
umain.pas(321,17) Error: identifier idents no member "AddPage"
umain.pas(322,10) Error: identifier idents no member "Height"
umain.pas(323,10) Error: identifier idents no member "Width"
umain.pas(325,18) Error: identifier idents no member "AddFont"
umain.pas(325,46) Error: Identifier not found "fpdaSubset"
umain.pas(326,10) Error: identifier idents no member "Size"
umain.pas(327,23) Error: Wrong number of parameters specified for call to "SetFont"
fppdf.pp(2346,20) Error: Found declaration: SetFont(LongInt;LongInt);
umain.pas(338,51) Error: identifier idents no member "Size"
umain.pas(339,66) Error: identifier idents no member "Size"
umain.pas(348,12) Error: identifier idents no member "DrawText"
umain.pas(359,14) Error: identifier idents no member "DrawText"
--- End quote ---

Fred vS:

--- Quote ---OK, I've changed into fppdf.
But, there are many errors:
--- End quote ---

You said that Google gives the code...
Hum, was it from Google AI ?
If so, not very impressing.

Maybe try with a other AI (Chatgpt, ...).
(Or even better, study the fppdf code https://wiki.freepascal.org/fcl-pdf  :-X )

seghele0:
I did the maximum, but my level of programming is too low to make this complex procedure.
If no help is possible, which I can understand, then I drop the problem.
I've a working procedure for a stringgrid to TXT, but would have liked to have the PDF alternative for hand.
 :-[
Thanks for the answers..

Navigation

[0] Message Index

[#] Next page

Go to full version