chr(12) Form Feed in Notepad

W

W. eWatson

I thought I'd put a page break, chr(12), character in a txt file I wrote
to skip to the top of the page. It doesn't work. Comments?
 
G

Grant Edwards

I thought I'd put a page break, chr(12), character in a txt
file I wrote to skip to the top of the page. It doesn't work.
Comments?

Yes, it does work.
 
W

W. eWatson

Grant said:
Yes, it does work.
Apparently not with with my Brother 1440 laser printer. The character in
NotePad.txt looks like a small rectangle, and on the printed page. Same
result HP C6180 Photosmart.
 
T

Tim Chase

W. eWatson said:
Apparently not with with my Brother 1440 laser printer. The character in
NotePad.txt looks like a small rectangle, and on the printed page. Same
result HP C6180 Photosmart.

But are you sending the raw control codes to the printer, or are
you sending the image-of-my-text-document to a printer-GDI which
then renders the as-you-see-it out of the printer?

The pseudo-pipeline comparison would be

type file.txt > lpt1:

which would send the raw text file to the printer (assuming it's
set up on LPT1, otherwise, use whatever port it's attached to in
your printer control panel); or are you using something like

notepad file.txt
File -> Print

which renders to an internal image representation and then sends
that image out to the printer. If it were a dot-matrix printer,
you'd here/see the difference in a jiffy -- the raw dump is fast
and uses the printer's built-in fonts while the render-as-image
is slow and NOISY.

One alternative is possibly to set up the "Generic Text" printer
as a device type and attach it to the same port; I've had fair
fortune with this letting me control the printer more directly if
I want fast dumps (particularly on dot-matrix printers) rather
than pretty dumps.

-tkc
 
W

W. eWatson

Tim said:
But are you sending the raw control codes to the printer, or are you
sending the image-of-my-text-document to a printer-GDI which then
renders the as-you-see-it out of the printer?

The pseudo-pipeline comparison would be

type file.txt > lpt1:

which would send the raw text file to the printer (assuming it's set up
on LPT1, otherwise, use whatever port it's attached to in your printer
control panel); or are you using something like

notepad file.txt
File -> Print

which renders to an internal image representation and then sends that
image out to the printer. If it were a dot-matrix printer, you'd
here/see the difference in a jiffy -- the raw dump is fast and uses the
printer's built-in fonts while the render-as-image is slow and NOISY.

One alternative is possibly to set up the "Generic Text" printer as a
device type and attach it to the same port; I've had fair fortune with
this letting me control the printer more directly if I want fast dumps
(particularly on dot-matrix printers) rather than pretty dumps.

-tkc
I should mention I'm using Windows. I just put chr(12) right in the txt.
It's the first character in the next line of the txt file where I want
to page forward. Not acquainted with GDI. Maybe I need some sequence of
such characters?
 
T

Tim Chase

W. eWatson said:
I should mention I'm using Windows. I just put chr(12) right in the txt.
It's the first character in the next line of the txt file where I want
to page forward. Not acquainted with GDI. Maybe I need some sequence of
such characters?

It's not a matter of you controlling the GDI stuff. Unless
you're writing directly to the printer device, printing on
Windows is done (whether by Notepad, gvim, Word, Excel, whatever)
into a graphical representation which is then shipped off to the
printer. So if you're printing from Notepad, it's going to print
what you see (the little square), because Notepad renders to this
graphical representation to print. If you send the file
*directly* to the printer device (bypassing the Win32 printing
layer), it will send the ^L directly and should eject a new page
on most printers.

-tkc
 
W

W. eWatson

Tim said:
It's not a matter of you controlling the GDI stuff. Unless you're
writing directly to the printer device, printing on Windows is done
(whether by Notepad, gvim, Word, Excel, whatever) into a graphical
representation which is then shipped off to the printer. So if you're
printing from Notepad, it's going to print what you see (the little
square), because Notepad renders to this graphical representation to
print. If you send the file *directly* to the printer device (bypassing
the Win32 printing layer), it will send the ^L directly and should eject
a new page on most printers.

-tkc
I am writing a txt file. It's up to the user to print it using Notepad
or some other tool. I have no idea how to send it directly to the
printer, but I really don't want to furnish that capability in the
program. From Google, The Graphics Device Interface (GDI).
 
M

Mensanator

I am writing a txt file. It's up to the user to print it using Notepad
or some other tool.  I have no idea how to send it directly to the
printer, but I really don't want to furnish that capability in the
program. From Google, The Graphics Device Interface (GDI).

