Skip to content

Instantly share code, notes, and snippets.

@SimpleProgrammingAU
Created February 4, 2015 11:40
Show Gist options
  • Select an option

  • Save SimpleProgrammingAU/2e7d8c21f400d65a3a58 to your computer and use it in GitHub Desktop.

Select an option

Save SimpleProgrammingAU/2e7d8c21f400d65a3a58 to your computer and use it in GitHub Desktop.
PDF Watermarking Tool
<?php
error_reporting(E_ALL);
require_once("watermarker/fpdf/fpdf.php");
require_once("watermarker/fpdi/fpdi.php");
require_once("watermarker/pdfwatermark.php");
require_once("watermarker/pdfwatermarker.php");
$image = $_GET['username'];
$pdf = substr($_GET['pdf'], 0, strlen($_GET['pdf'])-1);
$filename = sys_get_temp_dir() . '/' . uniqid() . '.pdf';
try {
//Specify path to image
$watermark = new PDFWatermark('test@email.com');
//Specify the path to the existing pdf, the path to the new pdf file, and the watermark object
$watermarker = new PDFWatermarker($pdf, $filename, $watermark);
} catch (Exception $e) {
print($e->getMessage());
}
//Set the position
$watermarker->setWatermarkPosition('center');
//Save the new PDF to its specified location and open
$watermarker->watermarkPdf();
?>
<?php
/*
The MIT License (MIT)
Copyright (c) 2012 BinaryStash
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.
*/
//I Have edited the original PDFWatermark class to attmept to allow it to use a png image created on the fly, the original files are at https://github.com/binarystash/pdf-watermarker
Class PDFWatermark {
private $path;
private $height;
private $width;
function __construct($string) {
//Create blank image
$image = imagecreatetruecolor(333, 20);
//Define a colour for the background
$bgColor = imagecolorallocate($image, 255, 255, 255);
//Set the background colour
imagefill($image, 0, 0, $bgColor);
//Define the colour of the text
$textColor = imagecolorallocate($image, 0, 0, 0);
//Define a font and size of text
//TODO - Work out whether I can do this or not
//Draw in some text
imagestring($image, 5, 0, 0, $string, $textColor);
//Resize the image
$resized = imagecreatetruecolor(1000, 60);
imagecopyresampled($resized, $image, 0, 0, 0, 0, 999, 60, 333, 20);
//Output the image
imageinterlace($resized,false);
imagesavealpha($resized,true);
$this->path = sys_get_temp_dir() . '/' . uniqid() . '.png';
imagepng($resized, $this->path);
//Clear the memory.
imagedestroy($image);
imagedestroy($resized);
}
public function getFilePath() {
return $this->path;
}
public function getHeight() {
return 60;
}
public function getWidth() {
return 1000;
}
}
?>
<?php
/*
The MIT License (MIT)
Copyright (c) 2012 BinaryStash
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.
*/
//Nothing has been changed from the original here
class PDFWatermarker {
private $_originalPdf;
private $_newPdf;
private $_tempPdf;
private $_watermark;
private $_imagePositionOutput;
public function __construct($originalPdf,$newPdf,$watermark) {
$this->_originalPdf = $originalPdf;
$this->_newPdf = $newPdf;
$this->_tempPdf = new FPDI();
$this->_watermark = $watermark;
$this->_validateAssets();
$this->setWatermarkPosition();
}
private function _validateAssets() {
if ( !file_exists( $this->_originalPdf ) ) {
throw new Exception("Inputted PDF file doesn't exist: " . $this->_originalPdf);
}
else if ( !file_exists( $this->_watermark->getFilePath() ) ) {
throw new Exception("Watermark doesn't exist: " . $this->_watermark->getFilePath());
}
}
/**
* $position string - 'center','topright', 'topleft', 'bottomright', 'bottomleft'
*/
public function setWatermarkPosition($position="center") {
$this->_imagePositionOutput = $position;
}
private function _watermarkPdf() {
$pageCtr = $this->_tempPdf->setSourceFile($this->_originalPdf);
for($ctr = 1; $ctr <= $pageCtr; $ctr++){
$this->_watermarkPage($ctr);
}
}
private function _watermarkPage($page_number) {
$templateId = $this->_tempPdf->importPage($page_number);
$templateDimension = $this->_tempPdf->getTemplateSize($templateId);
if ( $templateDimension['w'] > $templateDimension['h'] ) {
$orientation = "L";
}
else {
$orientation = "P";
}
$this->_tempPdf->addPage("P",array($templateDimension['w'],$templateDimension['h']));
$this->_tempPdf->useTemplate($templateId);
$wWidth = ($this->_watermark->getWidth() / 96) * 25.4; //in mm
$wHeight = ($this->_watermark->getHeight() / 96) * 25.4; //in mm
$watermarkPosition = $this->_determineWatermarkPosition( $wWidth,
$wHeight,
$templateDimension['w'],
$templateDimension['h']);
$this->_tempPdf->Image($this->_watermark->getFilePath(),$watermarkPosition[0],$watermarkPosition[1],-96);
}
private function _determineWatermarkPosition( $wWidth, $wHeight, $tWidth, $tHeight ) {
switch( $this->_imagePositionOutput ) {
case 'topleft':
$x = 0;
$y = 0;
break;
case 'topright':
$x = $tWidth - $wWidth;
$y = 0;
break;
case 'bottomright':
$x = $tWidth - $wWidth;
$y = $tHeight - $wHeight;
break;
case 'bottomleft':
$x = 0;
$y = $tHeight - $wHeight;
break;
default:
$x = ( $tWidth - $wWidth ) / 2 ;
$y = ( $tHeight - $wHeight ) / 2 ;
break;
}
return array($x,$y);
}
public function watermarkPdf() {
$this->_watermarkPdf();
return $this->_tempPdf->Output($this->_newPdf);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment