TcPDF Report

The Morfik tcPDFReport package contains server-side classes that allow the creation of run-time PDF documents that include Unicode characters and is based upon the open source TCPDF PHP software which can be downloaded from http://www.tecnick.com.

package-view.png
package-down.png


A Quick Walkthrough

To use the Morfik tcPDFReport package simply add the package to the project by utilizing the “Used Packages” command on the project ribbon (see Figure 1 Used Packages Command), or simply drag the package file onto the application main client area. Once the package has been added, a number of server-side classes supporting PDF Report document creation will be available and additional fonts supporting subset of Unicode characters, available from the Font menu item on the ribbon. These fonts should be used when designing reports containing text with international characters.


security-fig1.png
Figure 1: Used Packages Command


Adding Unicode Character Capability to Reports

Adding the capability to represent Unicode characters in reports is accomplished merely by importing the tcPDFReport package. Once added, Unicode characters such as those included in the report below will correctly be generated for the PDF report.

FX Code

Type
Report1 = Class(Report)
    TextLabel1 : TextLabel;
    Procedure DetailBeforePrint(Sender: TWebControl; Canvas: TWebCanvas; Var Print: Boolean); Message;
End;
Implementation
Procedure Report1.DetailBeforePrint(Sender: TWebControl; Canvas: TWebCanvas; Var Print: Boolean);
Begin
    TextLabel1.Caption := ‘Γεια σας κόσμο!’;  /// Hello world ///
End;


You can also simply type text with international characters for TextLabel and other controls displaying text at design-time.

NOTE: Unicode characters will be rendered correctly only if one of tcPDF-specific fonts is used, such as Free Mono, Free Sans and Free Serif.

Advanced usage:

Upon installing tcPDFReport package a pdf-rendering engine different to the default one will be registered. All existing reports will use new engine and produce same output, however you may choose default pdf-rendering engine by overriding CreatePDFCanvas method of Report class.

To increase the capabilities for report generation, just include mfk_tcPDFCanvas in the Uses clause of the Implementation section of the report. This module adds a number of useful methods and properties that can enhance reports.


The mfk_tcPDFCanvas Module

Adding this module to the Uses clause of a Report introduces a number of methods that can be used to enhance PDF reporting capabilities. Some of the more significant methods are discussed below:

FX Code

Function UnScaleCoordinateValue (L : Double) : Integer;


Because of the difference in coordinate systems used by the built-in Morfik PDFCanvas and the package module mfk_PDFCanvas, coordinates need to be converted from one system to the other. The UnScaleCoordinateValue function takes an mfk_PDFCanvas value and converts it to a PDFCanvas value. Please note that all coordinates passed to functions in the mfk_PDFCanvas members are in the original Morfik PDFCanvas measurements to allow mfk_PDFCanvas to function as a drop-in replacement for the PDFCanvas class.

FX Code

Function ScaleCoordinateValue(L : Integer) : Integer;


This function is the reverse of the one above, changing PDFCanvas coordinates to mfk_PDFCanvas coordinates. It is automatically called by any function in the mfk_PDFCanvas module that is passed a coordinate or size.

FX Code

Procedure SelectBrush(BColor : TColor; BStyle : TBrushStyle);


Use this method to set the color and style of a fill operation when drawing objects. When fill is not desired, set BColor to clTransparent and BStyle to bsClear (see code below).

FX Code

Procedure SelectPen(PWidth : Integer; PColor : TColor; PStyle : TPenStyle);


This method is similar to the one above but sets the properties for any lines used in drawing.

FX Code

Procedure SelectFont(FName : String;  FColor : TColor; FSize : Integer; FBold, FItalic, FUnderLine, FStrikeThrough : Boolean);


This method sets the font name, color, size and style for any subsequent text output to the canvas. The code below demonstrates how to draw a non-filled green rectangle around an existing TextLabel when a report is produced. The Rectangle method parameters are for the top-left X and Y coordinates and the bottom-right X and Y coordinates.

