Giter Club home page Giter Club logo

naitsirch-tcpdf-extension's Introduction

TCPDF Extension

TCPDF is a PHP library to generate PDF documents. It is very feature rich, but not easy to use.

This library builds on top of the TCPDF library. At the current state it provides only a smart API to create tables in a comfortable way.

ABANDONED

This repository is abandoned and should not be used anymore. Please change from naitsirch/tcpdf-extension to the compatible stollr/tcpdf-extension.

Features

  • More comfortable creation of tables

Installation

The easiest way to use this library is by installing it via Composer.

Add this to your project's composer.json:

{
    "require": {
        "naitsirch/tcpdf-extension": "dev-master"
    }
}

Usage

Creating tables

First you have to create an instance of TCPDF.

use TCPDF;

$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetTitle('My PDF file');
$pdf->SetMargins(20, 20, 20);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->SetAutoPageBreak(true, 9);
$pdf->SetFont('dejavusans', '', 10);

Please look into the documentation of the TCPDF class if you are not familiar with it.

In the next step you can create the table.

use Tcpdf\Extension\Table\Table;

$pdf->AddPage(); // add a new page to the document
$table = new Table($pdf);
$table
    ->newRow()
        ->newCell()
            ->setText('Last Name')
            ->setFontWeight('bold')
        ->end()
        ->newCell()
            ->setText('First Name')
            ->setFontWeight('bold')
        ->end()
        ->newCell()
            ->setText('DOB')
            ->setFontWeight('bold')
        ->end()
        ->newCell()
            ->setText('Email')
            ->setFontWeight('bold')
        ->end()
    ->end()
    ->newRow()
        ->newCell('Foo')->end()
        ->newCell('John')->end()
        ->newCell('1956-04-14')->end()
        ->newCell('[email protected]')->end()
    ->end()
;
$table->end(); // this prints the table to the PDF. Don't forget!

The above code shows how to create a very simple table. But you are able to customize table cells in different ways:

$table
    ->newRow()
        ->newCell('Last Name')         // you can set the cell content like this
            ->setText('Override Text') // or like this
            ->setFontWeight('bold')    // set font weight 'bold' or 'normal'
            ->setAlign('L')            // text alignment ('L', 'C', 'R' or 'J')
            ->setVerticalAlign('top')  // vertical alignment ('top', 'bottom' or 'middle')
            ->setBorder(1)             // border format (like in TCPDF::MultiCell)
            ->setRowspan(1)            // rowspan like in HTML
            ->setColspan(2)            // colspan like in HTML
            ->setFontSize(10)          // unit for font size is same as defined in TCPDF
            ->setMinHeight(10)         // defining min-height of the cell like in CSS
            ->setPadding(2, 4)         // setting cell padding (inner margin) like in CSS
            ->setPadding(2, 4, 5, 6)   // or like this
            ->setWidth(125)            // unit for width is same as defined in TCPDF
        ->end()

Background

Define a background color:

$table
    ->newRow()
        ->newCell('Last Name')
            ->setBackgroundColor('#ff4400')                 // hexadecimal RGB color code
            ->setBackgroundColor(array(250, 80, 10)         // decimal RGB color array
        ->end()
    ->end()

It is possible to define a background image for each table cell.

$table
    ->newRow()
        ->newCell('Last Name')
            ->setBackgroundDpi(300)                         // define the resolution for the printing
            ->setBackgroundImage('/path/to/my/image.png')   // pass the path to your image
            ->setBackgroundImage($binaryImageString)        // or pass the binary file content of your image
        ->end()
    ->end()

naitsirch-tcpdf-extension's People

Contributors

detook avatar doughayward avatar ianmustafa avatar leonex-cs1 avatar stollr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

naitsirch-tcpdf-extension's Issues

cell setBackgroundColor doesn't work and a couple other minor things

  1. I noticed in Cell.php there's a function called setBackgroundColor(). This is exactly what I need to do, but it does not work. I realize that I could use a solid color background image to do the same thing, but I'm trying to keep the size of the pdfs generated as small as possible as I hope to be able to keep them email-able size and they may end up being several pages. The use case is that the table rows need to have alternating rows with white and (slightly) grey backgrounds. Is this not supported?

  2. Part of my code needs to take a variable number of data records in an array and create table rows with them, so I'll need to create rows with a foreach loop. I'm rather new to the command chaining syntax, so it's not obvious how to do this. Could you update the documentation showing a simple example of how to create rows and cells dynamically in a foreach loop? Perhaps with the first (header) row outside of the loop?

  3. It would be really great if you could include a brief statement in the documentation how to install your extension if composer is not available. (We don't always get the environment we want to work in) From what I understand, I essentially have to require_once each of the pages in the package. So far this approach seems to work, but if there's a better way, it would be nice to know.

Thank you so much for your time.

Add Image to a newCell()

Hello,

your extension is very cool!

Is it possible to put an Image to a new Cell?

Kind regards
Buschdieb

setBackgroundColor() doesn't work.

It appears no matter what properties you enter into setBackgroundColor() the result is a great load of nothing... Up to now I've tried

     //Hex
     $table->newRow()->newCell()->setBackgroundColor("#222");
     $table->newRow()->newCell()->setBackgroundColor("#222222");
     $table->newRow()->newCell()->setBackgroundColor("222222");
     $table->newRow()->newCell()->setBackgroundColor(222222);
     //RGB
     $table->newRow()->newCell()->setBackgroundColor(125,125,125);
     $table->newRow()->newCell()->setBackgroundColor(array(125,125,125));

All to no avail, the function certainly exists. Along with some documentation specifying it's use. I have tried using a background image for this, however as I will never really know the full height/width of each cell this becomes an issue as there doesn't seem to be an option for stretching the image. As a work around I've started supplying a full height half width image, but this obviously isn't the most ideal solution.

Any ideas are more than welcomed. If it involves working on the library implementation itself I'm happy to give it a shot, however I feel I would probably need some direction.

Table header

Hello,

your extension is great! It is a nice feature! It is possible to set a table header like th in html?

Kind regards
Buschdieb

infinite loop on creating a table with large text block

Hi there, ive encontered an issue, when creating a pdf with 2 columns 7 rows if one of the rows has a text size greater than the page remaining space / size the class TableConverter.php enters a infinite loop of add page until memory exausted/timeout.
It happens on line 507 it loops adding page but the size of the row ($rowHeights[$r] >= $remainingPlace) is always bigger than the page remaining space / size.
Values my case before and after a few "$pdf->AddPage();" loops
$rowHeights[$r] = 269.8749999999999
$remainingPlace = 266.0000833333333

How to add html in table cell?

I need to insert the following code into my table cells. How can I do it?

<td class="linebreak"><p>Text only allowed to extend 50% of the cell.</p></td>

td.linebreak p {
    width: 50%;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.