Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
filippotoso committed Mar 12, 2022
0 parents commit f7aa88d
Show file tree
Hide file tree
Showing 22 changed files with 1,248 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test
vendor
composer.lock
update.bat
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2022 Filippo Toso

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
132 changes: 132 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# PDF Watermarker
PDFWatermarker enables you to add a text or an image as a watermark to existing PDF files. It uses FPDF that allows you to write PDF files and FPDI that allows you to import existing PDF documents into FPDF.

Using it, you can:

* Use a text and TTF to create a watermark
* Use jpg and png ( with alpha channels ) files with a 96 DPI resolution as a watermark
* Easily position the watermark on the pages of the PDF file

## Installation

Installing using composer

``` bash
composer require filippo-toso/pdf-watermarker
```
## Usage

In vanilla PHP you can watermark a PDF with an image in this way:

``` php
<?php

use FilippoToso\PdfWatermarker\Support\Pdf;
use FilippoToso\PdfWatermarker\Support\ImageWatermark;
use FilippoToso\PdfWatermarker\PdfWatermarker;

// Specify path to the existing pdf
$pdf = new Pdf('my.pdf');

// Specify path to image. The image must have a 96 DPI resolution.
$watermark = new Watermark('watermark.png');

// Create a new watermarker
$watermarker = new PDFWatermarker($pdf, $watermark);

// Save the new PDF to its specified location
$watermarker->save('output.pdf');

```

## Options

You can also specify additional options:

``` php
use FilippoToso\PdfWatermarker\Support\Position;

// Set the position of the watermark including optional X/Y offsets
$position = new Position(Position::BOTTOM_CENTER, -50, -10);

// All possible positions can be found in Position::options
$watermarker->setPosition($position);

// Place watermark behind original PDF content. Default behavior places it over the content.
$watermarker->setAsBackground();

// Only Watermark specific range of pages
// This would only watermark page 3 and 4
$watermarker->setPageRange(3, 4);

```

## Output Methods

You can get the watermarked PDF in the following ways:

``` php
// The filename is optional for all output options
$watermarker->save();

// Start a download of the PDF
$watermarker->download('output.pdf');

// Send the PDF to standard out
$watermarker->output('output.pdf');

```

## Laravel Support

In Laravel you can use the two facades: ImageWatermarker and TextWatermarker:

``` php
use FilippoToso\PdfWatermarker\Facades\ImageWatermarker;
use FilippoToso\PdfWatermarker\Support\Position

ImageWatermarker::input('input.pdf')
->watermark('watermark.png')
->output('laravel-image.pdf')
->position(Position::BOTTOM_CENTER, -50, -10)
->asBackground()
->pageRange(3, 4)
->save();
```

``` php
use FilippoToso\PdfWatermarker\Facades\TextWatermarker;
use FilippoToso\PdfWatermarker\Support\Position

TextWatermarker::input('input.pdf')
->output('laravel-text.pdf')
->position(Position::BOTTOM_CENTER, -50, -10)
->asBackground()
->pageRange(3, 4)
->text('Hello World')
->angle(25)
->font('arial.ttf')
->size('25')
->color('#CC00007F')
->save();
```

You can also generate a valid response to stream or download the file:

``` php
use FilippoToso\PdfWatermarker\Facades\ImageWatermarker;

// Within a controller action
return ImageWatermarker::input('input.pdf')
->watermark('watermark.png')
->download('example.pdf');
```

``` php
use FilippoToso\PdfWatermarker\Facades\ImageWatermarker;

// Within a controller action
return ImageWatermarker::input('input.pdf')
->watermark('watermark.png')
->stream('example.pdf');
```
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "filippo-toso/pdf-watermarker",
"description": "Simple PDF Watermarker with Laravel support",
"license": "MIT",
"authors": [{
"name": "Filippo Toso",
"email": "[email protected]"
}],
"require": {
"php": ">=7.1.0",
"setasign/fpdi-fpdf": "^2.2"
},
"autoload": {
"psr-4": {
"FilippoToso\\PdfWatermarker\\": "src"
}
},
"extra": {
"laravel": {
"aliases": {
"PdfWatermark": "FilippoToso\\PdfWatermarker\\Facades\\PdfWatermarker",
"ImageWatermark": "FilippoToso\\PdfWatermarker\\Facades\\ImageWatermarker"
}
}
}
}
27 changes: 27 additions & 0 deletions src/Contracts/Watermark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace FilippoToso\PdfWatermarker\Contracts;

