Forum > Lazarus Extra Components

Printer4Lazarus not working

(1/2) > >>

asmx:
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  [+][-]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";}};} ---program PrintUtil; uses  SysUtils, printers; const  LEFTMARGIN = 100;  HEADLINE = 'I Printed My Very First Text On ';var  YPos, LineHeight, VerticalMargin: integer;  SuccessString: string;begin  with Printer do  try    BeginDoc;    Canvas.Font.Name := 'Courier New';    Canvas.Font.Size := 10;    Canvas.Font.Color := $0;    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. 
AccessViolationException is thrown directly when running to BeginDoc.

wp:
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  [+][-]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";}};} ---program PrintUtil; uses  Interfaces,    // <--- ADDED  SysUtils, printers,  osprinters;    // <--- ADDED  //, printer4lazarus;          // <--- Not needed in "uses", but only in "required packages" of project const  LEFTMARGIN = 100;  HEADLINE = 'I Printed My Very First Text On ';var  YPos, LineHeight, VerticalMargin: integer;  SuccessString: string;begin  with Printer do  try    BeginDoc;    Canvas.Font.Name := 'Courier New';    Canvas.Font.Size := 10;    Canvas.Font.Color := $0;    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.

AlexTP:
I added this new info (for me) to wiki:
https://wiki.freepascal.org/Using_the_printer#Printing_from_console_application

Thaddy:
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.

asmx:

--- Quote from: wp 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  [+][-]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";}};} ---program PrintUtil; uses  Interfaces,    // <--- ADDED  SysUtils, printers,  osprinters;    // <--- ADDED  //, printer4lazarus;          // <--- Not needed in "uses", but only in "required packages" of project const  LEFTMARGIN = 100;  HEADLINE = 'I Printed My Very First Text On ';var  YPos, LineHeight, VerticalMargin: integer;  SuccessString: string;begin  with Printer do  try    BeginDoc;    Canvas.Font.Name := 'Courier New';    Canvas.Font.Size := 10;    Canvas.Font.Color := $0;    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.
--- End quote ---

Thank you for reply :), It worked!

Navigation

[0] Message Index

[#] Next page

Go to full version