[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Convert PHP report to PDF for online printing

Posted on 2010-04-07
4
Medium Priority
?
535 Views
Last Modified: 2013-12-13
I want to take a dynamic summary report, which has been created in PHP, and output it as a PDF.  What is the best way to achieve this?  User-friendly, ideally.
0
Comment
Question by:ssailer
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
g105b earned 2000 total points
ID: 30053162
The PDF functions in PHP can create PDF files using the PDFlib library which was initially created by Thomas Merz and is now maintained by PDFlib GmbH.

The below code shows how to construct basic PDF file from PHP.

If you don't have access to this library, or can't install it on your server, there is a free PHP class called FPDF that can be used to create server-side PDFs. This can be found at http://www.fpdf.org/

Take a look at the PHP docs about the inbuilt PDFlib - http://php.net/manual/en/book.pdf.php
<?php
$p = PDF_new();

/*  open new PDF file; insert a file name to create the PDF on disk */
if (PDF_begin_document($p, "", "") == 0) {
    die("Error: " . PDF_get_errmsg($p));
}

PDF_set_info($p, "Creator", "hello.php");
PDF_set_info($p, "Author", "Rainer Schaaf");
PDF_set_info($p, "Title", "Hello world (PHP)!");

PDF_begin_page_ext($p, 595, 842, "");

$font = PDF_load_font($p, "Helvetica-Bold", "winansi", "");

PDF_setfont($p, $font, 24.0);
PDF_set_text_pos($p, 50, 700);
PDF_show($p, "Hello world!");
PDF_continue_text($p, "(says PHP)");
PDF_end_page_ext($p, "");

PDF_end_document($p, "");

$buf = PDF_get_buffer($p);
$len = strlen($buf);

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

PDF_delete($p);
?>

Open in new window

0
 

Author Comment

by:ssailer
ID: 30058159
Where, in this string of code, would I put my dynamic content?  I'm assuming I would be the recordset query at the top of the page?  Thanks.
0
 
LVL 2

Expert Comment

by:g105b
ID: 30061972
You can use the PDF_show or PDF_continue_text functions to output content just like an echo does. There are plenty of examples in the php docs on http://php.net/manual/en/book.pdf.php
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

590 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