Lazarus

Free Pascal => FV/Textmode IDE => Topic started by: Elap on December 07, 2015, 11:36:06 am

Title: Printing Using the Textmode IDE
Post by: Elap on December 07, 2015, 11:36:06 am
Having used Turbo Pascal for many years I am quite happy using the textmode version of Free Pascal - but, out of curiosity, is it possible to access my Windows printer by using the Printers unit?

I love learning from manuals but I haven't managed to find anything so far.

Could some kind soul point me in the right direction?
Title: Re: Printing Using the Textmode IDE
Post by: Graeme on December 07, 2015, 12:28:11 pm
Take a look at the Free Pascal Text IDE (fp.exe) - it has printing support. The FP IDE code is obviously available too, so browse and learn. ;-)  The joins of open source software.
Title: Re: Printing Using the Textmode IDE
Post by: Thaddy on December 07, 2015, 01:00:46 pm
The very basics looks like this and prints to the default printer:
Code: Pascal  [Select][+][-]
  1. program Project2;
  2. {$APPTYPE CONSOLE}
  3. uses
  4.   printers;
  5. var
  6.   prnFile:Textfile;
  7. begin
  8.   AssignPrn(prnFile);
  9.   try
  10.     Rewrite(prnFile);
  11.     writeln(prnFile, 'Hello, Printer World');
  12.   finally
  13.     CloseFile(prnFile);
  14.   end;
  15. end.

this also works with a windows printer.
Title: Re: Printing Using the Textmode IDE
Post by: rvk on December 07, 2015, 02:26:53 pm
The very basics looks like this and prints to the default printer:
Does this work in FP.EXE ???
Printers.pas is part of LCL.
Is that available to FPC?

(did you try this code in FP.EXE ?)

(we are in the Textmode IDE-subforum so no Lazarus here :))
Title: Re: Printing Using the Textmode IDE
Post by: Thaddy on December 07, 2015, 06:47:37 pm
@Rik: You obviously didn't try my code (again). >:D
I have a Lazarus installation , so that might be the case, but that unit should not be there in that case, because it is even TP compatible.. I'll have a quick check later.

[edit]
Yes it is LCL. (Which it should not be: it should contain simple text/textfile redirects.)
In that case it should be RTL but I have to check the content. Maybe it is too feature rich.
It is not very difficult to write a printers.pp for the RTL.
Title: Re: Printing Using the Textmode IDE
Post by: rvk on December 07, 2015, 06:50:21 pm
@Rik: You obviously didn't try my code (again). >:D
How do you mean "again" ?? I always try code. So I don't see what you mean with again. I think you got me confused with someone else.

I did try your code and it says it can't find unit printers.

The only unit printers I can find is in Lazarus LCL. I can't find one in FPC. Can you direct me where I can find the unit printers in FPC? There is a unit printer (without the s) but that only creates a PRN-file assignment (which would normally be LPT1 as I remember).

Edit: So YOU didn't try your code in FP.EXE  >:D
Title: Re: Printing Using the Textmode IDE
Post by: Thaddy on December 07, 2015, 06:57:59 pm
@Rik: You obviously didn't try my code (again). >:D
How do you mean "again" ?? I always try code. So I don't see what you mean with again. I think you got me confused with someone else.

I did try your code and it says it can't find unit printers.

The only unit printers I can find is in Lazarus LCL. I can't find one in FPC. Can you direct me where I can find the unit printers in FPC? There is a unit printer (without the s) but that only creates a PRN-file assignment (which would normally be LPT1 as I remember).

Edit: So YOU didn't try your code in FP.EXE  >:D

No. I always test with fp.exe or from another editor if it is about fpc code. But I admit I was partly wrong as during the edits from you and me. But only partly.
Title: Re: Printing Using the Textmode IDE
Post by: Thaddy on December 07, 2015, 07:13:37 pm
Anyway. It works. so submitter's question is answered.
Title: Re: Printing Using the Textmode IDE
Post by: rvk on December 07, 2015, 07:18:58 pm
Anyway. It works. so submitter's question is answered.
Does it work in FP.EXE???? OP is using the textmode version of Free Pascal.

Having used Turbo Pascal for many years I am quite happy using the textmode version of Free Pascal - but, out of curiosity, is it possible to access my Windows printer by using the Printers unit?

He's asking if he can use the printers unit in FP.EXE.
So the answer is NO, or do you have a solution which he can use to utilize printers.pas in FP.EXE?

Please note that this question is asked in the "FV/Textmode IDE"-subforum !!!
Title: Re: Printing Using the Textmode IDE
Post by: Thaddy on December 07, 2015, 07:26:03 pm
Yes it works in fp.exe on my setup. If fp can find the LCL it simply works. I hardly use the Lazarus IDE (unless I have to)

But I made a mistake in assuming it was part of the rtl/fcl. Can still be done using printer.pp (without the s)but I just checked and that is incomplete.
Title: Re: Printing Using the Textmode IDE
Post by: rvk on December 07, 2015, 07:46:57 pm
Ok, so with adding some of the LCL unit-directories from Lazarus to the search-unit-directories of FP.EXE, the printers-unit can be used. (You do need the complete source of Lazarus or extract the appropriate units).

Yeah, it would be easier if the base-code of the printers was moved to the FPC-project.

