Printer.Canvas.Font.Height is always 0 ? lazarus / 21-General ---------------------------------------------------------------------- Hansvb · 2023-07-11 12:50 Hi, I want to print the text from a TSynedit to a PDF. For that I tested the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFrmMain.Button1Click(Sender: TObject);var  printDialog    : TPrintDialog;  i, line : Integer;begin  // Create a printer selection dialog    printDialog := TPrintDialog.Create(FrmMain);     if printDialog.Execute then    begin      Line := 0 ;      Printer.BeginDoc;       for I := 0 to SynEditJsonData.Lines.Count - 1 do begin        Printer.Canvas.TextOut(0, Line, SynEditJsonData.Lines[I]);           {Font.Height is calculated as -Font.Size * 72 / Font.PixelsPerInch which returns           a negative number. So Abs() is applied to the Height to make it a non- negative           value}         Line := Line + Abs(Printer.Canvas.Font.Height);  //==> Printer.Canvas.Font.Height is always: 0 ?        //Line := Line + Abs(20);        if (Line >= Printer.PageHeight) then          Printer.NewPage;      end;       Printer.EndDoc;    end;end; Everything is printed on the first line. That's because Printer.Canvas.Font.Height is always 0. If I replace Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Line := Line + Abs(Printer.Canvas.Font.Height); Through Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Line := Line + Abs(20); Then everything is printed, but I have to zoom in 600% to see it. How can I print text to PDF in a good way? ---------------------------------------------------------------------- paweld · 2023-07-11 13:28 you just have to get the height of the text:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Printer.Canvas.TextHeight();i.e.Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFrmMain.Button1Click(Sender: TObject);var  printDialog    : TPrintDialog;  i, line : Integer;begin  // Create a printer selection dialog  printDialog := TPrintDialog.Create(FrmMain);    if printDialog.Execute then  begin    Line := 0 ;    Printer.BeginDoc;    for I := 0 to SynEditJsonData.Lines.Count - 1 do     begin      Printer.Canvas.TextOut(0, Line, SynEditJsonData.Lines[I]);      Line := Line + Printer.Canvas.TextHeight(SynEditJsonData.Lines[I]);  // + row margin?      if (Line >= Printer.PageHeight) then        Printer.NewPage;    end;      Printer.EndDoc;  end;end; ---------------------------------------------------------------------- Hansvb · 2023-07-11 14:38 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Line := Line + Printer.Canvas.TextHeight(SynEditJsonData.Lines[I]);  // + row margin?After changing to this new line everything is printed on 1 page with a verry small font. All the other pages are empty. ---------------------------------------------------------------------- wp · 2023-07-11 17:57 Font size 0 in the LCL means: use the default font size. Call the procedures GetFontData (https://lazarus- ccr.sourceforge.io/docs/lcl/graphics/getfontdata.html) or GetTextMetrics (https://lazarus- ccr.sourceforge.io/docs/lcl/lclintf/gettextmetrics.html) to determine the "real" height of this font in screen pixels. Then calculate the font size (in points, 1pt = 1/72 inch) from it and assign it to the Printer.Canvas.Font.Size which automatically calculates the font heights in printer resolution. Find in the attachment a simple demonstration which prints a memo containing the "Lorem impsum" dummy text. Paragraphs are word-wrapped. The project, however, is a bit simplified because it applies page breaks only between paragraphs but  not within paragraphs. I added lots of comments to explain what is happening. I think it should not be too difficult for you to generalize the demo to the task of printing json data. ---------------------------------------------------------------------- paweld · 2023-07-11 18:04 Attached is a sample project exporting synedit content to pdf using fpPdf ---------------------------------------------------------------------- Hansvb · 2023-07-12 09:34 Both examples work great. I have some reading/learning to do to understand them. ---------------------------------------------------------------------- paweld · 2023-07-12 11:03 @Hansvb: more info about fppdf: https://www.freepascal.org/~michael/articles/lazpdf/lazpdf.pdf