Recent

Author Topic: Printer4Lazarus not working  (Read 1623 times)

asmx

  • New Member
  • *
  • Posts: 14
Printer4Lazarus not working
« on: January 07, 2024, 01:11:50 pm »
I followed this page (https://wiki.freepascal.org/Using_the_printer) to write a print test program, but it doesn't work properly.

Code: Pascal  [Select][+][-]
  1. program PrintUtil;
  2.  
  3. uses
  4.   SysUtils, printers;
  5.  
  6. const
  7.   LEFTMARGIN = 100;
  8.   HEADLINE = 'I Printed My Very First Text On ';
  9. var
  10.   YPos, LineHeight, VerticalMargin: integer;
  11.   SuccessString: string;
  12. begin
  13.   with Printer do
  14.   try
  15.     BeginDoc;
  16.     Canvas.Font.Name := 'Courier New';
  17.     Canvas.Font.Size := 10;
  18.     Canvas.Font.Color := $0;
  19.     LineHeight := Round(1.2 * Abs(Canvas.TextHeight('I')));
  20.     VerticalMargin := 4 * LineHeight;
  21.     // There we go
  22.     YPos := VerticalMargin;
  23.     SuccessString := HEADLINE + DateTimeToStr(Now);
  24.     Canvas.TextOut(LEFTMARGIN, YPos, SuccessString);
  25.   finally
  26.     EndDoc;
  27.   end;
  28.  
  29. end.
  30.  

AccessViolationException is thrown directly when running to BeginDoc.
« Last Edit: January 08, 2024, 02:08:15 pm by asmx »

wp

  • Hero Member
  • *****
  • Posts: 12288
Re: Printer4Lazarus not working
« Reply #1 on: January 07, 2024, 04:48:19 pm »
The wiki article you are referring to is meant for printing out of a Lazarus GUI application. You are testing printing, however, in a console application. The difference is that a console application by-passes the Lazarus widgetset architecture and does not know about the LCL. The elemental step to make the LCL available in a consolue application is to add the unit "Interfaces" to the uses clause. But then you need another unit: "OSPrinters" to make the printer units of your operating system available.

So, in total, modify your uses clause as follows, and your application will work (tested on Windows):
Code: Pascal  [Select][+][-]
  1. program PrintUtil;
  2.  
  3. uses
  4.   Interfaces,    // <--- ADDED
  5.   SysUtils, printers,
  6.   osprinters;    // <--- ADDED
  7.   //, printer4lazarus;          // <--- Not needed in "uses", but only in "required packages" of project
  8.  
  9. const
  10.   LEFTMARGIN = 100;
  11.   HEADLINE = 'I Printed My Very First Text On ';
  12. var
  13.   YPos, LineHeight, VerticalMargin: integer;
  14.   SuccessString: string;
  15. begin
  16.   with Printer do
  17.   try
  18.     BeginDoc;
  19.     Canvas.Font.Name := 'Courier New';
  20.     Canvas.Font.Size := 10;
  21.     Canvas.Font.Color := $0;
  22.     LineHeight := Round(1.2 * Abs(Canvas.TextHeight('I')));
  23.     VerticalMargin := 4 * LineHeight;
  24.     // There we go
  25.     YPos := VerticalMargin;
  26.     SuccessString := HEADLINE + DateTimeToStr(Now);
  27.     Canvas.TextOut(LEFTMARGIN, YPos, SuccessString);
  28.   finally
  29.     EndDoc;
  30.   end;
  31.  
  32. end.

AlexTP

  • Hero Member
  • *****
  • Posts: 2455
    • UVviewsoft
Re: Printer4Lazarus not working
« Reply #2 on: January 07, 2024, 06:20:42 pm »

Thaddy

  • Hero Member
  • *****
  • Posts: 15509
  • Censorship about opinions does not belong here.
Re: Printer4Lazarus not working
« Reply #3 on: January 07, 2024, 07:10:27 pm »
If you want simple printing from a console app, use the unit printer, not printers.
It is limited to what was possible in DOS times, but does the job.
My great hero has found the key to the highway. Rest in peace John Mayall.
Playing: "Broken Wings" in your honour. As well as taking out some mouth organs.

asmx

  • New Member
  • *
  • Posts: 14
Re: Printer4Lazarus not working
« Reply #4 on: January 08, 2024, 02:07:55 pm »
The wiki article you are referring to is meant for printing out of a Lazarus GUI application. You are testing printing, however, in a console application. The difference is that a console application by-passes the Lazarus widgetset architecture and does not know about the LCL. The elemental step to make the LCL available in a consolue application is to add the unit "Interfaces" to the uses clause. But then you need another unit: "OSPrinters" to make the printer units of your operating system available.

So, in total, modify your uses clause as follows, and your application will work (tested on Windows):
Code: Pascal  [Select][+][-]
  1. program PrintUtil;
  2.  
  3. uses
  4.   Interfaces,    // <--- ADDED
  5.   SysUtils, printers,
  6.   osprinters;    // <--- ADDED
  7.   //, printer4lazarus;          // <--- Not needed in "uses", but only in "required packages" of project
  8.  
  9. const
  10.   LEFTMARGIN = 100;
  11.   HEADLINE = 'I Printed My Very First Text On ';
  12. var
  13.   YPos, LineHeight, VerticalMargin: integer;
  14.   SuccessString: string;
  15. begin
  16.   with Printer do
  17.   try
  18.     BeginDoc;
  19.     Canvas.Font.Name := 'Courier New';
  20.     Canvas.Font.Size := 10;
  21.     Canvas.Font.Color := $0;
  22.     LineHeight := Round(1.2 * Abs(Canvas.TextHeight('I')));
  23.     VerticalMargin := 4 * LineHeight;
  24.     // There we go
  25.     YPos := VerticalMargin;
  26.     SuccessString := HEADLINE + DateTimeToStr(Now);
  27.     Canvas.TextOut(LEFTMARGIN, YPos, SuccessString);
  28.   finally
  29.     EndDoc;
  30.   end;
  31.  
  32. end.

Thank you for reply :), It worked!

asmx

  • New Member
  • *
  • Posts: 14
Re: Printer4Lazarus not working
« Reply #5 on: January 08, 2024, 03:59:16 pm »
Thanks every one!

 

TinyPortal © 2005-2018