* * *

Author Topic: Printing Using the Textmode IDE  (Read 5369 times)

Elap

  • Jr. Member
  • **
  • Posts: 52
Printing Using the Textmode IDE
« 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?

Graeme

  • Hero Member
  • *****
  • Posts: 1388
    • Graeme on the web
Re: Printing Using the Textmode IDE
« Reply #1 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.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

Thaddy

  • Hero Member
  • *****
  • Posts: 4432
Re: Printing Using the Textmode IDE
« Reply #2 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.
« Last Edit: December 07, 2015, 01:21:58 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

rvk

  • Hero Member
  • *****
  • Posts: 2685
Re: Printing Using the Textmode IDE
« Reply #3 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 :))

Thaddy

  • Hero Member
  • *****
  • Posts: 4432
Re: Printing Using the Textmode IDE
« Reply #4 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.
« Last Edit: December 07, 2015, 06:56:35 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

rvk

  • Hero Member
  • *****
  • Posts: 2685
Re: Printing Using the Textmode IDE
« Reply #5 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
« Last Edit: December 07, 2015, 06:54:59 pm by rvk »

Thaddy

  • Hero Member
  • *****
  • Posts: 4432
Re: Printing Using the Textmode IDE
« Reply #6 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.
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

Thaddy

  • Hero Member
  • *****
  • Posts: 4432
Re: Printing Using the Textmode IDE
« Reply #7 on: December 07, 2015, 07:13:37 pm »
Anyway. It works. so submitter's question is answered.
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

rvk

  • Hero Member
  • *****
  • Posts: 2685
Re: Printing Using the Textmode IDE
« Reply #8 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 !!!

Thaddy

  • Hero Member
  • *****
  • Posts: 4432
Re: Printing Using the Textmode IDE
« Reply #9 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.
« Last Edit: December 07, 2015, 07:28:08 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

rvk

  • Hero Member
  • *****
  • Posts: 2685
Re: Printing Using the Textmode IDE
« Reply #10 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.

rvk

  • Hero Member
  • *****
  • Posts: 2685
Re: Printing Using the Textmode IDE
« Reply #11 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.)

Leledumbo

  • Hero Member
  • *****
  • Posts: 7649
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Printing Using the Textmode IDE
« Reply #12 on: December 08, 2015, 06:56:08 am »
Doesn't FP IDE use printer unit from RTL?

rvk

  • Hero Member
  • *****
  • Posts: 2685
Re: Printing Using the Textmode IDE
« Reply #13 on: December 08, 2015, 09:19:44 am »
Doesn't FP IDE use printer 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) or use another ready made unit like printers from Lazarus if that doesn't depend too much on anything else.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5643
Re: Printing Using the Textmode IDE
« Reply #14 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.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus