Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP, create multi-page txt file

Posted on 2012-03-21
5
Medium Priority
?
274 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

783 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