Lazarus
Home
Forum
Help
TinyPortal
Search
Login
Register
Lazarus
»
Free Pascal
»
Windows
(Moderators:
FPK
,
Tomas Hajny
) »
Print
Free Pascal
Website
Downloads
Wiki
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
FAQ
Wiki
Bugtracker
Packages
IRC channel
Developer Blog
Follow us on Twitter
Latest SVN
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
Search
Advanced search
« previous
next »
Print
Pages:
1
[
2
]
Author
Topic: Print (Read 5217 times)
DesJardins
New member
Posts: 11
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
Logged
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]
uses
classes
,
printers
,
osprinters
,
math
;
const
LineCount
=
50
;
// Adjust font size to fit 50 lines on page
procedure
PrintStrings
(
aLines
:
TStrings
)
;
var
PrintArea
,
LineRect
:
TRect
;
I
,
LineHeight
:
Integer
;
begin
// half of an inch margin for each side
with
PrintArea
do
begin
Left
:
=
Printer
.
XDPI
div
2
;
Top
:
=
Printer
.
YDPI
div
2
;
Right
:
=
Printer
.
PageWidth
-
Printer
.
XDPI
div
2
;
Bottom
:
=
Printer
.
PageHeight
-
Printer
.
YDPI
div
2
;
end
;
LineRect
.
Left
:
=
PrintArea
.
Left
;
LineRect
.
Right
:
=
PrintArea
.
Right
;
LineRect
.
Top
:
=
PrintArea
.
Top
;
Printer
.
Title
:
=
'My Report'
;
Printer
.
BeginDoc
;
try
Printer
.
Canvas
.
Font
.
Height
:
=
(
PrintArea
.
Bottom
-
PrintArea
.
Top
)
div
LineCount
;
for
I
:
=
0
to
Min
(
aLines
.
Count
-
1
,
LineCount
-
1
)
do
begin
if
Trim
(
aLines
[
I
]
)
=
''
then
LineHeight
:
=
Printer
.
Canvas
.
TextHeight
(
'X'
)
else
LineHeight
:
=
Printer
.
Canvas
.
TextHeight
(
aLines
[
I
]
)
;
LineRect
.
Bottom
:
=
LineRect
.
Top
+
LineHeight
;
Printer
.
Canvas
.
TextRect
(
LineRect
,
LineRect
.
Left
,
LineRect
.
Top
,
aLines
[
I
]
)
;
LineRect
.
Top
:
=
LineRect
.
Bottom
;
end
;
finally
Printer
.
EndDoc
;
end
;
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
»
Logged
Thaddy
Hero Member
Posts: 5769
Re: Print
«
Reply #17 on:
May 02, 2017, 11:39:46 am »
Quote from: SkyKhan on May 02, 2017, 05:04:19 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
»
Logged
recommends {$macro on}{$define Silly:=ObjFpc}
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]
procedure
RawPrintStrings
(
aLines
:
TStrings
)
;
const
CRLF
=
#13
#10
;
var
I
:
Integer
;
begin
Printer
.
Title
:
=
'My Report'
;
Printer
.
RawMode
:
=
true
;
Printer
.
BeginDoc
;
try
for
I
:
=
0
to
aLines
.
Count
-
1
do
Printer
.
Write
(
aLines
[
I
]
+
CRLF
)
;
finally
Printer
.
EndDoc
;
end
;
end
;
Logged
DesJardins
New member
Posts: 11
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.
Logged
Print
Pages:
1
[
2
]
« previous
next »
Lazarus
»
Free Pascal
»
Windows
(Moderators:
FPK
,
Tomas Hajny
) »
Print
Recent
Material Design
by
lainz
[
Today
at 03:12:09 pm]
Insert check mark symbol
by
exdatis
[
Today
at 03:08:44 pm]
how to disable directive
by
taazz
[
Today
at 02:56:07 pm]
Fpcupdeluxe
by
EMartin
[
Today
at 02:53:07 pm]
An advice - um conselho
by
hrayon
[
Today
at 02:43:40 pm]
macOS 32-bit app warning
by
Phil
[
Today
at 02:40:10 pm]
IDE output window shown, ...
by
Lyvia
[
Today
at 02:26:23 pm]
lazSubTitlesTranslator fo...
by
laguna
[
Today
at 12:41:07 pm]
Compiler can't find TPU f...
by
Thaddy
[
Today
at 12:26:46 pm]
[TCocoaMenuItem _setMenuO...
by
Renat.Su
[
Today
at 12:20:03 pm]