interface Watermark
{
/**
* Return the path to the tmp file
*
* @return string
*/
public function getFilePath();

/**
* Returns the watermark's height
*
* @return int
*/
public function getHeight();

/**
* Returns the watermark's width
*
* @return int
*/
public function getWidth();
}
62 changes: 62 additions & 0 deletions src/Contracts/Watermarker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace FilippoToso\PdfWatermarker\Contracts;

use FilippoToso\PdfWatermarker\Support\Position;

interface Watermarker
{
/**
* Set page range.
*
* @param int $startPage - the first page to be watermarked
* @param int|null $endPage - (optional) the last page to be watermarked
*/
public function setPageRange($start, $end = null);

/**
* Set the Position of the Watermark
*
* @param Position $position
* @return void
*/
public function setPosition(Position $position);

/**
* Set the watermark as background.
*
* @return void
*/
public function setAsBackground();

/**
* Set the watermark as overlay.
*
* @return void
*/
public function setAsOverlay();

/**
* Save the PDF.
*
* @param $file
* @return void
*/
public function save($file);

/**
* Download the PDF.
*
* @param $file
* @return void
*/
public function download($file);

/**
* Send the PDF to th standard.
*
* @param $file
* @return void
*/
public function output($file);
}
27 changes: 27 additions & 0 deletions src/Facades/ImageWatermarker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace FilippoToso\PdfWatermarker\Facades;

use Illuminate\Support\Facades\Facade;
use FilippoToso\PdfWatermarker\Laravel\ImageWatermarker as FacadeAccessor;

/**
* @method static \FilippoToso\PdfWatermarker\Laravel\ImageWatermarker output($filename)
* @method static \FilippoToso\PdfWatermarker\Laravel\ImageWatermarker position($position, $offsetX = 0, $offsetY = 0)
* @method static \FilippoToso\PdfWatermarker\Laravel\ImageWatermarker asBackground()
* @method static \FilippoToso\PdfWatermarker\Laravel\ImageWatermarker asOverlay()
* @method static \FilippoToso\PdfWatermarker\Laravel\ImageWatermarker pageRange($fromPage, $toPage = null)
* @method static \FilippoToso\PdfWatermarker\Laravel\ImageWatermarker save($output = null)
* @method static \FilippoToso\PdfWatermarker\Laravel\ImageWatermarker watermark($filename)
* @method static \Illuminate\Http\Response stream($filename = null)
* @method static \Illuminate\Http\Response download($filename = null)
*
* @see \FilippoToso\PdfWatermarker\Laravel\ImageWatermarker
*/
class ImageWatermarker extends Facade
{
protected static function getFacadeAccessor()
{
return FacadeAccessor::class;
}
}
32 changes: 32 additions & 0 deletions src/Facades/TextWatermarker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace FilippoToso\PdfWatermarker\Facades;

use Illuminate\Support\Facades\Facade;
use FilippoToso\PdfWatermarker\Laravel\TextWatermarker as FacadeAccessor;

/**
* @method static \FilippoToso\PdfWatermarker\Laravel\TextWatermarker output($filename)
* @method static \FilippoToso\PdfWatermarker\Laravel\TextWatermarker position($position, $offsetX = 0, $offsetY = 0)
* @method static \FilippoToso\PdfWatermarker\Laravel\TextWatermarker asBackground()
* @method static \FilippoToso\PdfWatermarker\Laravel\TextWatermarker asOverlay()
* @method static \FilippoToso\PdfWatermarker\Laravel\TextWatermarker pageRange($fromPage, $toPage = null)
* @method static \FilippoToso\PdfWatermarker\Laravel\TextWatermarker save($output = null)
* @method static \FilippoToso\PdfWatermarker\Laravel\TextWatermarker output($filename)
* @method static \FilippoToso\PdfWatermarker\Laravel\TextWatermarker text($text)
* @method static \FilippoToso\PdfWatermarker\Laravel\TextWatermarker font($font)
* @method static \FilippoToso\PdfWatermarker\Laravel\TextWatermarker size($size)
* @method static \FilippoToso\PdfWatermarker\Laravel\TextWatermarker angle($angle)
* @method static \FilippoToso\PdfWatermarker\Laravel\TextWatermarker color($color)
* @method static \Illuminate\Http\Response stream($filename = null)
* @method static \Illuminate\Http\Response download($filename = null)
*
* @see \FilippoToso\PdfWatermarker\Laravel\TextWatermarker
*/
class TextWatermarker extends Facade
{
protected static function getFacadeAccessor()
{
return FacadeAccessor::class;
}
}
Loading

0 comments on commit f7aa88d

Please sign in to comment.