Page 1 of 1

letters

Posted: Tue Mar 16, 2004 9:33 am
by gurjit
hi all,

i am currently coding a batch of letters. i was wondering if there were any good resources out there to do letter templates.

the letters will pull data from a database and have alot of static data in them too.
i was also wondering if anyone has any good ideas on how i can store these letter templates? is it best to put them in a database table or individual templates?

any help with representing letters with a header title and footer and middle body will be appreciated. the letters will be done in normal web php templates and not any other software such as pdf.

i want to be able to split the letter on two pages when it reaches the bottom of the page and re-print the header and footer for page 2. how can i do this in php?

any help will be appreciated.

Re: letters

Posted: Tue Mar 16, 2004 10:06 am
by TheBentinel.com
gurjit wrote:i want to be able to split the letter on two pages when it reaches the bottom of the page and re-print the header and footer for page 2. how can i do this in php?

any help will be appreciated.
You're in for a wild ride if you're trying to nicely format output for printing using a browser and HTML. Will the browser be maximized? How big is the client's screen? What is their text view setting? (In IE View-->Text Size) Does the user have a stylesheet in place that overrides whatever you send?

You can get around some of that with tables that have a pre-defined width and height, in pixels. Then set your font sizes using STYLE attributes and again, use pixels. <font style="font-size: 12px">

Be careful not to use tags that have variable sizes depending on user settings, like <P>. After you get a page looking the way you want, change your view-->text size setting and refresh the page. Did it change?

You can force page-breaks thru CSS, look for page-break-after and page-break-before in google.

Posted: Tue Mar 16, 2004 10:12 am
by gurjit
with the dynamic data from database i can determine how long the page is going to be.

my idea was to set the table size the height of a A4 paper sheet but because of the dynamic data i dont know how i can go on to the next page and duplicate the header and footer. will there be a page 2 on the letter,all depends on the dynamic text.

these letters will be printed on USA size paper and do not need to be viewable to the user.

Posted: Tue Mar 16, 2004 10:28 am
by TheBentinel.com
gurjit wrote:with the dynamic data from database i can determine how long the page is going to be.

my idea was to set the table size the height of a A4 paper sheet but because of the dynamic data i dont know how i can go on to the next page and duplicate the header and footer. will there be a page 2 on the letter,all depends on the dynamic text.

these letters will be printed on USA size paper and do not need to be viewable to the user.
Maybe I missed your point, how were you going to print them? Were you intending to write directly to a printer from PHP? I thought you intended to bring them up in a browser and have the user print them.

I hadn't thought about it, but you could do that. Is PHP the best language to write that in?

Still, it's pretty much the same thing. Instead of print'ing stuff, you'd send it to a print spool.

You could write a page-printer function that takes a chunk of data to print, puts in your header and footer, and then spools the page. Your program would be responsible for sending the right amount of data to each page. Of course, you could also toss together a function that takes the whole whack of data and breaks it up based on size, calling the other function as needed.

I feel like I'm not helping you here. Are you asking how to store data in a database? How to include header/footer files? I don't mean to drive your question off in the wrong direction. Can you refocus me here?

Posted: Tue Mar 16, 2004 10:35 am
by gurjit
what i'm trying to do is send a letter to the printer. i dont want the end user to see what the letter looks like on a template, i want to send the letter straight to a printer.

some letters have dynamic text coming from a database table. this text will get added to the correct parts of the letter.

for example. i have a students acceptance letter. where i pull from the database the students name,address. the timetable for the classes he or she attends. the letter will have dedicated places where to put this data.

when the user hits a button called 'print letter' this letter will read the appropriate information from the databse and send it to the printer.

the letter will have a school logo at the top right and a footer at the bottom with school address. if the text is too long to fit on one page, i want to be able to print the remaming text on page 2 with the company logo and footer re-printed.

sorry if i'm still unclear. please ask more if you are unsure.

how can i spool straight to the printer, pull the dynamic text in the letter and determine the number of pages?

Posted: Tue Mar 16, 2004 10:48 am
by TheBentinel.com
gurjit wrote:how can i spool straight to the printer, pull the dynamic text in the letter and determine the number of pages?
It depends on the O/S your server is running on. If it's unix, then you'd probably use "lp". If it's Windows, then I don't know what you'd use. Maybe "print"?

I don't know how to send images and stuff to the printer. Isn't there a printer language? PostScript, maybe?