FX Code

Procedure Report1.DetailAfterPrint(Sender: TWebControl; Canvas: TWebCanvas);
Begin
    If DrawRectAroundLabel Then
    Begin
        With Canvas as TtcPDFCanvas Do
        Begin
            SelectBrush(clTransparent,bsClear);
            SelectPen(UnScaleCoordinateValue(5), clGreen, psSolid);
            Rectangle(UnScaleCoordinateValue(100), 
                      UnScaleCoordinateValue(100), 
                      UnScaleCoordinateValue(500),
                      UnScaleCoordinateValue(GetCoordIntInPixel(TextLabel1.Top 
                             + TextLabel1.absBoundingRect.Bottom
                             - TextLabel1.absBoundingRect.Top) + 100));
        End;
    End;
End;


One of the properties in the TtcPDFCanvas class found in the mfk_PDFCanvas module is PdfDocHandle of type TtcPdf. The TtcPdf class can be found in the mfk_tcPDF module, discussed below.


The mfk_tcPDF Module

The mfk_tcPDF module contains a significant number of additional properties and methods that can be used to create sophisticated PDF documents at runtime on the server side. The primary class is TtcPdf and some of its methods are discussed here. Using the methods of this class it is possible to include charts and graphs in a report. When methods are found in both the TtcPDFCanvas class and the TtcPdf class, you should use the ones in the TtcPdfCanvas class. The sample code below adds a demonstration of how to draw an ellipse around the same TextLabel in the other samples, this time in red. In this case, the report is checking a parameter passed in by the initiating module (OpenReport('Report1', '"circle=' + CheckBox1.Checked.ToString + '"');). Please note that in this case, since the methods of TtcPdf are called directly, the coordinates have to be converted to the appropriate measurement; in this case that is accomplished using the GetCoordIntInPixel function. A sample of drawing a curve is also demonstrated below.

FX Code

Procedure Report1.DetailAfterPrint(Sender: TWebControl; Canvas: TWebCanvas);
Begin
    If DrawRectAroundLabel Then
    Begin
        With Canvas as TtcPDFCanvas Do
        Begin
            SelectBrush(clTransparent,bsClear);
            SelectPen(UnScaleCoordinateValue(5), clGreen, psSolid);
            Rectangle(UnScaleCoordinateValue(100), 
                      UnScaleCoordinateValue(100), 
                      UnScaleCoordinateValue(500),
                      UnScaleCoordinateValue(GetCoordIntInPixel(TextLabel1.Top 
                             + TextLabel1.absBoundingRect.Bottom
                             - TextLabel1.absBoundingRect.Top) + 100));
        End;
    End;
    If Uppercase(GetParameterValueByName('circle')) = 'TRUE' Then
    Begin
        With Canvas as TtcPDFCanvas Do
        Begin
            SelectBrush(clTransparent, bsClear);
            SelectPen(UnScaleCoordinateValue(5), clRed, psSolid);
            PdfDocHandle.Ellipse(300,
                                 GetCoordIntInPixel(TextLabel1.Top 
                                        + ((TextLabel1.absBoundingRect.Bottom 
                                        - TextLabel1.absBoundingRect.Top) div 2)) + 47,
                                 GetCoordIntInPixel(TextLabel1.Width),
                                 GetCoordIntInPixel((TextLabel1.absBoundingRect.Bottom 
                                         - TextLabel1.absBoundingRect.Top) div 2) + 50);
        End;
    End;
    With Canvas as TtcPDFCanvas Do
        PdfDocHandle.Curve(100, 300, 120, 280, 200, 340, 100, 350);
End;


For Additional Information

You can find documentation for the PHP classes on the web at http://www.tecnick.com/pagefiles/tcpdf/doc/com-tecnick-tcpdf/TCPDF.html. Please note that the PHP classes, methods and properties do not always match mfk_tcPDF ones.


See Also

Back to top