Printing plain text with exact positioning on Windows

K

KvS

Dear all,

for a python program running on Win XP that is used to produce
invoices I need to be able to have the printer print a page with just
plain text, but positioned (almost) exactly at prescribed places (due
to the use of 'pre formatted' invoice forms). In particular as close
to the left and right edges of the page as possible. So far I've been
using a combination of HTML & CSS, IE automation and fiddling in the
registry to set left and right printing margins of IE to 0 and back to
defaults after the printing is done.

This worked ok, but recently some problem occured that I wasn't able
to solve so far (seemingly random crashes, probably due to an update
to IE 8, I've posted about this problem before:
http://groups.google.com/group/comp...8dc24c2b85252c?lnk=gst&q=KvS#ba8dc24c2b85252c
but unfortunately no answers).

So now I'm looking for other ways to get this printing job done. I
know of Tim Goldens page about Windows printing:
http://timgolden.me.uk/python/win32_how_do_i/print.html and have been
googling a lot, but I'm still not convinced what the best way to go
is. E.g. I can't get clear whether the combination reportlab & Acrobat
Reader allows to print very close to the left and right edge of the
paper (without a user having to change something in a Print dialog
etc.).

You would expect this is a relatively typical problem and others have
ran into it before me. Any thoughts on this?

Thanks in advance, Kees
 
A

alejandro

I think the easyest way for printing text is with ReportLab. Just few lines
of code and you have a nice pdf...
 
K

KvS

I think the easyest way for printing text is with ReportLab. Just few lines
of code and you have a nice pdf...

Ok, thanks. In that scenario I would also need to be able to
programatically adjust the printing margins in Acrobat reader, i.e.
automate it to some extent. Is that also possible?
 
A

alejandro

Don't remember much.. but maybe you could play with canvas?
I think i don't undertand(i am a croatian so english is not my mother
language). If you have the need that the pdf and the printer setup have lets
say the same paper size, sorry but can't help.
If you want lets say different printer margins for different documents/pages
just calculate them.
A4 is 210x297mm and you have need for a margin of 10mm..
width = 210 - margin
height = 297 - margin
now for positioning use the variables instead the absolute positioning..

i think that reportlab has some methods that alow you to position
text/photos from the left top corner instead the normal positioning.

Don't know if I helped you in any way...

Good luck!

I think the easyest way for printing text is with ReportLab. Just few
lines
of code and you have a nice pdf...

Ok, thanks. In that scenario I would also need to be able to
programatically adjust the printing margins in Acrobat reader, i.e.
automate it to some extent. Is that also possible?
 
C

Chris Gonnerman

KvS said:
So now I'm looking for other ways to get this printing job done. I
know of Tim Goldens page about Windows printing:
http://timgolden.me.uk/python/win32_how_do_i/print.html and have been
googling a lot, but I'm still not convinced what the best way to go
is. E.g. I can't get clear whether the combination reportlab & Acrobat
Reader allows to print very close to the left and right edge of the
paper (without a user having to change something in a Print dialog
etc.).
I have a page, and a module, for Windows printing which gives a lot of
control.

http://newcenturycomputers.net/projects/pythonicwindowsprinting.html

But in your case, I think I'd consider ReportLab. Also, it seems to me
that I've seen a simpler PDF generator module for Python. Dunno where,
though.

-- Chris.
 
K

KvS

Don't remember much.. but maybe you could play with canvas?
I think i don't undertand(i am a croatian so english is not my mother
language). If you have the need that the pdf and the printer setup have lets
say the same paper size, sorry but can't help.
If you want lets say different printer margins for different documents/pages
just calculate them.
A4 is 210x297mm and you have need for a margin of 10mm..
width = 210 - margin
height = 297 - margin
now for positioning use the variables instead the absolute positioning..

i think that reportlab has some methods that alow you to position
text/photos from the left top corner instead the normal positioning.

Don't know if I helped you in any way...

Good luck!




Ok, thanks. In that scenario I would also need to be able to
programatically adjust the printing margins in Acrobat reader, i.e.
automate it to some extent. Is that also possible?

I trust I can do that in reportlab. But then if you print the pdf, you
would still get some margin between the edge of the paper and the text
due to the printing settings of Acrobat Reader basically. I need the
text to be as close as possible to the actual edge of the paper, so I
need to somehow make those margins as small as possible as well...
 
A

alejandro

Did you mean borderless printing?
Every printer needs his margins, some more some less. Some printers have the
ability to do borderless printing but usualy they can do it only on special
or photo paper. So you can adjust the pdf as you wish, even with no margins,
and then try to find under printer options "borderless printing". That is
why I didn't understand :)) it is a printer thing not pdf!
 
K

KvS

Did you mean borderless printing?
Every printer needs his margins, some more some less. Some printers have the
ability to do borderless printing but usualy they can do it only on special
or photo paper. So you can adjust the pdf as you wish, even with no margins,
and then try to find under printer options "borderless printing". That is
why I didn't understand :)) it is a printer thing not pdf!

As much as possible "borderless", yes. Of course the printer will
still apply some small margin, but that's ok. A margin of say <0.5 cm.
is fine. So it's not a printer thing, I accept the (physical)
limitations of the printer, but I want to avoid any extra margins due
to software settings. So I need the create a pdf without/less as
possible margins, and then I need Acrobat Reader to (silently) print
it without any/less as possible extra margins added. I believe the
first step is possible, but I am wondering if the second step is
possible as well, i.e. can I adjust the options normally appearing in
the Printing Dialog through Python?
 
C

Chris Gonnerman

KvS said:
... can I adjust the options normally appearing in
the Printing Dialog through Python?
Yes, if you use my method or my module, as I gave in my previous post.
If you use Adobe Reader to print, I'm not sure how to automate the print
settings.
 
K

KvS

Yes, if you use my method or my module, as I gave in my previous post.  
If you use Adobe Reader to print, I'm not sure how to automate the print
settings.

Thanks Chris, I'll go on and have a look.
 
K

KvS

Yes, if you use my method or my module, as I gave in my previous post.  
If you use Adobe Reader to print, I'm not sure how to automate the print
settings.

Ok, actually I quite like being able to print straightforward through
your code, i.e. without any extra modules installed. I understand that
sending text to the printer is in principle as simple as

dc.TextOut(scale_factor * 72,
-1 * scale_factor * 72,
"Testing...")

I didn't see you do anything with adjusting margins in the code. Does
that mean that if I would e.g. do

dc.TextOut(0,
0,
"Testing...")

the printout would appear in the upper left corner of the paper, as
close to the edges as the printer is capable of? (Sorry, but I only
have Ubuntu available at the moment, no Windows).
 
K

KvS

Yes, if you use my method or my module, as I gave in my previous post.  
If you use Adobe Reader to print, I'm not sure how to automate the print
settings.

Sorry, one more. I completely forgot it's not exactly plain text, but
occasionally also a limited number of non-ASCII characters (accents in
names etc.). Would this be possible through your method?
 
N

Nobody

As much as possible "borderless", yes. Of course the printer will
still apply some small margin, but that's ok. A margin of say <0.5 cm.
is fine. So it's not a printer thing, I accept the (physical)
limitations of the printer, but I want to avoid any extra margins due
to software settings.

"Hardcopy" document formats such as PostScript and PDF use positions
relative to the edges of the page, not the margins.
 
K

KvS

"Hardcopy" document formats such as PostScript and PDF use positions
relative to the edges of the page, not the margins.

Right. Still, Acrobat Reader by default scales the contents to fit on
a page and creates some margins by doing so, no? So if my text is
close to the left and right edges, as I want, it will get scaled and
extra margins will occur. Avoiding this still requires me to be able
to turn off this scaling in the printing preferences somehow
programmatically, so it doesn't seem to make the problem easier?
 
C

Chris Gonnerman

KvS said:
Ok, actually I quite like being able to print straightforward through
your code, i.e. without any extra modules installed. I understand that
sending text to the printer is in principle as simple as

dc.TextOut(scale_factor * 72,
-1 * scale_factor * 72,
"Testing...")

I didn't see you do anything with adjusting margins in the code. Does
that mean that if I would e.g. do

dc.TextOut(0,
0,
"Testing...")

the printout would appear in the upper left corner of the paper, as
close to the edges as the printer is capable of? (Sorry, but I only
have Ubuntu available at the moment, no Windows).
Actually, as I understand it (and I'll admit my understanding is a bit
flawed in some areas), when you tell MSWinPrint.py to print at (0, 0),
you are telling it to print at the actual paper edge; if that's outside
the printable area (and it probably is for most printers), your printing
will be cut off.

Best thing to do is to try it.
 
C

Chris Gonnerman

KvS said:
Sorry, one more. I completely forgot it's not exactly plain text, but
occasionally also a limited number of non-ASCII characters (accents in
names etc.). Would this be possible through your method?
If Windows can print it, then MSWinPrint.py should be able to also.

But I haven't tested that extensively. YMMV.
 
S

Sean DiZazzo

Right. Still, Acrobat Reader by default scales the contents to fit on
a page and creates some margins by doing so, no? So if my text is
close to the left and right edges, as I want, it will get scaled and
extra margins will occur. Avoiding this still requires me to be able
to turn off this scaling in the printing preferences somehow
programmatically, so it doesn't seem to make the problem easier?

Maybe you could have the user print your data on a larger sheet of
paper;one that is sure to include all of the data, and include crop
marks on the printout. The user then cuts along the crop marks to
leave a perfectly sized, marginless page. This is how printers do
bleeds.
 
G

Gerry

If this is, by any chance, an HP printer, the printer may support PCL
5 (or a similar language). I've written PCL scripts (in Pascal, so a
while ago) to precisely print points at the printer resolution (i.e.,
I picked which six-hundredth of an inch in height and width dimensions
to print a dot. PCL 5 absolutely would let you put text anywhere you
wanted, including the absolute limits on margins that the hardware
supports. (PCL is printer control language).

Gerry
 
N

Nobody

Right. Still, Acrobat Reader by default scales the contents to fit on
a page and creates some margins by doing so, no? So if my text is
close to the left and right edges, as I want, it will get scaled and
extra margins will occur. Avoiding this still requires me to be able
to turn off this scaling in the printing preferences somehow
programmatically, so it doesn't seem to make the problem easier?

If the document is the same size as the physical page, it will be
transferred directly without any scaling or offset. The document will not
be scaled to fit the printable area; if the document contains any marks
which lie within the printer's margins, those marks won't appear on the
printed page.

PostScript and PDF documents don't have "margins". There might be an area
around the edge of the page which doesn't contain any marks, but that's
irrelevant; the area is still part of the page.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top