Pdf

Create PDF file

Arguments

$name
Name of the predefined PDF
tuloste, lasku, lahete, kirjekuori, rahtikirja, osoitetarra, romutustodistus

Example

$Pdf = $this->get('pdf','writer'); $Pdf->Text('Hellow World!'); $Pdf->outputFile();

PdfWriter

Pdf writer extends FPDF class and brings more effective ways to print content to PDF file.

$Pdf = $this->get('pdf','writer');
$Pdf->addPage();

$Pdf->setArea(20,20);
$Pdf->Text('Header text','header');
$Pdf->Text('Normal text');

$Pdf->output();

Font

Add new font

$Pdf->initFont(Array(
    'name'          => 'myfont',
    'family'    => 'arial',  // times, symbol, helvetica, courier, courieri, calibri
    'bold'          => true,
    'size'      => 16,       // pt
    'lineHeight'    => 1.1,      // times size
    'color'         => 'black'
));

// USe font
$Pdf->Font('myfont');
$Pdf->Text('This is my font');

Modify font parameters:

$Pdf->setFontStyle('normal', 'size', 11);

Set render area

Set are defines an area to print in:

$Pdf->setArea($x,$y,$width,$align);

$x

If $x is not given, width will be the current position.

$x = page-left: Page left margin

$Pdf->setArea('left',$y,$width);

$x = page-center: Page center line - given width/2

$Pdf->setArea('center',$y,$width,'center');

$x = page-right: Page Page right margin - given width

$Pdf->setArea('right',$y,$width,'right');

If $y is not given, width will be the current position.

If $width is not given, width will be the remaining available space from X to right margin

Text

Render given text to active area with active font

$Pdf->Text('Normal text');

Render this text with different font.

$Pdf->Text('Header text','header');

Override font definitions:

$Pdf->Text('Header text',Array(
    'name' => 'header', // If not given, uses active font 
    'size' => 20
    'color' => 'blue'
));

TextHeader

Render given text to active area with active font

$Pdf->TextHeader('Header text');

TextInput

Render label+value -box to active area. NOTE! Not editale PDF, just looks visually as form.

$Pdf->TextInput('25%', 'First name', $Client->name->etunimi);
$Pdf->TextInput('50%', 'Last name', $Client->name->sukunimi);
$Pdf->TextInput('25%', 'Phone', $Client->phone);
// Adds newline if does not fit. If multirow, makes box higher.
$Pdf->TextInput('100%', 'Other infomation', $Client->description);

IMAGE

Render image to given area:

$Pdf->ImageArea(Array(
    'file' => $filename,
    'left' => $x, // Area left
    'top' => $y, // Area top
    'width' => 60, // Area width
    'height' => 40, // Area height
    'margin' => 1, // Margin between image and area. Default 0
    'border' => 'red', // Border color. Default: false.
    'align' => 'left', // left center, right. Default: left
    'valign' => 'middle' // top, middle, bottom. Default: top
    'bg' => 'gray', // Fill color. Default: none
    'border' => 'black' // Border color. Default: none
));

Table

Render table to page

$Pdf->table(Array(
    'x' => 0, // IF not given, uses current X
    'y' => 0, // IF not given, uses current Y
    'width' => 400, // Table width. If not given, is calculated from cols. If col missin, uses available width.
    'margin' => 2, // Cell margin
    'font' => 'text', // Cell font. Name or array.
    'border' => true, // Render border
    'borderColor' => 'cyan', // Border color
    'borderWidth' => 0.1, // Border width
    'header' => true, // If false, header row is not rendered
    'headerBreak' => true, // If false, header row is not rendered when page break
    'headerFont' => 'small', // Font for header row. Name or array
    'headerBg' => 'cyan-light', // Background color for header row
    'rowsNoPageBreakBottom' => 3, // Number of rows that do not trigger PageBreak (to avoid oneline -pages)
    'page-break-inside' => true, // Allow break inside content. Default false.
    'cols' => Array(
        Array(
            'title' => 'Title name',
            'width' => 40, // If not given, calculates avail space from table width
            'align' => 'left', // right, center
            'color' => 'blue', // TextColor
            'bg' => 'gray-light', // FillColor
            'hidden' => false // If true, col is nor rendered
        )
    ),
    'data' => Array(
        Array(
            'Col 1 content',
            'Col 2 content',
            'Col 3 content'
        )
    )
));

