Printing Using the Textmode IDE lazarus / 65-FV-Textmode IDE ---------------------------------------------------------------------- Elap · 2015-12-07 10:36 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 · 2015-12-07 11:28 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. ---------------------------------------------------------------------- Thaddy · 2015-12-07 12:00 The very basics looks like this and prints to the default printer: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project2;{$APPTYPE CONSOLE}uses  printers;var  prnFile:Textfile;begin  AssignPrn(prnFile);  try    Rewrite(prnFile);    writeln(prnFile, 'Hello, Printer World');  finally    CloseFile(prnFile);  end;end. this also works with a windows printer. ---------------------------------------------------------------------- rvk · 2015-12-07 13:26 Quote from: Thaddy on December 07, 2015, 01:00:46 pmThe 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 · 2015-12-07 17:47 @Rik: You obviously didn't try my code (again). 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. ---------------------------------------------------------------------- rvk · 2015-12-07 17:50 Quote from: Thaddy on December 07, 2015, 06:47:37 pm@Rik: You obviously didn't try my code (again). 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 ---------------------------------------------------------------------- Thaddy · 2015-12-07 17:57 Quote from: rvk on December 07, 2015, 06:50:21 pmQuote from: Thaddy on December 07, 2015, 06:47:37 pm@Rik: You obviously didn't try my code (again). 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  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. ---------------------------------------------------------------------- Thaddy · 2015-12-07 18:13 Anyway. It works. so submitter's question is answered. ---------------------------------------------------------------------- rvk · 2015-12-07 18:18 Quote from: Thaddy on December 07, 2015, 07:13:37 pmAnyway. It works. so submitter's question is answered. Does it work in FP.EXE???? OP is using the textmode version of Free Pascal. Quote from: Elap on December 07, 2015, 11:36:06 amHaving 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 · 2015-12-07 18:26 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. ---------------------------------------------------------------------- rvk · 2015-12-07 18:46 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test; {$mode objfpc}{$H+} uses jwawinbase, Windows; function GetDefaultPrinter: string;var  DefaultPrinter: array [0 .. 79] of Char;  I: Integer;begin  GetProfileString('windows', 'device', '', DefaultPrinter, SizeOf(DefaultPrinter) - 1);  I := Pos(',', DefaultPrinter);  if I = 0 then I := Length(DefaultPrinter) + 1;  GetDefaultPrinter := Copy(DefaultPrinter, 1, I - 1);end; procedure Printing(const sPrinter: string);var  pDc: hDc;  DocInfo: TDocInfo;  S: String;begin  pDc := CreateDC(pchar('WINSPOOL'), pchar(sPrinter), nil, nil);  try    FillChar(DocInfo, SizeOf(DocInfo), #0);    DocInfo.cbSize := SizeOf(DocInfo);    DocInfo.lpszDocName := 'Myprogram';    if StartDoc(pDc, DocInfo) = 0 then      Exit;    if not StartPage(pDc) = 0 then    begin      EndDoc(pDc);      Exit;    end;     S := 'Hello World';    TextOut(pDc, 200, 200, @S[1], Length(S));     EndPage(pDc);    EndDoc(pDc);   finally    DeleteDc(pDc);  end; end; begin  Printing(GetDefaultPrinter);end. Margins, pagelength and fonts could all be accounted for and you could create a simple writeln-procedure which does NewPage etc. ---------------------------------------------------------------------- rvk · 2015-12-07 18:55 Quote from: Thaddy on December 07, 2015, 01:00:46 pmThe 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 · 2015-12-08 05:56 Doesn't FP IDE use printer unit from RTL? ---------------------------------------------------------------------- rvk · 2015-12-08 08:19 Quote from: Leledumbo on December 08, 2015, 06:56:08 amDoesn'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 · 2015-12-08 10:58 (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. ---------------------------------------------------------------------- rvk · 2015-12-08 11:07 Quote from: 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. 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i AUX           1st serial portPRN             1st parallel portCOM1 thru COM4  Serial portsLPT1 thru LPT3  Parallel portsCON             Keyboard and screenNUL             Dummy (for testing) But yes, printer.pp is very limited. ---------------------------------------------------------------------- marcov · 2015-12-08 12:16 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 ---------------------------------------------------------------------- rvk · 2015-12-08 12:32 Quote from: marcov on December 08, 2015, 01:16:46 pmAfaik 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.