Solved

PHP, create multi-page txt file

Posted on 2012-03-21
5
258 Views
Last Modified: 2012-03-21
Hello,

I have a list of names and addresses in my db, I would like to be able to output a multi-page txt file so that each name and address in on a separate page.

How do I create a multi page txt file using php?

Thank you,
Matt
0
Comment
Question by:movieprodw
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:Kim Walker
ID: 37749118
Can you provide more detail about what you need? A txt file has nothing but characters and line breaks. It doesn't contain formatting or styles, or layout such as page breaks. Please describe in detail the type of file you wish to generate.
0
 
LVL 1

Author Comment

by:movieprodw
ID: 37749185
Hello,

I am using a label printer and it will print each page as a label so I just need the txt file or even a pdf file (whichever is easier) to create one document that is broken up by pages.

Example:

Username
Birthdate
phonenumber

(NEW PAGE)
Username
Birthdate
phonenumber

(NEW PAGE)
Username
Birthdate
phonenumber

(NEW PAGE)
Username
Birthdate
phonenumber

(NEW PAGE)
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37749249
Consider using the excellent free and open source FPDF class.  It takes a bit of work to get the formatting correct the first time, so build that into your schedule.  Once it is working, it's lightning fast and provides very precise positioning of the printed data.  I've used it for labels, postcards, business cards and baseball rosters.

http://fpdf.org
<?php // RAY_fpdf_hello_world.php
error_reporting(E_ALL);


// DEMONSTRATE THE BASICS OF FPDF


// SOME VARIABLES FOR OUR TESTS (COULD COME FROM DATA BASE, ETC)
$font = 'Arial';
$text = 'Hello World!';

// BRING IN THE PDF THING
require_once('fpdf16/fpdf.php');

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= DIRECTORY_SEPARATOR
. 'RAY_junk'
. DIRECTORY_SEPARATOR
. 'temp_pdf_blue'
. '.pdf'
;
$pdf_file_name
= getcwd()
. $pdf_file_link
;

// DO THE HELLO WORLD EXERCISE IN WHITE ON A BLUE BACKGROUND
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont($font, 'B', 16);
$pdf->SetFillColor(  0,   0, 255);
$pdf->SetTextColor(255, 255, 255);
$pdf->Cell(40, 10, $text, 0, 2, 'L', TRUE);

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">Blue PDF</a>';



// START OVER FOR A NEW PDF
unset($pdf);
echo PHP_EOL . "<br/>";

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= DIRECTORY_SEPARATOR
. 'RAY_junk'
. DIRECTORY_SEPARATOR
. 'temp_pdf_red'
. '.pdf'
;
$pdf_file_name
= getcwd()
. $pdf_file_link
;

// DO THE HELLO WORLD EXERCISE IN WHITE ON A RED BACKGROUND
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont($font, 'B', 16);
$pdf->SetFillColor(255,   0,   0);
$pdf->SetTextColor(255, 255, 255);
$pdf->Cell(40, 10, $text, 0, 2, 'L', TRUE);

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">Red PDF</a>';

Open in new window

0
 
LVL 1

Author Closing Comment

by:movieprodw
ID: 37749423
Already have it up and running, as usual you have saved me lots of time, thank you Ray!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37749680
Thanks for the points, and best of luck with your project, ~Ray
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question