You'd need to create a file of what you wanted to print, then throw it to the print spooler via the pass_thru function:

Code: Select all

$result = pass_thru("lp -d myprinter /tmp/myfile12345.txt");
  print($result);
I think PHP is gonna be a small part of this project. Getting the print file built will be the fun part.

Posted: Tue Mar 16, 2004 10:56 am
by gurjit
i'll look into this. seems interesting. i'll get back to you when i fall into traps.

thanks for your help.

so really i need to create a text file and send it to the printer. then delete this text file from the server if i correctly understand your method.

Posted: Tue Mar 16, 2004 10:59 am
by Bill H
what i'm trying to do is send a letter to the printer. i dont want the end user to see what the letter looks like on a template, i want to send the letter straight to a printer.
If you are talking about a printer connected to the Internet user's computer, that's going to be a problem. PHP (i.e. the server) has communication only with the user's browser and nothing beyond that. Imagine the chaos if the Internet could send directly to your disk or printer.

You might be able to do something with javascript, but I suspect content would have to output to the browser first.

Or are you talking about sending it to a printer connected to the server?

Posted: Tue Mar 16, 2004 11:04 am
by gurjit
bill

how would you recommend going about spliting data over 2 pages if displaying on a template first and then printing via javascript?

NOTE: that its not necessary the text on the letter will split over two pages. it may be that its only on one page. all depending on the dynamic data pulled from the database. if it does go over 2 pages then the school logo and footer with the remamining body text should print on page 2.

any ideas?

Posted: Tue Mar 16, 2004 11:06 am
by Bill H
Let me be clear, are you talking about a users printer?

Posted: Tue Mar 16, 2004 11:11 am
by gurjit
i'm talking about printing a letter on a users printer.

the user would click a button called 'print letter'. the letter template will get generated and sent to the printer for printing.

i will be printing on standard A4 USA size paper.

Posted: Tue Mar 16, 2004 11:33 am
by TheBentinel.com
Bill H wrote:Let me be clear, are you talking about a users printer?
Ouch, I didn't think you meant the user's printer. I thought you were referring to printing on a printer connected to your web server, or on the same internal network as your printer.

I agree with Bill, if you intend a user to click a button then have something print on his own printer (and he's not on the same intranet as your webserver) then it will be a very complicated or impossible problem.

You'd be better off to format your letter in html and have the user print it. You could automatically fire the print command from javascript. All the previous caveats about formatting print documents in html apply.

Posted: Tue Mar 16, 2004 11:38 am
by gurjit
so how do i in php cut the letter off and continue printing on page 2 with the header and footer re-printing on the users printer?
any commands in php that can help?

Posted: Tue Mar 16, 2004 11:53 am
by TheBentinel.com
gurjit wrote:so how do i in php cut the letter off and continue printing on page 2 with the header and footer re-printing on the users printer?
any commands in php that can help?
If you go the HTML route, then you'd send the whole thing to the user's browser and then print the whole thing at once. PHP won't itself have any notion of "pages", but you can tell the browser about it.

Your challenge will be to figure out how much to stick on a page. You can't go by bytes, since the following is 5 lines, but only 10 bytes:
a
b
c
d
e

while this sentence has many bytes, and it's only one line. You'll need to determine how long lines can be and where the carriage returns are to figure out how much to dump to a single page.

The HTML isn't too bad, though.

You'd have a header, and a footer that outputs your logo and stuff. You could hold that in variables and print them whenever you need to. Your footer would probably have the page-break-after code that the browser will know to page break on.

Then you'd have a pageprint function that prints the header, your page text, and your footer.

The main PHP script would get the text from the database, then feed it a page at a time to the pageprint function.

Posted: Tue Mar 16, 2004 12:15 pm
by Bill H
TheBentinel has pretty much spelled it out for you. A couple things more:

Due to differences in browser settings, be sure to make all your html display commands (table cells and fonts) in pixels rather than in relative sizes.

I usually just instruct the viewer to click the browser "Print" button rather that fooling around with a javascript command. Many viewers have javascript turned off.

If you have two pages you will need something for them to click to get to the next page. A button is crummy, since it will print on the page. I usually make the logo the thing, since it is an image and doesn't become altered by the viewer's browser when it is made into a link. You then need some instructions before the letter is presented that tell the viewer how to print it.

PHP sessions are extremely useful for displaying multi-page reports (letters).

Good luck. Get started on it and post back if you have problems. We'll help.