Recent

Author Topic: Printer4Lazarus, Lazarus 3.4  (Read 1057 times)

irawan

  • New Member
  • *
  • Posts: 11
Printer4Lazarus, Lazarus 3.4
« on: June 25, 2024, 02:50:47 am »
i got error on this
Code: Pascal  [Select][+][-]
  1. procedure TfMain.Button1Click(Sender: TObject);
  2. const
  3.   LEFTMARGIN = 100;
  4.   HEADLINE = 'I Printed My Very First Text On ';
  5. var
  6.   YPos, LineHeight, VerticalMargin: Integer;
  7.   SuccessString: String;
  8. begin
  9.   with Printer do
  10.   try
  11.     BeginDoc;
  12.     Canvas.Font.Name := 'Courier New';
  13.     Canvas.Font.Size := 10;
  14.     Canvas.Font.Color := clBlack;
  15.     LineHeight := Round(1.2 * Abs(Canvas.TextHeight('I')));
  16.     VerticalMargin := 4 * LineHeight;
  17.     // There we go
  18.     YPos := VerticalMargin;
  19.     SuccessString := HEADLINE + DateTimeToStr(Now);
  20.     Canvas.TextOut(LEFTMARGIN, YPos, SuccessString);
  21.   finally
  22.     EndDoc;
  23.   end;
  24. end;                  
  25.  
any clue?

bytebites

  • Hero Member
  • *****
  • Posts: 651
Re: Printer4Lazarus, Lazarus 3.4
« Reply #1 on: June 25, 2024, 09:35:14 am »
Which error?

wp

  • Hero Member
  • *****
  • Posts: 12128
Re: Printer4Lazarus, Lazarus 3.4
« Reply #2 on: June 26, 2024, 05:52:28 pm »
Your code looks correct, and works fine on my system (Win 11, PDF printer). What might be helpful, is to add unit OSPrinters to the uses clause (in addition to unit Printers) in order to get correct access to OS-specific printing routines.

Please note that specifying the LEFTMARGIN as 100 pixels is not a very good idea because the printer resolution usually is much different from the screen resolution, and thus the 100 px may be too small. Better to think in device-independent units, e.g. millimeters, and provide a conversion function to pixels:

Code: Pascal  [Select][+][-]
  1. const
  2.   INCH = 25.4;   // 1 inch is 25.4 mm
  3.  
  4. function mmToPx(mm: Double; APixelsPerInch: Integer): Integer;
  5. begin
  6.   Result := round(mm / INCH * APixelsPerInch);
  7. end;
  8.  
  9. const
  10.   LEFT_MARGIN_MM = 10;  // Left margin should be 10 mm
  11. var
  12.   LeftMargin: Integer;   // Left margin in pixels, to be passed to the printer
  13. begin
  14.   with Printer do
  15.   begin
  16.     ...
  17.     LeftMargin := mmToPx(LEFT_MARGIN_MM, XDPI);
  18.     ...
  19.  
...

irawan

  • New Member
  • *
  • Posts: 11
Re: Printer4Lazarus, Lazarus 3.4
« Reply #3 on: June 27, 2024, 05:46:51 am »
Which error?
Project PortMap raised exception class 'External: ACCESS VIOLATION' with message:
Access violation reading from address $00000000000058.

in file 'printers.pas' at line 739

irawan

  • New Member
  • *
  • Posts: 11
Re: Printer4Lazarus, Lazarus 3.4
« Reply #4 on: June 27, 2024, 05:56:21 am »
Your code looks correct, and works fine on my system (Win 11, PDF printer). What might be helpful, is to add unit OSPrinters to the uses clause (in addition to unit Printers) in order to get correct access to OS-specific printing routines.
i got another problem when adding osprinter.
Cannot find osprinters used by sMain. Check if package Printer4Lazarus is in the dependency of the project inspector.
trying to find printer4lazarus in online package manager and got none.
cause of this i compiled by removing osprinters.
project compiled okay but gave error when executed.

and then following this https://wiki.freepascal.org/Using_the_printer#Adding_the_Printer4Lazarus_package_to_your_project_requirements
and now everything work as it should.
« Last Edit: June 27, 2024, 06:10:54 am by irawan »

wp

  • Hero Member
  • *****
  • Posts: 12128
Re: Printer4Lazarus, Lazarus 3.4
« Reply #5 on: June 27, 2024, 05:05:35 pm »
Unit Printers is in the "general-purpose" package LCLBase (heart of the LCL). This way, you can use the TPrinter class in your application. BUT: at this stage the LCL does not know how to create an instance of TPrinter. Therefore, you must add the Printer4Lazarus package to the project requirements which takes care of that. And along with this package you also get the OSPrinters unit - do not look for it in OPM. Quite often you do not have to add OSPrinters to the uses clause because this is handled automatically.

BTW, the easiest way to add Printer4Lazarus is to drop one of the print dialogs on a project form.

 

TinyPortal © 2005-2018