TYPO3 CMS Integration & Extension development
Thomas Deuling

Fluid-FPDF for TYPO3

This extension provides you a complete set of ViewHelpers for dealing with FPDF by using Fluid. You can use theViewHelpers easily in own Extensions just by defining the fpdf-Namespace.Additionally you're able to use the build in plugin, for displaying predefined PDF. This plugin offers you automaticallyall available PDFs.

Features:

  • Fluid-ViewHelper for all FPDF-Functions
  • Usable in own Extensions
  • Predefine PDFs by using Fluid-Templates and provide them to your page-editors

ViewHelpers


fpdf:addFont

Imports a TrueType, OpenType or Type1 font and makes it available.It is necessary to generate a font definition file first with the MakeFont utility.The definition file (and the font file itself when embedding) must be present in the font directory. If it is not found,the error "Could not include font definition file" is raised.

Parameter

NameTypeDefaultDescription
familystringFont family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
stylestringFont style. (N: regular, B: bold, I: italic)
filenamestringThe font definition file. By default, the name is built from the family and style, in lower case with no space.

Return

void

Usage:

<fpdf:setFont family="Arial" style="B" size="10.5" />

fpdf:pdf

Creates a new PDF.

Parameter

NameTypeDefaultDescription
filenamestringFluidFpdf.pdfThe name of the file
destinationstringIDestination where to send the document. It can take one of the following values: I, D, F, S
orientationstringPDefault page orientation.
unitstringmmUser unit.
sizestringA4The size used for pages.
titlestringFluid-FpdfDefines the title of the document.
subjectstringFluid-FpdfDefines the subject of the document.
authorstringFluid-FpdfFDefines the author of the document.
keywordsstringTYPO3 Fluid FPDF typo3(at)coding(dot)msAssociates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
fontPathstringEXT:fluid_fpdf/Resources/Private/FontsDefines the font path for the included fonts (for example: EXT:fluid_fpdf/Resources/Private/Fonts) or some TYPO3 path like /fileadmin/fonts/
imagePathstringEXT:fluid_fpdf/Resources/Private/ImagesDefines the image path for the included images (for example: EXT:fluid_fpdf/Resources/Private/Images) or some TYPO3 path like /fileadmin/fonts/
characterMaparrayDefines a character map, for mapping characters. For Example, for mapping €-character with character at ASCII-No. 162, use {162: '€'}

Return

void

Usage:

<fpdf:pdf>
    <fpdf:addPage orientation="P" size="A4">
        // Your PDF content...
    </fpdf:addPage>
</fpdf:pdf>

fpdf:setFont

Sets the font used to print character strings.

Parameter

NameTypeDefaultDescription
familystringArialFamily font.
stylestringNFont style. (N: regular, B: bold, I: italic, U: underline)
sizestring12Font size in points.

Return

void

Usage:

<fpdf:setFont family="Arial" style="B" size="10.5" />

fpdf:setSourceFile

Set the source file for PDF page templates.

Parameter

NameTypeDefaultDescription
filestringEXT:fluid_fpdf/Resources/Private/Pdf/TYPO3_2012-logo_sRGB_color.pdfFilename with abs path of the source

Return

int - Number of pages found in source file

Usage:

<fpdf:setSourceFile file="EXT:fluid_fpdf/Resources/Private/Pdf/Normbriefbogen_DIN5008.pdf" />

fpdf:useTemplate

Prints a PDF-Template into a page

Parameter

NameTypeDefaultDescription
pageNoint0The page number
xint0The x-position
yint0The y-position
wint0The new width of the template
hint0The new height of the template
adjustPageSizebooleanfalseIf set to true the current page will be resized to fit the dimensions of the template

Return

void

Usage:

<fpdf:useTemplate pageNo="1" x="0" y="0" w="0" h="0" adjustPageSize="0" />

Frequently asked questions (FAQ)

How to set an background PDF

Just use the fpdf:useTemplate-ViewHelper, for example:

