Skip to content

Commit

Permalink
Merge pull request #5 from amattu2/2.0.0
Browse files Browse the repository at this point in the history
v2.0.0 BREAKING: Rewrite library and label implementations
  • Loading branch information
amattu2 authored Mar 11, 2023
2 parents 6d690a3 + 02ff8e7 commit ed570a9
Show file tree
Hide file tree
Showing 10 changed files with 2,190 additions and 554 deletions.
124 changes: 33 additions & 91 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Introduction

This is a basic PHP project to implement support for generating [Avery.com](https://www.avery.com/templates) label templates using [FPDF](https://fpdf.org).
This is a PHP project to implement support for generating [Avery.com](https://www.avery.com/templates) label templates using [FPDF](https://fpdf.org).

# Templates

Expand Down Expand Up @@ -35,12 +35,15 @@ composer install amattu2/avery-fpdf-labels
composer install fpdf/fpdf
```

**NOTE:** You can use any fork of FPDF you want as long as it implements the same methods as FPDF.

Then

```php
require 'vendor/autoload.php';

$template = new Avery\Avery5160(Fpdf\Fpdf::class);
$template = new amattu2\LabelSheet("Avery5160");
$pdf = new Fpdf\Fpdf();
```

---
Expand All @@ -50,124 +53,63 @@ $template = new Avery\Avery5160(Fpdf\Fpdf::class);
If you choose to install without composer support, you can clone the repository directly. **You will need to include FPDF also.**

```bash
git clone https://github.com/amattu2/avery-fpdf-labels/tree/master
git clone https://github.com/amattu2/avery-fpdf-labels
```

Then

```php
require 'fpdf/Fpdf.php'; // Install FPDF manually
require 'src/Avery5160.php';
require 'src/LabelSheet.php';

$template = new Avery\Avery5160(Fpdf\Fpdf::class);
$template = new amattu2\LabelSheet("Avery5160");
$pdf = new Fpdf\Fpdf();
```

---

### Usage

```PHP
/* follow the setup above */

// Set PDF properties
$template->pdf->SetTitle("FPDF Template Example");
$template->pdf->AddPage("P", "Letter");
$template->pdf->SetFont('Helvetica', '', 11); // Font optional
$template->pdf->SetTextColor(25, 25, 25); // Color optional

// Add labels
$template->add(["ln1", "ln2"]);
// see example.php
## Usage

// Build PDF
$template->writeLabels();
See [example.php](example.php) for demonstration of usage.

// Output PDF
$template->pdf->Output("I", "Labels.pdf");
```

## Methods
### Text Label

### __construct($pdf)
Use the `addTextLabel` method to add a text label to the template. The method accepts an array of strings, an optional alignment parameter, and optional row and column parameters. This would typically be used for address labels.

This is a breaking change from pre-1.0.0 releases. The constructor now takes a classname as a argument, and no longer extends FPDF. This allows more flexibility if you have a need to pass a class that extends FPDF already.

```PHP
/**
* Contructor
*
* @param \FPDF|\Fpdf\Fpdf $pdf
*/
public function __construct($pdf)
```php
addTextLabel(array $lines, ?string $align = "C", int $row = null, int $col = null): void
```

### add(array $lines, int $row = 0, int $col = 0)

This function adds a new label to the list of labels to be produced. There is currently no way to revoke a label once it has added.

```PHP
/**
* Add a single label to the PDF
*
* @param array $lines an array of label liens
* @param integer $row optional desired insert row
* @param integer $col optional diesired intert column
* @throws TypeError
* @throws BadValueException
* @author Alec M. <https://amattu.com>
* @date 2021-09-05T13:49:28-040
*/
public function add(array $lines, int $row = 0, int $col = 0) : void;
```php
$template->addTextLabel([
"line 1",
"line 2",
"line 3",
// ...
])
```

Usage
### Image Label

```PHP
$lines = Array(
"Line 1",
"Line 2",
"Line 3",
"Line 4"
);
Use the `addImageLabel` method to add an image label to the template. The method accepts a path to an image file and optional row and column parameters.

$template->add($lines);
```php
addImageLabel(string $path, int $row = null, int $col = null): void
```

### writeLabels()

```PHP
/**
* Build the completed PDF with labels
*
* NOTE:
* (1) To save resources, no PDF is built until
* this function is called.
*
* @return void
* @throws InvalidStateException
* @author Alec M. <https://amattu.com>
* @date 2021-09-05T13:50:58-040
*/
public function writeLabels() : void;
```php
$template->addImageLabel("https://api.placeholder.app/image/350x350/.png");
```

Usage
### Custom Labels

```PHP
$template->writeLabels();
The `addCustomLabel` method allows you to expand upon the current label types (e.g. adding barcodes). You must instantiate your own implementation of the custom label and pass it to this method.

// PDF content is written, now output PDF as desired
```php
addCustomLabel(LabelInterface $label): void
```

## Variables

### `class->$pdf`

The variable `pdf` is available upon class instantiation. It references a instance of the FPDF class passed to the constructor.

```PHP
$template->pdf->Cell('xyz');
```
The custom label must implement the `LabelInterface` interface. See [/src/LabelInterface.php](/src/LabelInterface.php) for more information.

# Requirements & Dependencies

Expand Down
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@
"name": "Alec M.",
"email": "alecmattu1234@gmail.com"
}
]
],
"require-dev": {
"phpunit/phpunit": "^10.0"
},
"require": {
"fpdf/fpdf": "^1.85"
}
}
Loading

0 comments on commit ed570a9

Please sign in to comment.