Have you considered the possibility that your printer can't print
raw text files? I had one that would ONLY print Postscript. Embedding
a chr(12) would accomplish nothing, you HAD to use a driver that
would translate chr(12) into the appropriate Postcript codes.

What you're doing MIGHT work for others with different printers.
 
N

Neil Hodgson

W. eWatson said:
I am writing a txt file. It's up to the user to print it using Notepad
or some other tool.

WordPad will interpret chr(12) as you want.

Neil
 
T

Tim Chase

W. eWatson said:
I am writing a txt file. It's up to the user to print it using
Notepad or some other tool. I have no idea how to send it
directly to the printer, but I really don't want to furnish
that capability in the program. From Google, The Graphics
Device Interface (GDI).

If you're writing it to a text file and assuming that Notepad is
smart enough to properly handle form-feeds, I'm sorry, you'll be
disappointed...this says more about the brain-deadness of Notepad
than your optimism :)

If you have a configurable destination, you might be able to do
something within your Python app like

if 'win' in sys.platform.lower():
default_dest = "lpt1:"
else:
default_dest = "/dev/lp0"
dest = config.get("printer", default_dest)
f = file(dest, 'wb')
f.write(my_output_with_ff)
# optionally
# f.write(chr(12))
# to eject the last page
f.close()

Otherwise, you'd have to write to something a default Windows
application would know how to handle with embedded
form-feeds/page-breaks (i.e., not Notepad as the default .txt
handler). My first thought would be to export it as RTF (there
was a good python RTF library I tinkered with one afternoon --
it's a quick google away) which should allow embedding
page-breaks, and even give you a fair bit of additional control
over other aspects like fonts and styles.

-tkc
 
W

W. eWatson

Mensanator said:
Have you considered the possibility that your printer can't print
raw text files? I had one that would ONLY print Postscript. Embedding
a chr(12) would accomplish nothing, you HAD to use a driver that
would translate chr(12) into the appropriate Postcript codes.

What you're doing MIGHT work for others with different printers.
Could be, but I have no way of easily knowing. In any case, I was trying
to write a simple report that could be printed with titles at the top of
each page. If there's another "common" format that I can write in to
produce the file, that's fine. It may be this is so difficult to be
impossible. Long, long ago this was no problem. :)

I suppose I could copy the txt file into wordpad, and print it there.
 
N

Nobody

I thought I'd put a page break, chr(12), character in a txt file I wrote
to skip to the top of the page. It doesn't work. Comments?

The 1970's are over, and neither Notepad nor your printer attempts to
maintain compatibility with a Teletype model 37.
 
W

W. eWatson

Neil said:
WordPad will interpret chr(12) as you want.

Neil

That may be the solution. Just tell the end user to copy the file into
it, and print it there.

I just tried it in Wordpad, and it works, but my --- underlines are
pushed together. Maybe tabs instead of spaces. The columns past Seq # in
WordPad may suffer from the characters not being fixed width. Well, a
little work with WordPad might be enough for users to get it right.
"Copy txt file into wordpad. Select all the text, and set format to
fixed (if that's possible.).

Here's what a txt sample looks like. It has line wrap here, and the page
feed

Date/Time & Station UTC Seq # Frames Time Span Pix
Dst Pix/Sec
------------------- -- ------------------- ----- ------ ---------
------- -------
2008/11/12 17:38:58 WW 2008/11/13 01:38:58 1 Noise data. Short
track.
2008/11/12 17:39:24 WW 2008/11/13 01:39:24 2 Noise data. Short
track.


<-------------PAGE FEED
Date/Time & Station UTC Seq # Frames Time Span Pix
Dst Pix/Sec
------------------- -- ------------------- ----- ------ ---------
------- -------
2008/11/17 22:29:54 WW 2008/11/18 06:29:54 21 Noise data. Short
track.
2008/11/18 01:51:36 WW 2008/11/18 09:51:36 22 Noise data. Short
track.
2008/11/18 04:05:03 WW 2008/11/18 12:05:03 23 Noise data. Short
track.
2008/11/18 17:40:42 WW 2008/11/19 01:40:42 24 95 3.17
48.17 15.21
 
D

D'Arcy J.M. Cain

Could be, but I have no way of easily knowing. In any case, I was trying
to write a simple report that could be printed with titles at the top of
each page. If there's another "common" format that I can write in to
produce the file, that's fine. It may be this is so difficult to be
impossible. Long, long ago this was no problem. :)