<fpdf:pdf>
    <fpdf:addPage orientation="P" size="A4">
        <fpdf:setSourceFile file="EXT:fluid_fpdf/Resources/Private/Pdf/Normbriefbogen_DIN5008.pdf" />
        <fpdf:useTemplate pageNo="1" x="0" y="0" />
        <f:comment>
            // Your PDF content...
        </f:comment>
    </fpdf:addPage>
</fpdf:pdf>

How to use your own fonts in PDF

For using your own fonts in Fluid-FPDF, you need to convert the fonts into a special format.

The FPDF-Website supports a small Converting-Tool: http://www.fpdf.org/makefont/

Fluid-FPDF Font-Converting Tool

Just choose your TTF-File and select the required encoding (for example ISO-8859-15). The result are two files - onephp-File and one z-File.

Fluid-FPDF Font-Converting Result

For example, you want to convert the Ubunut-Font which is available on Google-Webfonts. You need the regular and boldfont. Downloaded you receive the following files:

  • Ubuntu-Regular.ttf
  • Ubuntu-Bold.ttf

Once converted, you have these files:

  • Ubuntu-Regular.php
  • Ubuntu-Regular.z
  • Ubuntu-Bold.php
  • Ubuntu-Bold.z

Follow the next steps, in order to use the font:

  1. Upload these two file into your TYPO3-Installation
  2. Define the new Fontpath in fpdf:pdf
  3. Add fonts with the fpdf:addFont
  4. Set the required font
  5. Write your text
<fpdf:pdf fontPath="EXT:theme_bootstrap4/Resources/Private/Fonts">
    <!-- Add required Fonts -->
    <fpdf:addFont family="Ubuntu" style="N" filename="Ubuntu-Regular.php" />
    <fpdf:addFont family="Ubuntu" style="B" filename="Ubuntu-Bold.php" />
    <fpdf:addPage orientation="P" size="A4">
        <!-- Set font -->
        <fpdf:setFont family="Ubuntu" style="N" size="12" />
        <!-- Write your text -->
        <fpdf:multiCell width="100" height="5" text="Hello world" />
        <f:comment>
            // Your PDF content...
        </f:comment>
    </fpdf:addPage>
</fpdf:pdf>

Notice:

You need some support by converting your font? Get in contact with us: typo3(at)coding(dot)ms

A character of my font isn't displayed well in PDF (for example the €-character) - what to do?

First create a character dump of the string, in order to identify which characters are used:

<fpdf:debug.stringCharacterDump string="1.20 €" />

The result should look like this:

1 : 49
. : 46
2 : 50
0 : 48
 : 32
? : 63
 : 0

Now you know, your €-character is placed by the character with ASCII-No. 63.

In the next step, you perform a font dump with the font you want to use:

<fpdf:debug.fontDump />

I our example, the character on number 63 is displayed as ? - that's not what we expect. Take a closer look on yourfont dump and search for the €-character. We found it on ASCII-No. 162.

FAQ

How to write a PDF template, which returns the PDF as string?

This is useful, if you like to attach the created PDF to a mail or something else.

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
      xmlns:fpdf="http://typo3.org/ns/CodingMs/FluidFpdf/ViewHelpers"
      data-namespace-typo3-fluid="true"><f:spaceless>
<f:format.raw>
    <fpdf:pdf destination="S">
        <fpdf:addPage orientation="P" size="A4">
            <fpdf:setFont family="Arial" style="B" size="10" />
            <fpdf:text x="10" y="10" text="Hello Pdf-World" />
            <fpdf:setXY x="40" y="40" />
            <fpdf:cell width="40" height="10" text="Hello World!" />
        </fpdf:addPage>
    </fpdf:pdf>
</f:format.raw>
</f:spaceless></html>

The important thing is, that there're no spaces and the PDF-Code is not escaped!

Contribution

Every contribution is welcome! Hands on documentation, tutorials, Blog posts, Bug reporting, Bug fixing!

Support