Index
General
PDF24.org provides a FREE PDF generation service to create PDF files. This PHP API is an interface to this service.
The API has been developed for blogs, forums, wiki systems and other article-based internet software to create PDF files in an easy way. Developers of blogs, forums and wiki systems can use this API to provide a PDF button.
The API has been developed for blogs, forums, wiki systems and other article-based internet software to create PDF files in an easy way. Developers of blogs, forums and wiki systems can use this API to provide a PDF button.
API Download
Click on the link Download PDF API to download the PHP PDF API.
Class References
There are two main PHP classes which have to be used to create a PDF files. The class PDF24Doc provides the functionality to manage general document content such as document title or document URL and provides methods to add elements to the document.
The class PDF24Element represents a content element inside a PDF24Doc document. A content element is a container holding some data such as a title and an html body.
The following table illustrates this:
The class PDF24Element represents a content element inside a PDF24Doc document. A content element is a container holding some data such as a title and an html body.
The following table illustrates this:
PDF24Doc
PDF24Element 1
PDF24Element 2
PDF24Element 3
Class PDF24Doc
Parameter Keys
charset
The charset of the document. Default is ISO-8859-1. Currently supported values are
ISO-8859-1 and UTF-8.headline
The headline of the document.
headlineUrl
The headline URL of the headline.
baseUrl
The baseUrl of the document. This URL is important if you use relative links in the body content of elements. This URL is used to resolve relative links to find images and other content.
filename
The filename of the created PDF file, e.g. myFileName.
pageSize
The size of each page in the document. The size is encoded as WIDTHxHEIGHT where WIDTH is the width of each page in mm and HEIGHT is the height of each page in mm. The default width is 210mm and the default height is 297mm which represents an ISO A4 page.
emailTo
One or more email addresses separated by a semicolon. This email addresses will receive the created PDF file.
emailFrom
The email address of the API user which will appear as the ‘From’ email address in emails with the attached PDF files.
emailSubject
The subject of the email with the created PDF file attached.
emailBody
The content of the email with the created PDF file attached.
emailBodyType
emailCharset
This parameter holds the charset of the email subject and body. Currently supported values are
ISO-8859-1 and UTF-8.Constructors
PDF24Doc()
Creates a document object with no parameters. Use the setXX methods to set parameters later.
PDF24Doc(params)
Creates a document object and initialises the document parameters with the parameters given in
params.
params is a PHP array with key-value entries. A key is one of the above parameter keys.Methods
addElement(element)
Adds the element
element to the PDF document. element can be a PDF24Element
or a PHP Array with key-value pairs from PDF24Element.setParam(paramKey, paramValue)
Sets a document parameter with the key
paramKey and the value paramValue.setParams(params)
Sets the document parameters in
params. All previously added parameters will be removed.addParams(params)
Adds document parameters from
params. Previously added parameters can be overwritten by this method.
params is an array which contains key-value pairs.getParam(paramKey)
Returns the document parameter with the key
paramKey.setCharset(charset)
Sets the
charset parameter of the document to charset. The default charset is ISO-8859-1.setHeadline(headline)
Sets the
headline parameter of the document to headline.setHeadlineUrl(headlineUrl)
Sets the
headlineUrl parameter of the document to headlineUrl.setBaseUrl(baseUrl)
Sets the
baseUrl parameter of the document to baseUrl.setFilename(filename)
Sets the
filename parameter of the document to filename.setPageSize(width, height)
Sets the
pageSize parameter of the document to width, height.setEmailTo(emailAddr)
Sets the
emailTo parameter of the document to emailAddr.addEmailTo(emailAddr)
Adds the email address
emailAddr to the list of receivers for the PDF file.setEmailFrom(emailAddr)
Sets the
emailFrom parameter of the document to emailAddr.setEmailSubject(subject)
Sets the
emailSubject parameter of the document to subject.setEmailBodyType(bodyType)
Sets the
emailBodyType parameter of the document to bodyType. bodyType can be text or html.setEmailBody(body)
Sets the
emailBody parameter of the document to body.setEmailCharset(charset)
Sets the charset of body and subject of the email with the attached PDF file.
createAndSend()
This method packs all the given parameters into a PDF24 service request and transmits the request to the PDF24 service. The service checks the request and gives a response which can be analysed. If the response is successful this method returns
true otherwise false is returned.Class PDF24Element
Parameter Keys
title
The title of the element.
url
The URL of the element. The title and the URL are used to form a link.
author
The author of the element’s content.
dateTime
A timestamp (Any string that represents a timestamp, e.g. date and time or only date or time)
body
The content of the element. Can be plain or html formatted text.
Constructors
PDF24Element()
Creates an element with no parameters. Use the setXX methods to set parameters later.
PDF24Element(params)
Creates an element and initializes it with parameters given in
params.
params is a PHP array with key-value entries. A key is one of the above parameter keys of PDF24Element.Methods
setTitle(title)
Sets the
title parameter of the element to title.setUrl(url)
Sets the
url parameter of the element to url. The title and the URL together form a link.setAuthor(author)
Sets the
author parameter of the element to author.setDateTime(dateTime)
Sets the
dateTime parameter of the element to dateTime.setBody(body)
Sets the
body parameter of the element to body. body can be plain or html formatted text.setParam(key,value)
Sets the value of a parameter with the key
key to value.setParams(params)
Sets the parameters in
params. All previously added parameters will be removed.
params is an array containing key-value pairs.addParams(params)
Adds parameter given in
params. params is a PHP array
containing key-value pairs. Previously added parameters can be overwritten.getParam(key)
Returns the value of a parameter belonging to
key.Class PDF24Response
Parameters
header
The header of the response to a service request.
body
The body of the response to a service request.
Methods
getHeader()
Returns the header of the response to a service request.
getBody()
Returns the body of the response to a service request.
__toString()
Returns a string representation of the header and body of a service request .
Code samples
Include the API first:
include(api.php);
Sample 1
/*
* Crate a document and add parameter
*/
$doc = new PDF24Doc();
$doc->setCharset('ISO-8859-1');
$doc->setHeadline('This is the headline of the PDF');
$doc->setHeadlineUrl('http://www.pdf24.org');
$doc->setBaseUrl('http://www.pdf24.org');
$doc->setFilename('test');
$doc->setPageSize(210, 297);
$doc->setEmailTo('stefanz@pdf24.org');
$doc->setEmailFrom('stefanz@pdf24.org');
$doc->setEmailSubject('Here is your created PDF file');
$doc->setEmailBody('The created PDF file is attached to this email!');
$doc->setEmailBodyType('text');
$doc->setEmailCharset('ISO-8859-1');
/*
* Create one or more elements
*/
$element = new PDF24Element();
$element->setTitle('This is the title of the element');
$element->setUrl('http://www.pdf24.org');
$element->setAuthor('Stefan Ziegler');
$element->setDateTime('2010-04-15 8:00');
$element->setBody('The is the body of the element');
/*
* Add elements
*/
$doc->addElement($element);
/*
* Create the PDF. Print response if there has been an error.
*/
if(!$doc->createAndSend()) {
echo $doc->getResponse();
}
Example 2
/*
* Create Document with parameter
*/
$doc = new PDF24Doc(array(
'charset' => 'UTF-8',
'headline' => 'This is a headline',
'headlineUrl' => 'http://www.pdf24.org',
'baseUrl' => 'http://www.pdf24.org',
'filename' => 'test',
'pageSize' => '210x297',
'emailTo' => 'stefanz@pdf24.org',
'emailFrom' => 'stefanz@pdf24.org',
'emailSubject' => 'Here is your created PDF file',
'emailBody' => 'The PDF file is attached to this email!',
'emailBodyType' => 'text'
));
/*
* Add an element without using PDF24Element
*/
$doc->addElement(array(
'title' => 'This is the title of the element',
'url' => 'http://www.pdf24.org',
'author' => 'Stefan Ziegler',
'dateTime' => '2010-04-15 8:00',
'body' => 'This is the body of the element'
));
/*
* Create the PDF. Print response if there has been an error.
*/
if(!$doc->createAndSend()) {
echo $doc->getResponse();
}
PDF Generation Test
There is a Javascript variant of the PDF API which uses the same PDF24 service. The information pages of the Javascript variant contain a Test-Generator. Look at
Javascript PDF Generator