Page 8 - FREE_TRIAL6Months
P. 8

CREATING PDF FILES IN LAZARUS AND FREE PASCAL PAGE 1/6

BY MICHAËL VAN CANNEYT

starter           expert

ABSTRACT                                                   Infos
In this article we show how to create PDF files in         This property has some properties that can be set
Lazarus and Free Pascal, in particular the                 (producer, author etc.) which will show up in the
TPDFDocumentcomponent that is distributed with             document properties in A PDF viewer.
Free Pascal.                                               DefaultPaperType
                                                           This is the default paper type to use when creating a

INTRODUCTION                                               new page. By default this is set to A4.
Lazarus has had a PDF creating component since a           DefaultOrientation
very long time: PowerPDF. It was ported from Delphi,       This property determines whether a newly added page
and is used in LazReport to create PDFs from reports.      will be using landscape or portrait orientation.
However, it has several drawbacks: it requires the         DefaultUnitOfMeasure
LCL and a GUI, has no support for embedding fonts,         This property determines the default unit of measure
and does not support unicode fonts.                        used by a page in the PDF document. It is one of Inches,
To remedy this, recently, a new implementation was         Millimeters (the default), Centimeters, Pixels
added to Free Pascal: fpPDF.                               (see below for more information).
                                                           FontDirectory

This new component has the following features:             The location of any fonts that need to be embedded.
? Pure Object Pascal code.                                 Options
? No dependencies on a GUI, external libraries             Various options can be set which will influence the
                                                           kind of PDF code that is generated.The various options
    or OS calls.                                           currently available include:
? Many drawing primitives :                                poOutLine
? Images (any FPC supported format)
? Lines (pen styles)                                       Create a document outline which will be shown in a
? Circles                                                  PDF viewer.
? Rectangles (optionally rounded)                          poCompressText
? Polylines, i.e. draw multiple line segments
                                                           Compress any strings that are written to the document.
    in a single command.                                   poCompressFonts
? Polygons, with 2 fill modes: non-zero winding
                                                           Compress embedded fonts.
   rule and the even-odd fill rule.                        poCompressImages
? Bezier curve support.
? Full TTF support.                                        Compress images that are included in the document.
? Unicode support.                                         poUseRawJPEG
? Optional Font Embedding
                                                           When adding a JPEG image, the image is included as-is.
                                                           The default is to read the image and convert it to PDF

         (Partial embedding is being worked on).           native format.

? Coordinate transformation support                        poNoEmbeddedFonts

    (rotation, scaling and translation).                   Do not embed fonts in the document. The default is to
? Compression support for text and images.                 embed all fonts in the document.
? Embed HTML ilnksBecause it requires no GUI               poPageOriginAtTop
this component is very suitable for use in e.g. a web
application, where no GUI is available. Since it has no    Flip the coordinate system so the origin is at the top,left
external dependencies, and does its own font               corner, instead of the bottom, left corner. See below for
handling, it can be used on any platform that              more information.Most of the work, however, is done
Free Pascal and lazarus support, including the             in code: there is no point-and-click interface for this
various Raspberry Pis. A new Lazarus lazReport PDF         component.

export has already been made with this component.          DRAWING: THE COORDINATE SYSTEM
The use of this component is quite simple and pretty       The PDF standard is derived from the PostScript
straightforward, as we'll demonstrate. The code is         standard. This means that the origin of the drawing
simple enough to port the component to Delphi with         canvas is the bottom-left corner of the page, as shown
little or no effort, if one would like to.                 in figure  . A bigger Y coordinate value means higher

INSTALLING THE COMPONENT                                   up the page.
The PDF component can be installed in the Lazarus          This in differs from the coordinate system used in a
IDE, the package is available from the Blaise Pascal       Canvas of a LCL or VCL application, where the origin
website. It suffices to install lazpdf.lpk in the IDE and  is at the top-left, and where higher Y coordinate values
the TPDFDocument component will appear on the PDF          mean lower on the screen.

tab of the component palette.                                    (width, height)
The component has a few published properties:

LineStyles

This is a collection of line styles which can be used

when drawing lines. A line style defines a color,

linewidth and pen style.                                                          Figure 1:
PageLayout                                                                        PDF coordinate
This property determines how a PDF viewer should                                  system
open the file: showing a single page, 2 pages or in

continuous mode.                                         (0, 0)

Issue Nr 8 2016 BLAISE PASCAL MAGAZINE                                                                            15
   3   4   5   6   7   8   9   10   11   12