Why not generate a PostScript or PDF file in the first place? Check
out reportlab.
 
W

W. eWatson

D'Arcy J.M. Cain said:
Why not generate a PostScript or PDF file in the first place? Check
out reportlab.

New Courier and NotePad produces a good looking result.

I'm trying to keep this effort to a minimum. I don't think tracking down
how to write PP code PDF code is worth for this effort.

In another related effort that I might get involved in, it would be good
to be able to produce graphical data as from MatPlotLib and be able to
print that to a printer directly from a Python program.
 
L

Lie Ryan

New Courier and NotePad produces a good looking result.

I'm trying to keep this effort to a minimum. I don't think tracking down
how to write PP code PDF code is worth for this effort.

How about creating a new print job for each discontinuous page? AFAIK,
modern printer spooler don't continue printing between separate jobs? A
bit of hackery, but guaranteed to work unless you're using roll paper.
 
J

John Yeung

I am writing a txt file. It's up to the user to print
it using Notepad or some other tool.

In another response, Tim Chase suggested creating an RTF file instead
of plain text. I think this is your best bet if your goal is to get
page breaks with the least amount of additional effort.

The package he's probably referring to is PyRTF. I took a quick look
at it and in my opinion it's overkill for your purposes. Since RTF is
actually just a markup language, and the only features of it that you
absolutely need are (1) a way to ensure a fixed-width font is used and
(2) a way to insert page breaks, it's probably quickest and simplest
to just throw the markup into the document yourself (well, with
Python) and naming the result with the .rtf suffix instead of .txt.

So, how do you find out what markup to use? Open WordPad, select the
font you want, type a bit of text, save the file as RTF (this should
be the default), and open up the file in Notepad. At the top will be
a bunch of setup codes, including the font selection. Make your
Python program put that whole spiel (including a trailing space or
newline, to separate your text from the last RTF tag) at the top of
your output. There will be one unmatched curly brace, which you
should close at the end of the document. On my computer, it looks
like this:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fmodern
\fprq1\fcharset0 Courier New;}}
{\*\generator Msftedit 5.41.15.1515;}\viewkind4\uc1\pard\f0\fs20

That takes care of the font (and other stuff, but you are interested
in the font). The only other things you need to do are put
r'\par' (the end-paragraph tag) at the end of each line and r'\page'
at the end of each page. Again, remember to make sure these tags
don't collide with your actual text. (I'd use '\\par\n' in place of
'\n' and '\\page\n' in place of chr(12).) Finally, make sure to end
with a closing curly brace as previously mentioned.

The RTF solution is probably more robust than embedding chr(12) and
telling the user to use WordPad, especially since I saw someone report
on another forum that chr(12) doesn't always work even in WordPad (it
might be dependent on the version of WordPad, or the printer, or
whatever).

I am too lazy to have actually read any documentation on RTF, but it's
freely available on the Web should you need to reference it.

John
 
B

bartc

Nobody said:
The 1970's are over, and neither Notepad nor your printer attempts to
maintain compatibility with a Teletype model 37.

Odd that TXT files under Windows still use the same 13 (carriage return), 10
(linefeed) and 9 (tab) codes that used to work on my ASR 33. So why not code
12 (formfeed)?
 
S

Steve Holden

Lie said:
How about creating a new print job for each discontinuous page? AFAIK,
modern printer spooler don't continue printing between separate jobs? A
bit of hackery, but guaranteed to work unless you're using roll paper.
Or unless you are printing to a production mailing system, and don't
want job headers printed or interspersed jobs.
A client of mine just built a web site where output from queries could
be HTML, PDF (Piza and ReportLab) or CSV (just written out). It's not
that hard (though the code isn't open sourced I may be able to get them
to document an outline of the process, or let me (in my copious spare time).

regards
Steve
 
T

Tim Chase

John said:
In another response, Tim Chase suggested creating an RTF file instead
of plain text. I think this is your best bet if your goal is to get
page breaks with the least amount of additional effort.

The package he's probably referring to is PyRTF. I took a quick look
at it and in my opinion it's overkill for your purposes.

Yes, John is correct -- it was PyRTF that I tinkered with (which
can be overkill if all you want to do is what John describes).
I'm not well versed in RTF, and he demonstrates:
I am too lazy to have actually read any documentation on RTF, but it's
freely available on the Web should you need to reference i

even in his laziness, he helpfully provided a quick guide on how
to hand-create simple documents without the addition of a
full-blown RTF toolkit like PyRTF.

-tkc
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top