[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

Export Records as pdf from website

Hi all,

I have a journal page on a site and I want to be able to offer users the ability to export all of their journal entries as a pdf document. I have all of the fields setup that retrieve the users information (using PHP/MYSQL) and just want to a user to be able to click on a button on the page and their records are saved in a pdf document for them locally.

Thanks for any advice on how to do this,

D
0
TLN_CANADA
Asked:
TLN_CANADA
  • 3
  • 2
1 Solution
 
mcnuteCommented:
You'll need something like fpdf to generate a pdf with your mysql query results.

Step 1 save query results in array
Step 2 generate pdf with looped results of query
Step 3 output to browser with header() php method.

Everything is explained very well on the fpdf website.
http://www.fpdf.org/
0
 
Ray PaseurCommented:
Both FPDF and TCPDF can do the job.  Both are free, open-source code sets; you can download them, store them in your server web root tree and begin making PDF documents in a matter of minutes. FPDF seems to be older and has not had much development activity in recent years, but for publishing PDF documents it worked well for me, as did TCPDF.

I think FPDF documentation is a little stronger than TCPDF.  I never figured out some parts of TCPDF, such as the setImageScale() method.

I found that TCPDF was easier to handle in some regards.  For example, FPDF seemed to want to put some padding around things in the headers and footers of pages.  With TCPDF I was able to override these methods and thereby nullify the automatic creation of unwanted page elements.

Both classes require meticulous attention to detail.  You're doing document layout, and you have positioning control down to the millimeter.  Both classes can place images and text in the PDF document.  

Here is my "hello world" example for FPDF.

<?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

And here is my hello world example for TCPDF (writes text over an image).

<?php // RAY_tcpdf_image_position.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');

// SYNTHESIZE THE PDF FILE NAME
$pdf_file_link    = '/RAY_tcpdf_example.pdf';
$pdf_file_name    = getcwd() . $pdf_file_link;

// THE TCPDF CLASS AND CONFIGURATION
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');

// EXTEND THE TCPDF OBJECT
class PDF extends TCPDF
{
    // NULLIFY AUTOMATIC HEADER AND FOOTER
    public function Header() {}
    public function Footer() {}
}

echo "<br />Starting PDF creation\n";

// INSTANTIATE THE OBJECT
$pdf = new PDF('P', 'mm', 'LETTER', true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(0,0,0,TRUE);
$pdf->SetAutoPageBreak(FALSE);
$pdf->setLanguageArray($l);
$pdf->SetFont('times', '', 13);
$pdf->setCellPaddings(0,0,0,0);
$pdf->setCellMargins(0,0,0,0);

// DO NOT DO THIS
// $pdf->setFillColor(255,255,224);


// CELL BORDERS, IF NEEDED
$bdr = '1TRBL';
$bdr = 0;

// ADD A PAGE
$pdf->AddPage('P', 'LETTER', TRUE);

// ADD AN IMAGE
// http://www.tcpdf.org/doc/classTCPDF.html#a714c2bee7d6b39d4d6d304540c761352

$img = 'RAY_EE_images/ray_padded.png';

// I HAVE NO IDEA WHAT THIS IS DOING
$pdf->setImageScale(1.53);

// GET THE IMAGE, I HOPE
$pdf->Image
( $img
, 0             // $x
, 0             // $y
, 0             // WIDTH
, 0             // HEIGHT
, 'PNG'         // TYPE
, '#'           // LINK URL
, 'T'           // SET POINTER TOP LEFT
, FALSE         // NO RESIZING
, 300           // DPI
, 'L'           // PALIGN
, FALSE         // ISMASK
, FALSE         // IMGMASK
, 0             // BORDER
, FALSE         // FIT TO BOX
, FALSE         // HIDDEN
, FALSE         // FIT ON PAGE
)
;

// ADD SOME TEXT ON TOP OF THE IMAGE
$pdf->Text
( 24            // $x,
, 24            // $y,
, 'HELLO WORLD'
, FALSE         // $fstroke = false,
, FALSE         // $fclip = false,
, TRUE          // $ffill = true,
, $bdr          // $border = 0,
, 2             // $ln = 0, 2=PUT CURSOR BELOW
, ''            // $align = '', DEFAULT LEFT
, FALSE         // $fill = false, CELL BACKGROUND
, ''            // $link = '', NOT A LINK
, 0             // $stretch = 0, NO TEXT STRETCH
, FALSE         // $ignore_min_height = false,
, 'A'           // $calign = 'T', A=FONT TOP INSIDE CELL
, 'T'           // $valign = 'M', T=VERTICAL ALIGN INSIDE CELL
, TRUE          // $rtloff = false TRUE = USE PAGE TOP LEFT CORNER TO ALIGN
)
;

// WRITE THE PDF FILE TO THE SERVER
echo "<br />Writing PDF\n";
$pdf->Output($pdf_file_name,'F');

// PRESENT A CLICKABLE LINK SO WE CAN D/L AND PRINT THE LABELS
echo "<br /><a target=\"my_PDF\" href=\"$pdf_file_link\"><strong>Print the PDF</strong></a>";

Open in new window

HTH, ~Ray
0
 
TLN_CANADAAuthor Commented:
Thank you Ray, this looks relatively simple alright. I went with your FPDF example, included this file in a page I already have and it gives the error:

FPDF error: Unable to create output file: /home/clear555/public_html/RAY_junk/temp_pdf_blue.pdf

Do you need to change this temp file output in your code?

Thanks,

D
0
Industry Leaders: 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!

 
Ray PaseurCommented:
My guess is that you need to create a directory named RAY_junk on your server. or generalize the solution to fit your particular needs and your required file paths. On my server, this is a public-facing script, so I keep all of the products in that directory and I remove the contents of the RAY_junk directory from time to time.  Garbage collection usually occurs on Sunday night.
0
 
TLN_CANADAAuthor Commented:
Working now, sorry I misread the code and though it was creating a temp directory rather than accessing it. Thanks again for your help Ray!
0
 
Ray PaseurCommented:
Thanks for the points... Maybe I should update the example to look for the directory and use PHP mkdir() if it's not there!  Best regards, ~Ray
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now