Anyway... printing to a Windows-printer could be done like this without using the printers-unit. This example is for Windows-only (I assume this is fine because we were talking about "Windows"-printers :)):
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses jwawinbase, Windows;
  6.  
  7. function GetDefaultPrinter: string;
  8. var
  9.   DefaultPrinter: array [0 .. 79] of Char;
  10.   I: Integer;
  11. begin
  12.   GetProfileString('windows', 'device', '', DefaultPrinter, SizeOf(DefaultPrinter) - 1);
  13.   I := Pos(',', DefaultPrinter);
  14.   if I = 0 then I := Length(DefaultPrinter) + 1;
  15.   GetDefaultPrinter := Copy(DefaultPrinter, 1, I - 1);
  16. end;
  17.  
  18. procedure Printing(const sPrinter: string);
  19. var
  20.   pDc: hDc;
  21.   DocInfo: TDocInfo;
  22.   S: String;
  23. begin
  24.   pDc := CreateDC(pchar('WINSPOOL'), pchar(sPrinter), nil, nil);
  25.   try
  26.     FillChar(DocInfo, SizeOf(DocInfo), #0);
  27.     DocInfo.cbSize := SizeOf(DocInfo);
  28.     DocInfo.lpszDocName := 'Myprogram';
  29.     if StartDoc(pDc, DocInfo) = 0 then
  30.       Exit;
  31.     if not StartPage(pDc) = 0 then
  32.     begin
  33.       EndDoc(pDc);
  34.       Exit;
  35.     end;
  36.  
  37.     S := 'Hello World';
  38.     TextOut(pDc, 200, 200, @S[1], Length(S));
  39.  
  40.     EndPage(pDc);
  41.     EndDoc(pDc);
  42.  
  43.   finally
  44.     DeleteDc(pDc);
  45.   end;
  46.  
  47. end;
  48.  
  49. begin
  50.   Printing(GetDefaultPrinter);
  51. end.

Margins, pagelength and fonts could all be accounted for and you could create a simple writeln-procedure which does NewPage etc.
Title: Re: Printing Using the Textmode IDE
Post by: rvk on December 07, 2015, 07:55:20 pm
The very basics looks like this and prints to the default printer:
Thaddy... I also tried to find AssignPrn function in LCL/Printers.pas but I can't find it anywhere in LCL/Lazarus or FPC. Do you know where it's defined?

(It is present in Delphi but that's not what we are using here. I'm also not sure if it's using the default WINDOWS-printer in Delphi.)
Title: Re: Printing Using the Textmode IDE
Post by: Leledumbo on December 08, 2015, 06:56:08 am
Doesn't FP IDE use printer (http://www.freepascal.org/docs-html/rtl/printer/index.html) unit from RTL?
Title: Re: Printing Using the Textmode IDE
Post by: rvk on December 08, 2015, 09:19:44 am
Doesn't FP IDE use printer (http://www.freepascal.org/docs-html/rtl/printer/index.html) unit from RTL?
Yes, FP IDE uses that printer unit.

But seeing that printer.pp only consists of one InitPrinter ('PRN'); (in both DOS and WIN directory) it doesn't really help in printing to Windows-printers. It only works printing to LPT1 and only to printers who understand direct printing. Newer (and cheaper) Windows printers don't support direct printing from a parallel port anymore and need the printer-driver from Windows to work.

I can't find any other printer-support for Windows-printers in FPC anywhere so you need to do it yourself on a lower level (like I showed in my other post (http://forum.lazarus.freepascal.org/index.php/topic,30651.msg195199.html#msg195199)) or use another ready made unit like printers from Lazarus if that doesn't depend too much on anything else.
Title: Re: Printing Using the Textmode IDE
Post by: marcov on December 08, 2015, 11:58:57 am
(afaik PRN is not a parallel port, but a pseudo device that is an alias for the true port (typically LPT or COM))

The printer unit is for TP compatibility so limited.
Title: Re: Printing Using the Textmode IDE
Post by: rvk on December 08, 2015, 12:07:46 pm
(afaik PRN is not a parallel port, but a pseudo device that is an alias for the true port (typically LPT or COM))
The printer unit is for TP compatibility so limited.
Can you redirect PRN to a COM port? (I never seen PRN as COM port)
I thought AUX was for COM ports.
PRN defaulted to the first parallel port.

Code: Pascal  [Select][+][-]
  1. AUX             1st serial port
  2. PRN             1st parallel port
  3. COM1 thru COM4  Serial ports
  4. LPT1 thru LPT3  Parallel ports
  5. CON             Keyboard and screen
  6. NUL             Dummy (for testing)

But yes, printer.pp is very limited.
Title: Re: Printing Using the Textmode IDE
Post by: marcov on December 08, 2015, 01:16:46 pm
Afaik with "mode lpt" you can redirect it to serial. It only /defaults/ to prn.

I couldn't quickly find the exact command, but wikipedia confirms the ability: https://en.wikipedia.org/wiki/DOS#Reserved_device_names
Title: Re: Printing Using the Textmode IDE
Post by: rvk on December 08, 2015, 01:32:57 pm
Afaik with "mode lpt" you can redirect it to serial. It only /defaults/ to prn.
I couldn't quickly find the exact command, but wikipedia confirms the ability: https://en.wikipedia.org/wiki/DOS#Reserved_device_names
Yes, you can redirect LPTx to COMx. But you can't directly change the PRN to a COMx.
Ultimately it wouldn't matter because you could redirect LPT1 to COM1 and printing to PRN would still end up on COM1 (but I haven't tested this :)).

B.T.W. the only OS I found that can change PRN to LPTx (where x>1) is DR-DOS 7.02 and higher. Normal MS-DOS can't change it from LPT1. Not even with the MODE LPT-command.
TinyPortal © 2005-2018