FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » PHP: convert a page to pdf
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: PHP: convert a page to pdf [message #180946 is a reply to message #180945] Thu, 28 March 2013 10:57 Go to previous messageGo to previous message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma:
Senior Member
On 28/03/13 08:40, badmus aliu wrote:
> can anyone help me on how to convert a database driven page to pdf using php
>
google PDFliblite - its a free version of a commercial library that
gives you the tools to make a reasonable PDF page.


http://www.pdflib.com/download/free-software/pdflib-lite-7/
http://pecl.php.net/package/pdflib


Its not to be used for profit - then you should buy the commercial version.

You may need to install a few fonts on your web server as well as the
library and the PHP library shims if you want to use fonts other than
the basic postcript ones. All of this is a bit clunky, but once there it
works.

here is a library in PDF I created to print, download or email invoices
as PDF attachments. Using the PDFliblite functions as primitives.

<?php
// Here we go then. Simple functions to set up text an images into PDF
format.
// Probably worth having some global variables../

$font=0; //current font handle
$fontstyle="normal";
$fontpoint=10;
$pdf=""; // the current resource.

function setfont($size,$weight)
{
global $fontpoint;
$fontpoint=$size;
global $fontstyle;
global $font;
global $pdf;
if ($fontstyle!=$weight)
{
if ($weight=='normal')
$font=pdf_load_font($pdf,"Helvetica","iso8859-1","");
else if ($weight=='bold')
$font=pdf_load_font($pdf,"Helvetica-Bold","iso8859-1","");
else if ($weight=='italic')
$font=pdf_load_font($pdf,"Helvetica-Oblique","iso8859-1","");
else if ($weight=='bold-italic')
$font=pdf_load_font($pdf,"Helvetica-BoldOblique","iso8859-1","");
$fontstyle=$weight;
}
pdf_setfont($pdf,$font,$fontpoint);
}

function end_page()
{
global $pdf;
PDF_end_page($pdf);
}
function close_document()
{
global $pdf;
pdf_close($pdf);
}
function new_page($title)
{
global $font,$fontweight,$fontstyle,$fontpoint,$pdf;
pdf_begin_page($pdf,595,842);
box(15,750,565,50,"#D0D0D0",0);
setfont(14,'bold');
$img=pdf_load_image($pdf,"gif","../Images/Logo2.gif","" );
pdf_place_image($pdf,$img,25,777,0.10);
text(297.5,760,$title,'centre');
setfont(8,'normal');
date_default_timezone_set ("Europe/London");
text(470,790,date("G:i T: D M d Y"),'left');
}
function new_document()
{
global $pdf;
$pdf=PDF_new();
pdf_begin_document($pdf,"","compatibility=1.5");
}

function text($x,$y,$text,$align)
{
global $pdf,$font,$fontpoint;
pdf_setfont($pdf,$font,$fontpoint);
// $text=mb_convert_encoding($text, "ISO-8859-1","UTF-8");
if($align=='right')
$x-=pdf_stringwidth($pdf,$text,$font,$fontpoint);
else if ($align=='centre')
$x-=pdf_stringwidth($pdf,$text,$font,$fontpoint)/2;
pdf_show_xy($pdf,$text,$x,$y);
return pdf_stringwidth($pdf,$text,$font,$fontpoint);
}

function box($x,$y,$width,$height,$color) // color is #XXXXXX
{
global $pdf;
setcolor($color);
pdf_rect($pdf,$x,$y,$width,$height);
pdf_fill($pdf);
setcolor("#000000"); // back to black.
}
function setcolor($color)
{
global $pdf;
$string = str_replace("#","",$color);
$red = hexdec(substr($string,0,2)) / 255;
$green = hexdec(substr($string,2,2)) / 255;
$blue = hexdec(substr($string,4,2)) / 255;
pdf_setcolor($pdf,"fill",'rgb', $red, $green, $blue,0);
}
function send_pdf_file($name)
{
global $pdf;
$buf = PDF_get_buffer($pdf);
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=\"".$name.".pdf\"");
print $buf;
}
function print_pdf_file($printer)
{
global $pdf;
$buf = PDF_get_buffer($pdf);
$len=strlen($buf);
$handle=popen("/usr/bin/lp -o sides=one-sided -d ".$printer." -- -", 'w');
fwrite($handle,$buf);
}

function mail_pdf_file($email, $subject,$from,$return_path,$body,$filename)
{
global $pdf;
$buf=PDF_get_buffer($pdf);
$attach=chunk_split( base64_encode($buf),72, "\n");
$boundary = md5( time() );
$headers = sprintf("From: %s\nMIME-Version: 1.0\nContent-Type:
multipart/mixed; boundary=\"%s\"",
$from, $boundary);
$message = sprintf("\nThis is a multi-part message in MIME
format.\n--%s\nContent-Type: text/plain; charset=UTF-8;
format=flowed\nContent-Transfer-Encoding:
7bit\n\n%s\n\n--%s\nContent-Type: application/pdf;
name=\"%s\"\nContent-Disposition: inline;
filename=\"%s\"\nContent-Transfer-Encoding: base64\n\n%s\n\n--%s--\n",
$boundary,$body,$boundary,$filename, $filename,$attach,$boundary);
mail($email, $subject, $message, $headers, "-f ".$return_path );
}
?>



--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: how to save the visitors ip addresses
Next Topic: Need Forex Feed in PHP
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 11:16:59 GMT 2024

Total time taken to generate the page: 0.04103 seconds