Jump to content

Fetch table data into pdf format


vishak

Recommended Posts

1. In order for the code to work, the following line in your php.ini has to be uncommented:

extension=php_pdf.dll

2. For more information about PDFlib library check: PDF Functions

 

<?php require_once('connection_to_db.php'); ?>
<?php
// select data from database for the PDF
mysql_select_db($database_cnnTest, $cnnTest);
$query_rsReportData = "SELECT * FROM categories";
$rsReportData = mysql_query($query_rsReportData, $cnnTest) or die(mysql_error());
$row_rsReportData = mysql_fetch_assoc($rsReportData);
$totalRows_rsReportData = mysql_num_rows($rsReportData);
?>
<?php
// create handle for new PDF document
$pdf = pdf_new();

// open a file
pdf_open_file($pdf, "");

// Set Info
pdf_set_info($pdf, "Author", "Georgi Kralev");
pdf_set_info($pdf, "Title", "Report");
pdf_set_info($pdf, "Creator", "Georgi Kralev");
pdf_set_info($pdf, "Subject", "Report");

// start a new page (A4)
pdf_begin_page($pdf, 595, 842);

// path of your TTF font directory 
$fontdir = "C:\WINDOWS\Fonts";

// Open .TTFs (true type fonts)
pdf_set_parameter($pdf, "FontOutline", "ArialItalic=$fontdir\ariali.ttf");
pdf_set_parameter($pdf, "FontOutline", "ArialBold=$fontdir\ARIALBD.TTF");
pdf_set_parameter($pdf, "FontOutline", "Arial=$fontdir\ARIAL.TTF");

// ------ Start output of the PDF Content ------//
// set the font - Arial Bold 15
$font = pdf_findfont($pdf, "ArialBold", "host",0); pdf_setfont($pdf, $font, 15);  
// output document title
pdf_show_xy($pdf, "Categories Report", 50, 788);
// draw a line
pdf_moveto($pdf, 20, 780);
pdf_lineto($pdf, 575, 780);
pdf_stroke($pdf);

// set the font - Arial Italic 12
$font = pdf_findfont($pdf, "ArialItalic", "host",0); pdf_setfont($pdf, $font, 12);
$y = 750;
// output data header
pdf_show_xy($pdf, "Category:", 50, $y);
$y -= 5;

// set the font - Arial 10
$font = pdf_findfont($pdf, "Arial", "host",0); pdf_setfont($pdf, $font, 10);

// output the data from Database
do 
{       $y -= 15;
       pdf_show_xy($pdf, $row_rsReportData['name'], 50, $y);
} 
while ($row_rsReportData = mysql_fetch_assoc($rsReportData));

// ------ End output of the PDF Content ------//

// end page
pdf_end_page($pdf);

// close and save file
pdf_close($pdf);

$buf = pdf_get_buffer($pdf);
$len = strlen($buf);

header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=report.pdf");
echo $buf;

pdf_delete($pdf);
?>
<?php
mysql_free_result($rsReportData);
?>

Link to comment
Share on other sites

Thank you for your reply.

 

But I am getting an error like:

 

Fatal error: Uncaught exception 'PDFlibException' with message 'Don't fetch buffer contents when writing to file' in C:\wamp\www\2pdf.php:125 Stack trace: #0 C:\wamp\www\2pdf.php(125): pdf_get_buffer(Resource id #6) #1 {main} thrown in C:\wamp\www\2pdf.php on line 125

 

Is this because I am using PHP5?

 

And in PHP5 the extension is php_pdflib, I uncommented php_cpdf too.

Link to comment
Share on other sites

Thank you for your reply.

 

But I am getting an error like:

 

Fatal error: Uncaught exception 'PDFlibException' with message 'Don't fetch buffer contents when writing to file' in C:\wamp\www\2pdf.php:125 Stack trace: #0 C:\wamp\www\2pdf.php(125): pdf_get_buffer(Resource id #6) #1 {main} thrown in C:\wamp\www\2pdf.php on line 125

 

Is this because I am using PHP5?

 

And in PHP5 the extension is php_pdflib, I uncommented php_cpdf too.

 

The function, PDF_get_buffer() can only be used when you are writing a file to memory, and not to a physical file as you are doing now. That's why you get the error "Don't fetch buffer contents when writing to file".

It would be strange if you didn't get the error in your case.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...