Recent

Author Topic: Print  (Read 11527 times)

DesJardins

  • New Member
  • *
  • Posts: 26
Re: Print
« Reply #15 on: May 02, 2017, 02:55:07 am »
Thanks SkyKhan,
Seems I am asking too much, and I know Thaddy will likely reply some time soon. 
I have a Windows 10 computer with a Brother MFC  laserjet printer operating on wifi, but it can be hard wired is necessary.  The Cannon Ink jet printer uses the USB port.  Neither one works with FPC.  I see others with new HP and other printers.
All I was looking for is a PROGRAM that prints "hello world".
Program Hi
interface
uses  whatever
Implementation
var f: textfile
begin
  rewrite f;
      print (f, 'hello world');
  close f;
end.

and have it go to the printer and actually print. 

or as an alternative have it save f to a text file on disk and use FPC to tell the computer to print the text file, like I could do with Python.  I'm just trying to not re-write 24,000 lines of code in Python.  I'm not looking at graphics or anything fancy.  Just print text that is the result of my program calculations.

I have been reading for 3 days and still don't find a workable example.

I can't be the only one.  Himie has had over 1000 views of his first post.

Richard

sky_khan

  • Guest
Re: Print
« Reply #16 on: May 02, 2017, 05:04:19 am »
Sorry, I realized I have not pay attention to your original question. I assumed that you still want to send text/binary data directly to printer but as I see now, you just want to write text to a lazer/inkjet printers.
The thing is these printers do not print text anymore, they draw text as graphic. So you can not send raw text to print. You need to draw on them. Like below,

Code: Pascal  [Select][+][-]
  1. uses
  2.   classes, printers, osprinters, math;
  3.  
  4. const
  5.   LineCount = 50;  // Adjust font size to fit 50 lines on page
  6.  
  7. procedure PrintStrings(aLines:TStrings);
  8. var
  9.   PrintArea,
  10.   LineRect : TRect;
  11.   I,
  12.   LineHeight : Integer;
  13. begin
  14.   // half of an inch margin for each side
  15.   with PrintArea do
  16.   begin
  17.     Left:=Printer.XDPI div 2;
  18.     Top:=Printer.YDPI div 2;
  19.     Right:=Printer.PageWidth-Printer.XDPI div 2;
  20.     Bottom:=Printer.PageHeight-Printer.YDPI div 2;
  21.   end;
  22.  
  23.   LineRect.Left:=PrintArea.Left;
  24.   LineRect.Right:=PrintArea.Right;
  25.   LineRect.Top:=PrintArea.Top;
  26.  
  27.   Printer.Title:='My Report';
  28.   Printer.BeginDoc;
  29.   try
  30.     Printer.Canvas.Font.Height:=(PrintArea.Bottom-PrintArea.Top) div LineCount;
  31.     for I:=0 to Min(aLines.Count-1,LineCount-1) do
  32.     begin
  33.       if Trim(aLines[I])='' then LineHeight:=Printer.Canvas.TextHeight('X')
  34.       else LineHeight:=Printer.Canvas.TextHeight(aLines[I]);
  35.       LineRect.Bottom:=LineRect.Top+LineHeight;
  36.       Printer.Canvas.TextRect(LineRect,LineRect.Left,LineRect.Top,aLines[I]);
  37.       LineRect.Top:=LineRect.Bottom;
  38.     end;
  39.   finally
  40.     Printer.EndDoc;
  41.   end;
  42. end;

In order to use this procedure you need to add Printer4Lazarus package to your project as dependancy. (Select Menu->Project->Project Inspector->Required Packages and click "Add >>" button)
You can put a TMemo component on your form and call this procedure as "PrintStrings(Memo1.Lines);" in a button click event for example.
« Last Edit: May 02, 2017, 05:12:42 am by SkyKhan »

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Print
« Reply #17 on: May 02, 2017, 11:39:46 am »
The thing is these printers do not print text anymore, they draw text as graphic.
That is simply not true anymore. There are very few printers nowadays that are true Windows only printers. These are a relic of the past.
It used to be the case for cheapo's around 5-8 years ago. Most printers can be used as a line printer and will even select a fixed font in that mode.

Also: OP has a real matrix printer available as he wrote.
The issue is that you simply need some extra code - compared to DOS -  to determine where the default printer is. It is often NOT at LPT1 because everything is USB.
Under windows it is in theory not much more than retrieving it through GetDefaultPrinter and THEN call InitPrinter with the name obtained from that call.
As I said, I will prepare an example.
« Last Edit: May 02, 2017, 11:42:46 am by Thaddy »
Specialize a type, not a var.

sky_khan

  • Guest
Re: Print
« Reply #18 on: May 02, 2017, 09:52:20 pm »
Well, I looked at source code for printers and winprinters and i noticed that it does support rawmode and it is easy to use like below.
But note that this will not work on virtual printers (like PDF printer drivers) or cheap printers which does not support text directly.

Code: Pascal  [Select][+][-]
  1. procedure RawPrintStrings(aLines:TStrings);
  2. const
  3.   CRLF = #13#10;
  4. var
  5.   I : Integer;
  6. begin
  7.   Printer.Title:='My Report';
  8.   Printer.RawMode:=true;
  9.   Printer.BeginDoc;
  10.   try
  11.     for I:=0 to aLines.Count-1 do
  12.       Printer.Write(aLines[I]+CRLF);
  13.   finally
  14.     Printer.EndDoc;
  15.   end;
  16. end;
  17.  

DesJardins

  • New Member
  • *
  • Posts: 26
Re: Print
« Reply #19 on: May 30, 2017, 06:46:19 pm »
Thaddy,

I finally have the rest of my first program re-write almost completed, and it sure would be nice if you could give an example of how to call a modern prin ter as you suggested using  GetDefaultPrinter and THEN call InitPrinter with the name obtained from that call. 

If it helps, I can save results to a file, and then just have the program tell the computer to choose that file and print it, like I could do with Python.  I want results saved to a file anyway, because I can use it to restart the program without re-entering a bunch of data.

 

TinyPortal © 2005-2018