New table

You can also create Table object and handle it before writing it to Pdf:

$Table = $Pdf->newTable($params);
$Table->setParam('header', false);

$Table->addCol(Array(
    'title' => 'Col title'
    'width' => 50,
    'align' => 'left'
));

// Data as arguments
$Table->addRow(
    'col 1',
    'col 2',
    'col 3'
);
// Data as array
$Table->addRow(Array(
    'col 1',
    'col 2',
    'col 3'
));

$Table->setRowParam('border', false);    // Set border for last row
$Table->setRowParam('bg', 'green');      // Set bg for last row
$Table->setRowParam('font', 'small');    // Set font for last row
$Table->setRowParam('color', 'green');   // Set font color for last row

$Table->render();

Col shorhand

IF you give multiple arguments to newTable(), they are handled as col definitions.

$Table = $Pdf->newTable('Title', 'Text');
$Table->addRow($title, $text);

If col definition is a string, it will be parsed to params

$Table = $Pdf->newTable('Title:40', 'Text:right')
    ->addCol('Price:40:right');

// number = col width
// left,center,right = Align
// Other string = Title

Variable lists

Font style

Value Description
title Page main title
header Text header
text Normal text
small Small text
mini Minimum sized text

Colors

PdfWriter has color definitions for all eKansio colors with 3 variations:

Value Description
blue Normal blue
blue-light Lighter blue (bg usage)
blue-dark Darker blue

Full method list

Render content

Method Return Description
text($text,$font) - Render multiline text to pdf. Moves Y position.
line(x,y,x2,y2) - Render line
lineH(x,y,width) - Render horizontal line.
lineV(x,y,height) - Render vertical line.
box(array) - Render box

Render position

Method Return Description
setArea(x,y,w,align) $this Set active proint area.
addY(x) - Add value to current Y position
addX(x) - Add value to current X position

Font and colors

Method Return Description
setTextColor($color) - Set active text color. Name or #000000 -value
setDrawColor($color) - Set active line color. Name or #000000 -value
setFillColor($color) - Set active bg color. Name or #000000 -value
initColor($name,$color) $this Assign new color.
initFont(array) $this Assign new font definition.
setFontStyle($font,$name,$value) $this Set value to given font defintion.
font($font[,$param]) $this Set active font. Font name or array. $param override font parameters.

Page info

Method Return Description
setHeaderHeight(int) $this Set margin between header and content in mm
setFooterHeight(int) $this Set margin between header and content in mm
setMarginLeft(int) $this Set left margin in mm
setMarginRight(int) $this Set right margin in mm.
setMarginTop(int) $this Set top margin in mm.
setMarginBottom(int) $this Set bottom margin in mm
setContentMarginTop(int) $this Set margin between header and content in mm
setContentMarginBottom(int) $this Set margin between content and footer in mm
setPageTop(int) $this Set page top in mm
setPageBottom(int) $this Set page bottom in mm
getMarginTop() int Get top margin in mm
getMarginBottom() int Get bottom margin in mm
getMarginRight() int Get right margin in mm
getMarginLeft() int Get left margin in mm
getHeaderTop([$add]) int Get header top position in mm
getHeaderHeight() int Get header height in mm
getHeaderBottom([$add]) int Set header bottom position in mm
getContentMarginTop([$add]) int Get margin between header and content in mm
getContentMarginBottom([$add]) int Get margin between content and footer in mm
getFooterTop([$add]) int Get footer top position in mm
getFooterHeight() int Get footer height in mm
getFooterBottom([$add]) int Set footer bottom position in mm
getPageTop([$add]) int Get content top position in mm
getPageBottom([$add]) int Get content bottom position in mm (PageBreakTrigger)
getPageLeft([$add]) int Get content left position in mm
getPageRight([$add]) int Get content right position in mm
getPageCenter([$add]) int Get content center position in mm
getPageWidth([$add]) int Get content width in mm
getPageAvailableHeight([$add]) int Get available space left in current position ( pageBottom - getY() )

File

