Basic printing of form content. lazarus / 21-General ---------------------------------------------------------------------- MarkMLl · 2025-08-18 15:25 I have a form containing a map viewer, a treeview and a couple of graphical panels. As attached, I am able to dump a .png with no problems using the code below: 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 TFormMain.MenuViewOutputAsPngClick(Sender: TObject); var  defaultName: string;  png: TPortableNetworkGraphic;  everything: TRect; begin (* We want the default name to match what's currently on the status bar, if we  *)(* used the current time this might differ by a few seconds from what's being   *)(* displayed due to message backlog.                                          *)   defaultName :=   StatusBar1.Panels[2].Text;  if Pos('.', defaultName) > 0 then    SetLength(defaultName, Pos('.', defaultName) - 1);  defaultName := DelChars(defaultName, '-');  defaultName := DelChars(defaultName, 'T');  defaultName := DelChars(defaultName, ':'); (* Capture the image to match, do this before faffing around trying to get the  *)(* name right in case it's an incident that needs to be recorded.             *)   png := TPortableNetworkGraphic.Create;  try    everything.Top := 0;    everything.Left := 0;    everything.Bottom := Height - MainMenu1.Height - 2;    everything.Right := Width - 1;    png.Height := everything.Bottom + 1;    png.Width := everything.Right + 1;    png.Canvas.CopyRect(everything, Canvas, everything);    SaveDialog1.Filename := AirfieldShortname + '-' + defaultName;    SaveDialog1.DefaultExt := 'png';    SaveDialog1.InitialDir := UserHome();    if SaveDialog1.Execute then      png.SaveToFile(SaveDialog1.Filename)  finally    png.Free  endend { TFormMain.MenuViewOutputAsPngClick } ;  Similar code works to the clipboard. What do I have to do to instead send this to a printer? I've tried several variants of the code below: 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 TFormMain.MenuViewPrintClick(Sender: TObject); const  LEFTMARGIN = 100;  HEADLINE = 'I Printed My Very First Text On ';var  YPos, LineHeight, VerticalMargin: Integer;  SuccessString: String; var  everything: TRect;   workingBitmap: TBitMap;  fromLimits, toLimits: TRect; begin { // This works. // https://wiki.freepascal.org/Using_the_printer  if PrintDialog1.Execute() then begin // This works    with Printer do try      BeginDoc;      Canvas.Font.Name := 'Courier New';      Canvas.Font.Size := 10;      Canvas.Font.Color := clBlack;      LineHeight := Round(1.2 * Abs(Canvas.TextHeight('I')));      VerticalMargin := 4 * LineHeight;      // There we go      YPos := VerticalMargin;      SuccessString := HEADLINE + DateTimeToStr(Now);    Canvas.TextOut(LEFTMARGIN, YPos, SuccessString);    finally      EndDoc;    end;  end} // The best I can get from either of the below is a blank page.   workingBitMap := TBitMap.Create;  workingBitMap.Width := Width;  workingBitMap.Height := Height - MainMenu1.Height;  PaintTo(workingBitMap.Canvas, 0, 0);  fromLimits := Rect(0, 0, workingBitMap.Width, workingBitMap.Height);  toLimits := Rect(0, 0, Printer.PaperSize.Width, Printer.PaperSize.Height);  if PrintDialog1.Execute() then begin    Printer.BeginDoc;    Printer.Canvas.CopyRect(toLimits, workingBitMap.Canvas, fromLimits);  Printer.EndDoc;    workingBitMap.Free  end; {// https://forum.lazarus .freepascal.org/index.php/topic,62651.msg473978.html#msg473978  if PrintDialog1.Execute() then begin // This doesn't work    Printer.BeginDoc;    try      everything.Top := 0;      everything.Left := 0;      everything.Bottom := Min(Printer.PageHeight, Height - MainMenu1.Height) - 2;      everything.Right := Min(Printer.PageWidth, Width) - 1;      Printer.Canvas.CopyRect(everything, Canvas, everything)    finally    Printer.EndDoc    end  end}end { TFormMain.MenuViewPrintClick } ;  If nobody has any quick suggestions I'll drop this for the moment, since what I'm trying to focus on is generating a video for the airfield's "fly-in-" in three weeks time. Lazarus 2.2.6, FPC 3.2.2, Linux with GTK2 on a KDE desktop (Debian 12), printer is a networked colour laser branded Dell which is generally trouble-free. MarkMLl ---------------------------------------------------------------------- wp · 2025-08-18 17:49 Why don't you just CopyRect the png that you successfully created in the 1st code to the printer's Canvas? I am attaching a project which has a MapViewer and a button on a form, paints the form onto a bitmap and stretch-prints the bitmap to the printer such that it fits into the printable page width (raw page width minus page margins) ---------------------------------------------------------------------- MarkMLl · 2025-08-19 08:27 Quote from: wp on August 18, 2025, 07:49:40 pmWhy don't you just CopyRect the png that you successfully created in the 1st code to the printer's Canvas? I am attaching a project which has a MapViewer and a button on a form, paints the form onto a bitmap and stretch-prints the bitmap to the printer such that it fits into the printable page width (raw page width minus page margins) Using your standalone demo, the image is correctly transferred to the test.bmp file but does not appear on either the colour or the B&W printer; I do however get the two lines of text. Same happens if I move your code into my program (which I've done, because I know from other forum postings etc. that you've put substantial work into the scaling etc. right). If you have any immediate suggestions I'll try them in the interest of understanding what's going wrong, but otherwise I'll just disable the menu entry until I've moved the whole thing onto Lazarus v3 or even v4 (which won't be for at least another month). Thanks for the help, which if nothing else has narrowed the problem down to Lazarus v2. MarkMLl ---------------------------------------------------------------------- wp · 2025-08-19 09:48 For testing I had printed only to PDF, but now I switched to the "real" printer - perfect print-out, no issue. An issue with your printer driver? What happens when you print to PDF and/or a similar printer? ---------------------------------------------------------------------- MarkMLl · 2025-08-19 10:21 Quote from: wp on August 19, 2025, 11:48:04 amFor testing I had printed only to PDF, but now I switched to the "real" printer - perfect print-out, no issue. An issue with your printer driver? What happens when you print to PDF and/or a similar printer? I've just installed Debian's printer-driver-cups-pdf, printing e.g. this page to a PDF works fine but your demo only outputs the two lines of text. MarkMLl ---------------------------------------------------------------------- MarkMLl · 2025-08-19 10:37 Quote from: MarkMLl on August 19, 2025, 12:21:43 pmI've just installed Debian's printer-driver-cups-pdf, printing e.g. this page to a PDF works fine but your demo only outputs the two lines of text. It's specific to GTK2, possibly on LCL v2 (the Lazarus v3.0 I've got here has an internal error on GTK2, so I've not been able to test that combination yet). QT5 works. MarkMLl