Have your PHP web pages produce PDF documents on the fly 
We were looking for an easy way to produce quotes from our web site.
Ideally the quote would be a PDF document automatically emailed to the applicant and tailored according to certain criteria input from a simple form.

We were pleased to discover how easy it proved to be to create PDF documents.

Here is some sample code. The comments should be self explanatory.

In this case PHP is running with Apache on a Windows server.


<?php

# file path to pdf document
# return the actual path to the script and append to it the
# subfolder where the quotes are to be stored
$mypath = getcwd();
$mypath = str_replace("\\","/",$mypath);
$mypath = $mypath."/quotes/";
#So $mypath will be something like "D:/WEBS/mysite/pdf/quotes/";

# create unique filename based on date and time
$myfile = $mypath."quote-".strftime("%y%m%d-%H%M%S").".pdf";

# create handle for new PDF document
$mypdf = pdf_new();

# open a file for the new document
pdf_open_file($mypdf, $myfile);

# start a new page (A4) - dimensions in points (1/72 of an inch)
pdf_begin_page($mypdf, 595, 842);

# add an image top right. jpg in same folder as script
# coords are bottom left of image and 3rd argument is scaling
# coords are again in 1/72 of an inch
$myimage = pdf_open_image_file($mypdf, "jpeg", "logo.jpg");
pdf_place_image($mypdf, $myimage, 420, 760, 0.5);

# get and use a font object
# to add our address below our logo
$tahoma = pdf_findfont($mypdf, "Tahoma", "host", 1);
pdf_setfont($mypdf, $tahoma, 9);
pdf_show_xy($mypdf, "4 Hamilton Business Park WD6 2FR",430, 750);
pdf_show_xy($mypdf, "T 020 8236 9160 F 020 8236 9161",430, 738);
pdf_show_xy($mypdf, "www.technicasolutions.co.uk",430, 726);

# set font to verdana 10 points now
$verdana = pdf_findfont($mypdf, "Verdana", "host", 1);
pdf_setfont($mypdf, $verdana, 10);

# LETTERHEAD DONE

# print text coords in points (1/72 of an inch) with origin at bottom left
# 20 points between lines is about right
pdf_show_xy($mypdf, "This is a test print.",50, 750);
pdf_show_xy($mypdf, "This is line 2 of the test print", 50,730);

# set a colour for the drawing
# colours are set as 0 to 1 so 0,0,1 is pure blue (0,0,255)
pdf_setcolor($mypdf, "stroke", "rgb", 0, 0, 1);

# draw a blue line around middle of page
pdf_moveto($mypdf, 20, 400);
pdf_lineto($mypdf, 575, 400);
pdf_stroke($mypdf);

# note you can also set fills:
# pdf_setcolor($mypdf, "stroke", "rgb", 0, 0, 1);
# pdf_setcolor($mypdf, "fill", "rgb", 1, 1, 0);
# draw and fill a rectangle
# pdf_rect($mypdf, 50, 500, 200, 300);
# pdf_fill_stroke($mypdf);
# or draw a circle
# pdf_circle($mypdf, 400, 600, 100);
# pdf_fill_stroke($mypdf);

# end page
pdf_end_page($mypdf);

# close and save file
pdf_close($mypdf);

# file is now sitting there saved on your server ready for your script to send it

?>



Jason Ozin is a well-known IT professional, technical architect and joint owner of Technica Solutions - www.technicasolutions.co.uk. Jason consults leading UK and international enterprises on real world technical and IT issues. Jason specialises in business infrastructure and IT security matters.

[ add comment ] ( 16962 views )   |  [ 0 trackbacks ]   |  permalink

<<First <Back | 1 | 2 | Next> Last>>