Method Return Description
outputFile() - Send Pdf to browser (opens in browser).
downloadFile() - Start file download.
saveToFile($filename) - Save Pdf to server.
getContent() string Get file content string.
renderTo($Pdf) - Render PDF to another PDF.

Debug

Method Return Description
renderMargins([$small,$large]) - Render margin helpers
renderGuideY($int[,$text]) - Render horizontal guide line.
renderGuideX($int,[$text]) - Render vertical guide line.
renderGrid([$small,$large]) - Render helper grid
setAreaBorder($bool) $this If true, renders border to all Text() printouts (to see boundaries)

DEPRECATED - EI TARKISTETTU

Everything under this is available not not recommended !!!!

$Pdf = $this->get('pdf','lasku');
$Pdf->sivu(Array(
    lasku => Array(
        laskunumero => $sivu->laskunumero,
        laskupaiva  => $sivu->laskupaiva
    )
  ))
  ->tulosta();

Create file

$Pdf = $this->get('pdf','tuloste');
$Pdf->addPage();
$Pdf->setFont("Arial",24,"B");      // Valid from now on. courier, helvetica, times
$Pdf->textarea(10,10,"Page title: ".$sivu->getTitle());
$Pdf->setFilename("new_pfd_file");  // Filename of the file to be printed
$Pdf->outputFile();

Table

// Taulukon sisältö
$data = Array(
    Array("1A","1B","1C"),
    Array("2A","2B","2C"),
);

// SARAKKEIDEN ASETUKSET
$param = Array(
    width  => Array(40,40,110), // leveus milleinä
    align  => Array('','','R'), // R|L|C
    color  => Array('0,0,0','0,0,0','0,256,0'), // R,G,B lukuarvona 0-256
);

$Pdf->table(11,70,6,$data,$param); // x,y,rivi h

Image

// url,x,y,maxW,maxY
// From account folder in server
$Pdf->kuva( $this->get('account')->getFile("logo_for_pdf.png") ,10,270,100,20);
// From attachment -field
$Pdf->kuva( $sivu->kuva->file ,10,270, 100,20);

Barcode

$Pdf->barcode('123456123456' ,$x,$y ,20,5);

// Shorthand for
$Pdf->Image( $this->get('barcode','123456123456')->getFile() ,$x,$y ,20,5);

QR code

$Pdf->Image( $this->get('qrcode','http://www.ekansio.fi')->getFile() ,$x,$y ,25,25);

Graphics

// Preferences: valid from now on
$Pdf->setDrawColor(255,0,0);
$Pdf->setFillColor(125,125,125);

$Pdf->line(10,25,100,25);
$Pdf->vaakaviiva(25,30,10);
$Pdf->pystyviiva(30,25,10);
$Pdf->laatikko(20,20,40,40);
$Pdf->setLineWidth($w); // $w float width
$Pdf->Rect($x,$y,$w,$h,$style=''); // $style: 'D'/'F'/'DF'/'FD'

Open PDF template

// From account folder in server
$pdf->setSourceFile( $this->get('account')->getFile("A4_lomakepohja.pdf") );

// From attachment -field
$pdf->setSourceFile( $sivu->liitetiedosto->file );

// Render template to pdf
$templatePdf = $pdf->importPage(1); // First page
$pdf->useTemplate($templatePdf, 0, 0, 210);

Save file to server

$pdf->tallenna();

Send file to browser

$pdf->outputFile();

Send file to browser

$pdf->outputFile();

Get file content

$content = $pdf->getContent();

NEW METHODS

These are named as methods in upcoming ePdf class.

Open PDF template

Predefined fonts: normal, title, heading, heading2. Define new font and use it.

$pdf->addFont('custom',Array(
    'family'     => 'Arial',
    'size'       => 10,
    'weight'     => 'normal', // bold
    'lineheight' => '',       // Default: 1.2*size
    'decoration' => 'none',   // underline
    'style'      => 'normal'  // italic
));
$pdf->useFont('custom');
$pdf->renderText('Text with custom font');

Full methd list

Method return Description
addFont($name,$params) this Define font.
useFont($name) this Use defined font.
Text return Description
renderText($txt) this Render text to current position. Add lineheight to Y.