48
32
Chapter 2: PDFlib Language Bindings
This will display a long info page about your current PHP configuration. On this page
check the section titled pdf. If this section contains PDFlib GmbH Version (and the
PDFlib version number) you are using the supported new PDFlib wrapper. The un-
supported old wrapper will display PDFlib Version instead.
> Load PDFlib at runtime with one of the following lines at the start of your script:
dl("libpdf_php.so");
# for Unix
dl("php_pdf.dll");
# for Windows
Modified error return for PDFlib functions in PHP. Since PHP uses the convention of re-
turning the value 0 (FALSE) when an error occurs within a function, all PDFlib functions
have been adjusted to return 0 instead of -1 in case of an error. This difference is noted
in the function descriptions in Section 7, »API Reference for PDFlib, PDI, and PPS«, page
135. However, take care when reading the code fragment examples in Section 3, »PDFlib
Programming«, page 41 since these use the usual PDFlib convention of returning -1 in
case of an error.
2.9.2 The »Hello world« Example in PHP
<?php
$p = PDF_new();
PDF_open_file($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($p, 595, 842); /* start a new page */
# Change "host" encoding to "winansi" or whatever you need!
$font = PDF_load_font($p, "Helvetica-Bold", "host", "");
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($p);
/* close page*/
PDF_close($p);
/* close PDF document*/
$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);
/* delete the PDFlib object */
?>
2.9.3 Error Handling in PHP
When a PDFlib exception occurs, a PHP exception is thrown. Unfortunately, PHP does
not yet support structured exception handling: there is no way to catch exceptions and
38
2.10 Python Binding 33
act appropriately. Do not disable PHP exceptions when using PDFlib, or you will run
into serious trouble.
PDFlib warnings (nonfatal errors) are mapped to PHP warnings, which can be dis-
abled in php.ini. Alternatively, warnings can be disabled at runtime with a PDFlib func-
tion like in any other language binding:
PDF_set_parameter($p, "warning", "false");
2.10 Python Binding
2.10.1 Installing the PDFlib Python Edition
The Python
1
extension mechanism works by loading shared libraries at runtime. For the
PDFlib binding to work, the Python interpreter must have access to the PDFlib Python
wrapper:
Unix. The library pdflib_py.so will be searched in the directories listed in the PYTHON-
PATH environment variable.
Windows. The library pdflib_py.dll will be searched in the directories listed in the PY-
THONPATH environment variable.
2.10.2 The »Hello world« Example in Python
from sys import *
from pdflib_py import *
p = PDF_new()
if PDF_open_file(p, "hello_py.pdf") == -1:
print "Couldn't open PDF file 'hello_py.pdf'\n"
exit(2);
PDF_set_info(p, "Author", "Thomas Merz")
PDF_set_info(p, "Creator", "hello.py")
PDF_set_info(p, "Title", "Hello world (Python)")
PDF_begin_page(p, 595, 842)
font = PDF_load_font(p, "Helvetica-Bold", "host", "")
PDF_setfont(p, font, 18.0)
PDF_set_text_pos(p, 50, 700)
PDF_show(p, "Hello world!")
PDF_continue_text(p, "(says Python)")
PDF_end_page(p)
PDF_close(p)
PDF_delete(p);
1. See http://www.python.org
40
34
Chapter 2: PDFlib Language Bindings
2.10.3 Error Handling in Python
The Python binding installs a special error handler which translates PDFlib errors to na-
tive Python exceptions. The Python exceptions can be dealt with by the usual try/catch
technique:
try:
...some PDFlib instructions...
except:
print 'Exception caught!'
2.11 RPG Binding
PDFlib provides a /copy module that defines all prototypes and some useful constants
needed to compile ILE-RPG programs with embedded PDFlib functions.
Since all functions provided by PDFlib are implemented in the C language, you have
to add x'00' at the end of each string value passed to a PDFlib function. All strings re-
turned from PDFlib will have this terminating x'00' as well.
2.11.1 Compiling and Binding RPG Programs for PDFlib
Using PDFlib functions from RPG requires the compiled PDFlib service program. To in-
clude the PDFlib definitions at compile time you have to specify the name in the D
specs of your ILE-RPG program:
d/copy QRPGLESRC,PDFLIB
If the PDFlib source file library is not on top of your library list you have to specify the li-
brary as well:
d/copy PDFsrclib/QRPGLESRC,PDFLIB
Before you start compiling your ILE-RPG program you have to create a binding directory
that includes the PDFLIB service program shipped with PDFlib. The following example
assumes that you want to create a binding directory called PDFLIB in the library PDFLIB:
CRTBNDDIR BNDDIR(PDFLIB/PDFLIB) TEXT('PDFlib Binding Directory')
After creating the binding directory you need to add the PDFLIB service program to your
binding directory. The following example assumes that you want to add the service pro-
gram PDFLIB in the library PDFLIB to the binding directory created earlier.
ADDBNDDIRE BNDDIR(PDFLIB/PDFLIB) OBJ((PDFLIB/PDFLIB *SRVPGM))
Now you can compile your program using the CRTBNDRPG command (or option 14 in
PDM):
CRTBNDRPG PGM(PDFLIB/HELLO) SRCFILE(PDFLIB/QRPGLESRC) SRCMBR(*PGM) DFTACTGRP(*NO)
BNDDIR(PDFLIB/PDFLIB)
2.11.2 The »Hello world« Example in RPG
*****************************************************************************************
d/copy QRPGLESRC,PDFLIB
*****************************************************************************************
59
2.11 RPG Binding 35
d p S *
d font s 10i 0
*
d error s 50
*
d filename s 256
d fontname s 50
d fontenc s 50
d infokey s 50
d infoval s 200
d text s 200
d n s 1 inz(x'00')
*****************************************************************************************
c clear error
*
* Init on PDFlib
c eval p=pdf_new
c if p=*null
c eval error='Couldn''t create PDFlib object '+
c '(out of memory)!'
c exsr exit
c endif
*
* Open new pdf file
c eval filename='hello_rpg.pdf'+x'00'
c if PDF_open_file(p:filename) = -1
c eval error='Cannot open PDF file '+
c %trim(filename)
c exsr exit
c endif
* Set info "Creator"
c eval infokey='Creator'+x'00'
c eval infoval='hello.rpg'+x'00'
c callp PDF_set_info(p:infokey:infoval)
* Set info "Author"
c eval infokey='Author'+x'00'
c eval infoval='Thomas Merz'+x'00'
c callp PDF_set_info(p:infokey:infoval)
* Set info "Title"
c eval infokey='Title'+x'00'
c eval infoval='Hello, world (RPG)!'+x'00'
c callp PDF_set_info(p:infokey:infoval)
* Start a new page
c callp PDF_begin_page(p:a4_width:a4_height)
*
c eval fontname='Helvetica-Bold'+x'00'
* Change "host" encoding to "ebcdic" or whatever you need!
c eval fontenc='host'+x'00'
c eval font=PDF_load_font(p:fontname:0:fontenc:n)
*
c callp PDF_setfont(p:font:24)
c callp PDF_set_text_pos(p:50:700)
*
c eval text='Hello world!'+x'00'
c callp PDF_show(p:text)
c eval text='(says ILE RPG)'+x'00'
c callp PDF_continue_text(p:text)
* Close page
53
36
Chapter 2: PDFlib Language Bindings
c callp PDF_end_page(p)
* Close PDF document
c callp PDF_close(p)
* Delete the PDF object
c callp PDF_delete(p)
*
c exsr exit
*****************************************************************************************
c exit begsr
c if error<>*blanks
c error dsply
c endif
c seton lr
c return
c endsr
You can compile this program as follows:
CRTBNDDIR BNDDIR(PDFLIB/PDFLIB) TEXT('PDFlib Binding Directory')
ADDBNDDIRE BNDDIR(PDFLIB/PDFLIB) OBJ((PDFLIB/PDFLIB *SRVPGM))
CRTBNDRPG PGM(PDFLIB/HELLO) SRCFILE(PDFLIB/QRPGLESRC) SRCMBR(*PGM) DFTACTGRP(*NO) +
BNDDIR(PDFLIB/PDFLIB)
2.11.3 Error Handling in RPG
PDFlib clients written in ILE-RPG can install an error handler in PDFlib which will be ac-
tivated when an exception occurs. Since ILE-RPG translates all procedure names to up-
percase, the name of the error handler procedure should be specified in uppercase. The
following skeleton demonstrates this technique:
*****************************************************************************************
d/copy QRPGLESRC,PDFLIB
*****************************************************************************************
d p S *
d font s 10i 0
*
d error s 50
*
d errhdl s * procptr
*
* Prototype for exception handling procedure
*
d errhandler PR
d p * value
d type 10i 0 value
d shortmsg 2048
*****************************************************************************************
c clear error
*
* Set the procedure pointer to the ERRHANDLER procedure.
*
c eval errhdl=%paddr('ERRHANDLER')
*
c eval p=pdf_new2(errhdl:*null:*null:*null:*null)
...PDFlib instructions...
c callp PDF_delete(p)
50
2.11 RPG Binding 37
*
c exsr exit
*****************************************************************************************
c exit begsr
c if error<>*blanks
c error dsply
c endif
c seton lr
c return
c endsr
*****************************************************************************************
* If any of the PDFlib functions will cause an exception, first the error handler
* will be called and after that we will get a regular RPG exception.
c *pssr begsr
c exsr exit
c endsr
*****************************************************************************************
* Exception Handler Procedure
* This procedure will be linked to PDFlib by passing the procedure pointer to
* PDF_new2. This procedure will be called when a PDFlib exception occurs.
*
*****************************************************************************************
p errhandler B
d errhandler PI
d p * value
d type 10i 0 value
d c_message 2048
*
d length s 10i 0
*
* Chop off the trailing x'00' (we are called by a C program)
* and set the error (global) string
c clear error
c x'00' scan c_message length 50
c sub 1 length
c if *in50 and length>0
c if length>%size(error)
c eval error=c_message
c else
c eval error=%subst(c_message:1:length)
c endif
c endif
*
* Always call PDF_delete to clean up PDFlib
c callp PDF_delete(p)
*
c return
*
p errhandler E
40
38
Chapter 2: PDFlib Language Bindings
2.12 Tcl Binding
2.12.1 Installing the PDFlib Tcl Edition
The Tcl
1
extension mechanism works by loading shared libraries at runtime. For the
PDFlib binding to work, the Tcl shell must have access to the PDFlib Tcl wrapper shared
library and the package index file pkgIndex.tcl. You can use the following idiom in your
script to make the library available from a certain directory (this may be useful if you
want to deploy PDFlib on a machine where you don’t have root privilege for installing
PDFlib):
lappend auto_path /path/to/pdflib
Unix. The library pdflib_tcl.so must be placed in one of the default locations for shared
libraries, or in an appropriately configured directory. Usually both pkgIndex.tcl and
pdflib_tcl.so will be placed in the directory
/usr/lib/tcl8.3/pdflib
Windows. The files pkgIndex.tcl and pdflib_tcl.dll will be searched for in the directories
C:\Program Files\Tcl\lib\pdflib
C:\Program Files\Tcl\lib\tcl8.3\pdflib
2.12.2 The »Hello world« Example in Tcl
package require pdflib 5.0.1
set p [PDF_new]
if {[PDF_open_file $p "hello_tcl.pdf"] == -1} {
puts stderr "Couldn't open PDF file!"
exit
}
PDF_set_info $p "Creator" "hello.tcl"
PDF_set_info $p "Author" "Thomas Merz"
PDF_set_info $p "Title" "Hello world (Tcl)"
PDF_begin_page $p 595 842
set font [PDF_load_font $p "Helvetica-Bold" "host" "" ]
PDF_setfont $p $font 18.0
PDF_set_text_pos $p 50 700
PDF_show $p "Hello world!"
PDF_continue_text $p "(says Tcl)"
PDF_end_page $p
PDF_close $p
PDF_delete $p
1. See http://dev.scriptics.com
8
2.12 Tcl Binding 39
2.12.3 Error Handling in Tcl
The Tcl binding installs a special error handler which translates PDFlib errors to native
Tcl exceptions. The Tcl exceptions can be dealt with by the usual try/catch technique:
if [ catch { ...some PDFlib instructions... } result ] {
puts stderr "Exception caught!"
puts stderr $result
}
2
40
Chapter 2: PDFlib Language Bindings
Documents you may be interested
Documents you may be interested